CSS (Cascading Style Sheets) styles web pages. Understanding CSS syntax is crucial for effective styling. This page covers the basics: selectors, properties, values, and more.
A CSS rule consists of a selector and a declaration block:
/* CSS Rule Set */
selector {
property: value;
}
Selectors target HTML elements to apply styles:
p
, h1
)..class-name
).#id-name
).[type="text"]
).h1, h2
).CSS properties define styles; values specify settings. For example:
/* Example CSS Rule */
p {
color: blue; /* Property: color */
font-size: 16px; /* Property: font-size */
margin: 10px; /* Property: margin */
}
Use comments to annotate your code. They're ignored by browsers:
/* Single-line comment */
/*
Multi-line comment
spanning multiple lines.
*/
Common units in CSS:
font-size: 16px;
).width: 50%;
).font-size: 1.5em;
).CSS properties can be inherited from parent to child elements. Specificity determines which styles are applied when multiple rules match an element.