CSS (Cascading Style Sheets) is the language used to style HTML documents. Understanding its CSS Syntax is crucial for designing visually appealing and user-friendly web pages.
1. Basic CSS Syntax
CSS follows a simple rule-based syntax consisting of selectors, properties, and values. Each rule set is enclosed within curly braces {}
.

Example:
selector {
property: value;
}
CSSExample with Styling:
p {
color: blue;
font-size: 16px;
}
CSSBreakdown:
p
→ Selector (targets<p>
elements)color
→ Property (defines text color)blue
→ Value (sets text color to blue)font-size
→ Another property (sets font size)16px
→ Value (sets text size to 16 pixels)
2. Types of CSS Selectors
Selectors define which HTML elements the styles apply to.
a. Element Selector
Targets all instances of an HTML element.
h1 {
color: red;
}
CSSb. Class Selector (.)
Targets elements with a specific class.
.my-class {
background-color: yellow;
}
CSSc. ID Selector (#)
Targets a unique element with a specific ID.
#my-id {
font-weight: bold;
}
CSSd. Group Selector (,)
Targets multiple elements at once.
h1, h2, h3 {
font-family: Arial, sans-serif;
}
CSSe. Descendant Selector (whitespace)
Targets elements nested within another element.
div p {
color: gray;
}
CSS3. CSS Comments
Comments are used to explain code and are ignored by browsers.
/* This is a comment */
CSS4. CSS Units
Values in CSS can be expressed in different units:
- Pixels (
px
): Fixed size (e.g.,16px
) - Relative (
em
,%
,rem
): Responsive and scalable - Viewport (
vw
,vh
): Based on screen size
5. CSS Import Methods
a. Inline CSS (Applied directly to an element)
<p style="color: blue; font-size: 16px;">Hello World</p>
HTMLb. Internal CSS (Inside <style> tags in <head>)
<style>
p {
color: blue;
}
</style>
HTMLc. External CSS (Linked via a separate file)
<link rel="stylesheet" href="styles.css">
HTML6. CSS Specificity & Importance
When multiple rules apply, CSS follows specificity:
- Inline styles (
style=""
) – Highest priority - ID Selectors (
#id
) – Higher specificity - Class Selectors (
.class
) – Moderate specificity - Element Selectors (
h1, p
) – Lower specificity
Use !important
to force a style (use sparingly):
p {
color: red !important;
}
CSSConclusion
Mastering CSS syntax is essential for web design. By understanding selectors, properties, values, and specificity, you can create stunning and responsive websites. Experiment with different CSS rules to improve your styling skills!