The syntax of CSS consists of three parts: selector, property, and value. Here is an example:
selector {
property: value;
}
- Selector: Selects the HTML element that you want to style. It could be a tag name, class, ID, or other selectors.
- Property: Specifies the property of the selected element that you want to change, such as color, font-size, padding, margin, etc.
- Value: Sets the value of the property that you want to apply to the selected element.
For example, the following CSS code sets the text color of all <p>
elements to red:
p {
color: red;
}
Here, p
is the selector, color
is the property, and red
is the value.
CSS also supports multiple selectors and properties in a single rule. For example:
h1, h2, h3 {
font-family: Arial, sans-serif;
color: #333;
}
Here, the rule sets the font family and color for all <h1>
, <h2>
, and <h3>
elements.
CSS also allows you to group multiple rules together. For example:
h1 {
font-family: Arial, sans-serif;
color: #333;
}
p {
font-family: Georgia, serif;
font-size: 16px;
line-height: 1.5;
}
Here, two rules are grouped together to set the font family and color for all <h1>
elements, and the font family, size, and line height for all <p>
elements.
In addition, CSS allows you to add comments to your code using /* */
notation. For example:
/* This is a comment */
h1 {
font-family: Arial, sans-serif;
color: #333; /* This is also a comment */
}
Comments are ignored by the browser and are used to provide notes or explanations to your code.