CSS Syntax Get Certified
What is CSS Syntax?
CSS syntax is the structure used to write CSS rules. A CSS rule tells the browser which HTML element to style and what styles should be applied to it.
Basic CSS Syntax Structure
A CSS rule consists of a selector and a declaration block. The selector targets the HTML element, while the declaration block contains one or more property-value pairs.
What this code does: Shows the general pattern of a CSS rule — a selector, curly braces, and one or more property: value; pairs inside the block.
selector {
property: value;
property: value;
}
Parts of a CSS Rule
- Selector: Specifies the HTML element to style.
- Property: Defines the style feature to change.
- Value: Sets the value for the property.
- Declaration: A property and value pair.
- Declaration Block: A group of declarations enclosed in curly braces.
Example of CSS Syntax
What this code does: Targets every h1 element and sets its text color to blue, font size to 36px, and centers the text on the page.
h1 {
color: blue;
font-size: 36px;
text-align: center;
}
Explanation of the Example
- h1 is the selector.
- color is the property.
- blue is the value assigned to the color property.
- font-size sets the size of the text.
- text-align centers the heading.
Multiple CSS Declarations
A single selector can contain multiple declarations. Each declaration must end with a semicolon (;), except the last declaration where it is optional but recommended.
What this code does: Styles all paragraphs with a dark gray color, 18px font size, comfortable line spacing, and space below each paragraph.
p {
color: #333;
font-size: 18px;
line-height: 1.6;
margin-bottom: 15px;
}
CSS Syntax Rules
- Selectors are followed by curly braces { }.
- Each declaration contains a property and a value separated by a colon :.
- Declarations end with a semicolon ;.
- Property names are case-insensitive, but lowercase is recommended.
- Spaces and indentation improve readability.
Common Syntax Mistakes
Complete CSS Syntax Example
What this code does: Styles the whole page with a light background and Arial font, makes headings blue, and sets paragraph text to dark gray at 18px — a small, complete stylesheet in one block.
body {
font-family: Arial, sans-serif;
background-color: #f8f9fa;
}
h1 {
color: #0d6efd;
}
p {
color: #444;
font-size: 18px;
}
Best Practices
- Always end declarations with a semicolon.
- Use proper indentation for readability.
- Keep selectors simple and meaningful.
- Group related styles together.
- Use consistent formatting throughout your stylesheet.
Web Resources on CSS Syntax
1. MDN Web Docs : Introduction to CSS syntax declarations, rulesets, and statements
2. Dev.to : CSS Syntax and Rules Selectors, Declarations, Properties, and Values
3. MDN Web Docs : CSS syntax
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.