Styling Web Pages with CSS
Learn CSS to style web pages: selectors, colors, fonts, spacing with the box model, and layout basics. Make plain HTML look great with runnable code and a quiz.
Key takeaways
- CSS controls how HTML looks: colors, fonts, spacing, and layout
- A rule is a selector plus declarations of property: value;
- Selectors target elements by tag, class (.name), or id (#name)
- The box model surrounds content with padding, border, and margin
- Linking an external .css file keeps style separate from content
From plain to polished
If you've built a page with HTML, you've seen that raw HTML looks plain: black text, blue links, times-style fonts, no spacing. CSS β Cascading Style Sheets β is the language that changes all of that. HTML decides what is on the page; CSS decides how it looks.
A CSS rule
CSS is made of rules. Each rule has a selector (what to style) and one or more declarations (how to style it):
p {
color: navy;
font-size: 18px;
}
Breaking it down:
pis the selector β here it targets every<p>(paragraph) element.- Inside the
{ }are declarations. - Each declaration is a property and a value separated by a colon and ending in a semicolon:
color: navy;.
This rule makes all paragraphs navy and 18 pixels tall.
Three ways to select
You won't always want to style every paragraph the same way. CSS gives you three common selectors:
/* By tag name: every <h1> */
h1 {
color: darkgreen;
}
/* By class: any element with class="highlight" */
.highlight {
background-color: yellow;
}
/* By id: the single element with id="header" */
#header {
font-size: 32px;
}
A class (selected with a dot .) can be reused on many elements. An id (selected with a hash #) should be unique β only one element per page. In the HTML they look like this:
<h1 id="header">My Blog</h1>
<p class="highlight">Read this part carefully.</p>
<p>A normal paragraph.</p>
Connecting CSS to HTML
The cleanest way to add CSS is in a separate file. Save your styles as style.css, then link it inside the <head> of your HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 id="header">My Blog</h1>
<p class="highlight">Welcome!</p>
</body>
</html>
The <link rel="stylesheet" href="style.css"> tag tells the browser to load and apply that stylesheet. One CSS file can style an entire website.
Colors and fonts
You can name colors directly (red, navy, teal) or use hex codes for precise shades. A hex code starts with # followed by six characters for red, green, and blue:
body {
background-color: #f4f4f4; /* light grey */
color: #222222; /* near-black text */
font-family: Arial, sans-serif;
line-height: 1.6;
}
font-family lists fonts in order of preference β the browser uses the first one available, falling back to a generic sans-serif. line-height: 1.6 adds breathing room between lines so text is easier to read.
The box model
Every element on a page is a rectangular box, and CSS lets you control the space around it. From the inside out, the box model has:
- content β the text or image itself
- padding β space inside the border, around the content
- border β a line around the padding
- margin β space outside the border, separating it from other elements
.card {
padding: 16px;
border: 2px solid #cccccc;
margin: 20px;
border-radius: 8px;
}
This rule adds inner spacing, a soft grey border with rounded corners, and outer spacing so cards don't touch each other. Understanding padding versus margin is one of the biggest "aha" moments in CSS: padding pushes the border outward; margin pushes other elements away.
A little layout
You can size and centre a block of content using width and margin: auto:
.container {
width: 600px;
margin: 0 auto; /* 0 top/bottom, auto left/right = centred */
padding: 20px;
}
margin: 0 auto is a classic trick: the auto left and right margins split the leftover space evenly, centring the box horizontally.
Putting it all together
Here's a small style.css that would make a basic page look clean and modern:
body {
font-family: Arial, sans-serif;
background-color: #fafafa;
color: #222;
line-height: 1.6;
margin: 0;
}
.container {
width: 600px;
margin: 0 auto;
padding: 24px;
}
h1 {
color: #1a5276;
}
.highlight {
background-color: #fff3b0;
padding: 4px 8px;
border-radius: 4px;
}
Wrap your page content in a <div class="container"> ... </div> and watch a plain page turn into something that looks designed.
What comes next
CSS handles the look. To make a page do things β respond to clicks, change content, react to the user β you'll add behaviour with JavaScript. Together, HTML, CSS, and JavaScript are the three core languages of every web page.
Practice challenges
- Style every
<h2>with a colour of your choice and a larger font size. - Create a
.cardclass with padding, a border, and a margin, and apply it to a<div>. - Centre a 500px-wide container on the page and give the body a light background colour.
Quick quiz
Test yourself and earn XP
What is the correct CSS to make all paragraphs blue?
A CSS rule is a selector (p) followed by declarations in braces: color: blue;.
Which selector targets an element with class="note"?
A dot before the name selects by class, so .note matches class="note".
In the box model, which space is INSIDE the border, around the content?
Padding is the space between the content and the border. Margin is outside the border.
What does this declaration do? `font-size: 20px;`
font-size sets the height of the text; px means pixels.
How do you link an external stylesheet named style.css in HTML?
A <link> tag with rel="stylesheet" and href pointing to the file loads external CSS.
FAQ
HTML provides the structure and content of a page β headings, paragraphs, images. CSS controls how that content looks β colors, fonts, spacing, and layout. HTML is the skeleton; CSS is the styling.
For real projects, put CSS in a separate .css file and link it with a <link> tag. This keeps content and style apart and lets one stylesheet format many pages. For quick tests you can use a <style> block in the page's head.
Keep exploring
More in Coding