CSS (Cascading Style Sheets) is a style sheet language used for describing the presentation of a document written in HTML. CSS syntax is easy to learn and understand, but it's essential to follow the correct syntax to avoid errors and ensure your styles are applied as intended.
CSS rules are made up of selectors and declaration blocks:
selector {
property: value;
}
The selector points to the HTML element you want to style. The declaration block contains one or more declarations separated by semicolons. Each declaration includes a CSS property name and a value, separated by a colon.
Here’s an example of CSS syntax in action:
p {
color: blue;
font-size: 14px;
}
This text is blue and has a font size of 14px.
CSS allows you to add comments within your stylesheets. Comments are ignored by the browser and are useful for explaining your code:
/* This is a comment */
p {
color: red;
}
This text is red.
CSS selectors are used to "select" the HTML elements you want to style. There are different types of selectors:
p
for paragraphs)..classname
).#idname
).You can group multiple selectors to apply the same styles to multiple elements:
h1, h2, p {
color: green;
}
This is a paragraph.