CSS (Cascading Style Sheets) syntax is the foundation of how we write styling rules for web pages. It tells the browser two important things:
π Which HTML element to style
π What style to apply
A CSS rule has two main parts:
selector {
property: value;
}
p selects all paragraph elements (<p>).{ }.Each declaration has:
color, font-size).blue, 18px).Important: Each declaration ends with a semicolon (;).
p {
color: blue;
font-size: 18px;
}
<p> elements to have blue text and font size of 18 pixels.Selectors choose which HTML elements to style.
tagname {
property: value;
}
h1 {
color: green;
}
<h1> headings green.p {
text-align: center;
}
body {
background-color: lightyellow;
}
<p> tags.The block inside { } contains style rules.
Structure:
selector {
property: value;
property: value;
}
h2 {
color: red;
font-family: Arial;
font-size: 20px;
}
| Part | Meaning |
|---|---|
color | Property |
red | Value |
font-family | Property |
Arial | Value |
font-size | Property |
20px | Value |
Tip: Multiple declarations are separated by semicolons inside the declaration block.
Every declaration is a pair of property and value.
color, margin, border-style, background-color.blue, 10px, solid, yellow.p {
color: blue;
}
colorbluep {
color: red;
font-size: 16px;
text-align: justify;
}
Observation: You can change several style features at once inside one rule.
HTML:
<p>This is a paragraph.</p>
CSS:
p {
color: blue;
background-color: yellow;
font-size: 18px;
}
p β selects all paragraph tags.color: blue; β Text color becomes blue.background-color: yellow; β Background of the paragraph becomes yellow.font-size: 18px; β Text size increases to 18 pixels.This shows how multiple properties come together to style an element completely.
Correct CSS syntax is very important because it ensures:
Remember to always use:
{ } in pairs.; after every declaration except the last one inside a block (though it's good practice to add it).Even one small syntax error can prevent the style from working.
See how CSS styling changes the look of a web page.
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: purple;
font-size: 20px;
}
</style>
</head>
<body>
<p>Hello, CSS World!</p>
</body>
</html>
test.html.test.html in your browser.<p> appears in purple color, size 20px.color or font-size in the CSS block (inside <style>) and refreshing browser will reflect changes.Try changing:
color: orange;font-size: 24px;and observe the new styles.
This activity helps you see CSS syntax in action!
Scenario: You want to style all headings (<h1>) on your webpage to be blue and centered.
h1 {
color: blue;
text-align: center;
}
Scenario: You forgot a semicolon between two declarations in a CSS rule.
Scenario: You want the background color of the entire webpage to be light green.
body selector and background-color property:body {
background-color: lightgreen;
}
Scenario: You want to apply styling only to paragraphs, but notice itβs affecting other elements too.
* (all elements) or specified a wrong tag (e.g., div instead of p).Scenario: You want your text inside paragraphs to be red and font size 16px.
p {
color: red;
font-size: 16px;
}
Happy styling and keep experimenting with CSS!