? CSS Syntax
? Quick Overview
CSS Syntax defines the structure used to write styles for HTML elements. Each CSS rule is made of a selector and a declaration block. Inside the block are one or more property–value pairs that control how elements appear on screen.
✅ Key Concepts
- Selector → Targets HTML elements
- Property → Defines what to style
- Value → Specifies how to style it
- { } → Wrap declarations
- : → Separates property & value
- ; → Ends each rule
? Syntax / Theory
The standard rule-set format:
? View Code Example
selector {
property1: value1;
property2: value2;
}
? Code Example
Styling an <h2> element using proper CSS syntax:
? View Code Example
h2 {
color: green;
font-size: 24px;
text-align: center;
}
? Live Output
This heading is styled using proper CSS syntax.
? Explanation
- h2 → Selector
- color, font-size, text-align → Properties
- green, 24px, center → Values
- ; → Ends instructions
- { } → Groups style rules
? More Example
? View Code Example
p {
color: blue;
background-color: #f0f0f0;
padding: 10px;
}
This paragraph uses correct CSS syntax for multiple properties.
? Tips & Best Practices
- Always end declarations with semicolons.
- Keep formatting clean and readable.
- Use meaningful selectors.
- Test changes using browser DevTools.
- Group related properties together.
? Try It Yourself
- Create a style for
div with background color.
- Change font size and text color of headings.
- Add padding and borders to paragraphs.
- Style a list using CSS syntax.