CSS Syntax
1. What is CSS Syntax?
CSS syntax defines how style rules are written.
Every CSS rule tells the browser:
- Which HTML element should be styled.
- Which property should be changed.
- What value should be applied.
h1{
color: blue;
}
2. Parts of CSS Syntax
| Part | Description |
|---|---|
| Selector | Selects the HTML element to style. |
| Property | Defines what style will be changed. |
| Value | Specifies the value for the property. |
| Declaration | A property and value together. |
p{
color:red;
}
3. CSS Selector
A selector tells the browser which HTML elements should receive the style.
| Selector | Selects |
|---|---|
| h1 | All h1 elements |
| p | All paragraphs |
| img | All images |
| body | The entire webpage |
h2{
color:green;
}
4. CSS Property
A property specifies which style you want to change.
| Property | Purpose |
|---|---|
| color | Changes text color. |
| background-color | Changes background color. |
| font-size | Changes text size. |
| text-align | Aligns text. |
| border | Adds a border. |
5. CSS Value
A value tells the browser exactly how the property should be applied.
| Property | Value | Result |
|---|---|---|
| color | blue | Blue text |
| font-size | 24px | 24-pixel text |
| text-align | center | Centered text |
| background-color | yellow | Yellow background |
p{
font-size:20px;
color:blue;
}
6. CSS Declaration Block
A Declaration Block is the part enclosed inside curly braces { }. It contains one or more CSS declarations.
h1{
color: blue;
font-size: 36px;
}
| Declaration | Meaning |
|---|---|
| color: blue; | Changes the text color to blue. |
| font-size: 36px; | Sets the font size to 36 pixels. |
7. Multiple CSS Properties
You can apply multiple styles to the same HTML element.
p{
color: navy;
font-size: 20px;
font-family: Arial;
text-align: center;
background-color: #f2f2f2;
}
Output
- Text color becomes navy.
- Font size becomes 20px.
- Font family changes to Arial.
- Paragraph becomes centered.
- Background becomes light gray.
8. CSS Comments
Comments are used to explain CSS code. Browsers ignore comments.
/* This is a CSS comment */
h1{
color: red;
}
Uses of Comments
- Explain CSS code.
- Improve readability.
- Temporarily disable code.
- Organize large stylesheets.
9. Writing Clean CSS
Good Example
body{
background:#ffffff;
}
h1{
color:blue;
text-align:center;
}
p{
font-size:18px;
}
Tips for Clean CSS
- Use proper indentation.
- Keep one declaration per line.
- Use meaningful class names.
- Group related properties together.
- Remove unnecessary code.
- Use comments where required.
10. Practical CSS Syntax Examples
Example 1 : Change Heading Color
h1{
color:green;
}
Example 2 : Style a Paragraph
p{
color:#444;
font-size:18px;
}
Example 3 : Style the Web Page
body{
background:#f8f9fa;
font-family:Arial;
}
Example 4 : Style a Button
button{
background:#007bff;
color:white;
padding:10px 20px;
border:none;
}
11. Browser Support
CSS syntax follows a standard supported by all modern web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Excellent |
| Mozilla Firefox | ✔ Excellent |
| Microsoft Edge | ✔ Excellent |
| Safari | ✔ Excellent |
| Opera | ✔ Excellent |
12. CSS Syntax Best Practices
- Write one property on each line.
- Always end declarations with a semicolon (;).
- Use proper indentation.
- Keep selectors simple and meaningful.
- Group related properties together.
- Use external CSS for large websites.
- Add comments to organize your stylesheet.
- Remove unused CSS rules.
- Validate your CSS before publishing.
- Use lowercase property names.
13. Common CSS Syntax Errors
| Error | Correct Way |
|---|---|
| Missing semicolon | color: blue; |
| Missing colon | font-size:20px; |
| Missing curly braces | { ... } |
| Incorrect property name | background-color |
| Incorrect selector | Use valid HTML selectors. |
❌ Wrong
h1
color:red
✔ Correct
h1{
color:red;
}
14. CSS Syntax Interview Questions
- What is CSS syntax?
- What are the parts of a CSS rule?
- What is a selector?
- What is a declaration block?
- What is a CSS property?
- What is a CSS value?
- Why are curly braces used in CSS?
- Why should every declaration end with a semicolon?
- Can one selector contain multiple properties?
- Which type of CSS is recommended for large websites?
15. Lesson Summary
In this lesson, you learned:
- ✔ What CSS syntax is.
- ✔ Selector, Property and Value.
- ✔ Declaration Block.
- ✔ Multiple CSS Properties.
- ✔ CSS Comments.
- ✔ Writing clean CSS.
- ✔ Practical syntax examples.
- ✔ Browser support.
- ✔ Best practices.
- ✔ Common syntax mistakes.
Quick Quiz
Which symbol starts a CSS declaration block?