CSS Syntax
Learn how to write CSS rules and understand the basic syntax
📝 CSS Rule Structure
A CSS rule consists of a selector and a declaration block. The selector points to the HTML element, and the declaration block contains the styling properties.
/* CSS Rule Structure */
selector {
property: value;
property: value;
}
Anatomy of a CSS Rule
Selector
Points to the HTML element to style
h1 { }
Declaration Block
Contains styling rules inside { }
{ color: blue; }
Property
The style attribute you want to change
color:
Value
The setting for the property
blue;
🔹 Complete CSS Rule Example
A complete CSS rule consists of a selector, declaration block, properties, and values, forming the fundamental building block of web styling. Example: p { color: #333; font-size: 16px; margin-bottom: 1em; } Here, p is the selector targeting all paragraphs. The declaration block { ... } contains property-value pairs defining style instructions. Each declaration ends with a semicolon. Well-formed rules follow consistent formatting, use meaningful values, and are grouped logically in the stylesheet. Mastering rule syntax ensures error-free CSS that browsers can parse efficiently, leading to faster rendering, fewer layout shifts, and a more stable user experience—all critical for achieving high performance scores and favorable search engine rankings.
/* Complete CSS Rule */
h1 {
color: blue;
font-size: 24px;
text-align: center;
}
Breaking it down:
- h1 - Selector (targets all h1 elements)
- { } - Declaration block (contains all the styles)
- color - Property (what we want to change)
- blue - Value (how we want to change it)
- ; - Semicolon (ends each declaration)
Result:
This heading is styled!
🔹 CSS Syntax Rules
CSS syntax rules govern how stylesheets are written and interpreted by browsers, ensuring predictable rendering. Key rules include: enclosing declarations in curly braces {}, ending each declaration with a semicolon, separating property and value with a colon, and using valid property/value pairs. Selectors must correctly target existing or anticipated HTML. Comments use /* ... */. Adhering to syntax prevents silent failures and cross-browser inconsistencies. Proper syntax, combined with consistent formatting and naming conventions, creates maintainable, scalable CSS. Clean, valid CSS improves parsing speed, reduces rendering-blocking resources, and enhances site performance. This technical excellence directly supports SEO by improving page speed metrics and ensuring reliable delivery of styled content to users and crawlers alike.
🔸 1. Semicolons are Required
/* ✅ Correct */
p {
color: red;
font-size: 16px;
}
/* ❌ Wrong - Missing semicolons */
p {
color: red
font-size: 16px
}
🔸 2. Curly Braces Must Match
/* ✅ Correct */
.my-class {
background: yellow;
}
/* ❌ Wrong - Missing closing brace */
.my-class {
background: yellow;
🔸 3. Property Names are Case-Sensitive
/* ✅ Correct */
div {
background-color: lightblue;
}
/* ❌ Wrong - Incorrect case */
div {
Background-Color: lightblue;
}
🔸 4. Quotes for Multi-Word Values
/* ✅ Correct */
body {
font-family: "Times New Roman", serif;
}
/* ❌ Wrong - Missing quotes */
body {
font-family: Times New Roman, serif;
}
🔹 Multiple Properties Example
Applying multiple properties within a single rule allows comprehensive styling of an element, covering typography, layout, color, and effects. A robust example: .card { background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 1.5rem; margin: 1rem 0; font-family: sans-serif; } This defines a complete visual component. Grouping related properties (like box-model, then typography) improves readability. Efficient property usage minimizes redundancy and override conflicts. Well-constructed multi-property rules create cohesive, reusable components that enhance design consistency and user experience. This approach leads to smaller, more efficient stylesheets, faster load times, and improved maintainability—all contributing to better Core Web Vitals scores and stronger SEO performance through optimized site speed and usability.
/* Styling a button with multiple properties */
.my-button {
background-color: #4CAF50; /* Green background */
color: white; /* White text */
padding: 15px 32px; /* Space inside button */
text-align: center; /* Center the text */
font-size: 16px; /* Text size */
border: none; /* Remove border */
border-radius: 4px; /* Rounded corners */
cursor: pointer; /* Hand cursor on hover */
}
<button class="my-button">Click Me!</button>
Result:
🔹 CSS Comments
CSS comments document the purpose, structure, and logic of stylesheets, serving as essential communication tools for developers and maintainers. Written as /* This is a comment */, they can be single-line or multi-line. Effective comments explain why certain styles exist, mark sections (like "Header Styles"), note browser hacks, or flag TODOs. They do not affect rendering. Well-commented CSS accelerates onboarding, simplifies debugging, and ensures long-term maintainability, especially in team environments. While comments add to file size, minification tools strip them in production. Organized, commented code promotes best practices, reduces technical debt, and supports scalable architecture. This professional approach indirectly benefits SEO by facilitating faster updates, fewer bugs, and a more stable site.
/* This is a single-line comment */
/*
This is a
multi-line comment
that spans several lines
*/
/* Header Styles */
h1 {
color: navy; /* Dark blue color */
font-size: 28px; /* Large text size */
}
/* Button Styles */
.btn {
background: #007cba; /* Brand blue color */
color: white;
}
🔹 Practice Exercise
Try writing this CSS rule yourself:
Task: Style a paragraph
Create a CSS rule that:
- Targets all paragraph elements
- Sets the text color to dark gray (#333)
- Sets the font size to 18px
- Sets the line height to 1.5
/* Your answer should look like this: */
p {
color: #333;
font-size: 18px;
line-height: 1.5;
}
Result:
This paragraph is styled with the CSS rule above. Notice the dark gray color, larger font size, and increased line spacing!