Class selector in CSS
In CSS, Class is used to style the element with the specified class. Classes are often used for group objects that want to have the same format attribute, each class name can be used with multiple elements.
To declare Class in CSS, we use the following syntax:
.ten_class {
định dạng các thuộc tính CSS
}
For example:
.quantrimang {
background-color: black;
color: white;
font-size: 20px;
To define HTML elements by class name, we use class="ten_class"
.
For example:
Website TipsMake.com
Note :
- Class can be used with any HTML element.
- Class names are only allowed to contain letters, numbers, underscores
_
, hyphens-
. Class name should not start with numbers. - The class name distinguishes between uppercase and lowercase letters.
Example 1: Select and style all elements with class = "intro":
.intro {
background-color: #58257b;
color: white;
}
Result:
Full code:
Welcome to Website TipsMake.com
Category Technology Village.
Technology
Introducing TipsMake.com
Example 2: Style all elements
with class = "hometown" (only applicable for
)
p.hometown {
background: #58257b;
color: white;
}
Full code:
Welcome to Website TipsMake.com
Category Technology Village
Technology category
Science Category
Application category
Example 3: An element can have multiple classes
The element has two combined classes
This heading is not affected by the center class
This paragraph will be centered and red
This paragraph will be centered, red and size
Very big font
See more:
- Learn Basic CSS
- Learn Advanced CSS
You should read it
May be interested
- Deeply understand Color in CSScss supports up to 140 colors with unique names and lots of colors under the values of hex, rgb, rgba, hsl, hsla.
- Unit - Unit of measurement in CSScss supports a lot of length measurement units, also called units, to apply to attributes that need it.
- Gradient - color linear transformation in CSSthe gradient allows the creation of colored background elements, which are the transition effects between two or more specified colors.
- Shadow effect in CSSthe shadow attribute in css is used to set the shadow for the element elements in the website.
- Some properties to handle Text in CSSlearn how to handle text too long, causing overflow of the element that contains it.
- Use Web Font in CSSweb font is the font that is put on the website by website designers, without installing on the user's computer.