CSS controls how a web page looks: its colours, text, spacing and layout. This CSS tutorial teaches the parts you need to style a real page, in the order you need them. You should already know basic tags from the HTML tutorial.
CSS stands for Cascading Style Sheets. You choose an element, then state how it should look. The browser applies your rules to the page.
CSS tutorial: write your first rule
Make two files in one folder. The first is index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS practice</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>My courses</h1>
<article class="card">
<h2>Java</h2>
<p>Start with variables and loops.</p>
</article>
</body>
</html>
The second is style.css:
h1 {
color: darkblue;
text-align: center;
}
.card {
border: 1px solid #cccccc;
padding: 16px;
}
Open the page. The heading is dark blue and centred. The card has a grey border and space inside it.
In the first rule, h1 is the selector. color is a property and darkblue is its value. Each line inside the braces is a declaration and ends with a semicolon.
There are three ways to add CSS: a separate file, a <style> tag, or a style attribute on one element. Use a separate file for most work, because many pages can share it.
Selectors: choose what to style
| Selector | Example | What it selects |
|---|---|---|
| Element | p |
Every paragraph |
| Class | .card |
Every element with that class |
| ID | #menu |
The element with that ID |
| Descendant | .card p |
Paragraphs inside a card |
| Child | nav > a |
Links directly inside nav |
| State | a:hover |
A link under the pointer |
| Group | h1, h2 |
Both heading types |
Use classes for most styles. You can use the same class on many elements, and one element can have several classes.
What happens when two normal rules in your file disagree? The browser compares specificity, which means how exact the selector is. An ID beats a class. A class beats an element name. If both rules are equally specific, the later one wins.
p { color: black; }
.note { color: green; }
/* <p class="note"> is green because a class beats an element */
Some values, such as text colour and font, are inherited. A child uses its parent's value unless a rule sets its own. Margin and border are not inherited.
Common mistake: Adding
!importanteach time a style does not work hides the real problem. Open browser DevTools, find the crossed-out rule and see which selector won.
The box model and units
Every element is a box. From the inside to the outside, the parts are content, padding, border and margin.
- Padding is space inside the border.
- Border is the line around the padding.
- Margin is space outside the border.
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
}
By default, width sets only the content. The visible box is 200 + 40 + 10 = 250px wide. Padding and border are added on both sides.
Put this rule at the top of your file to change that:
*, *::before, *::after {
box-sizing: border-box;
}
Now the same box is 200px wide, including padding and border. This makes sizes much easier to plan.
| Unit | Its size depends on | Good use |
|---|---|---|
px |
A fixed CSS pixel | Thin borders |
rem |
Root font size | Text and spacing |
% |
Usually the parent | Fluid widths |
vw |
Screen width | Wide sections |
fr |
Free grid space | Grid columns |
Make a row with Flexbox
Flexbox arranges items mainly in one direction: a row or a column. It suits a menu, a row of buttons or centred content.
.menu {
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
}
The rule goes on the parent. justify-content places items along the main direction, which is a row by default. align-items places them across it. gap adds space between items.
This page only introduces the idea. The CSS Flexbox guide covers growing, shrinking and wrapping in detail.
Learn CSS Grid with a card layout
Grid handles rows and columns together. To learn CSS Grid, start with three equal columns:
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
1fr means one share of the free space. Three children now sit beside each other. A fourth child moves to the next row.
You can let Grid choose the number of columns:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
}
Each column is at least 220px wide. Extra space is shared equally. In a container 500px wide with a 20px gap, two columns fit: 220 + 20 + 220 = 460px. Three would need 700px, so they do not fit.
You can also name areas of a page:
.page {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"menu main"
"footer footer";
}
.page > header { grid-area: header; }
.page > nav { grid-area: menu; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
The text looks like a map of the page. The header covers both columns because its name appears twice.
Interview note: Choose Flexbox when you mainly arrange one row or column. Choose Grid when rows and columns must line up. One page can use both.
Build a responsive page
A responsive page works on small and large screens. Write the phone layout first, then add a media query for wider screens.
body {
margin: 0;
padding: 16px;
font-family: Arial, sans-serif;
line-height: 1.6;
}
img {
max-width: 100%;
height: auto;
}
.columns {
display: grid;
grid-template-columns: 1fr;
gap: 16px;
}
@media (min-width: 768px) {
.columns {
grid-template-columns: 200px 1fr;
}
}
Below 768px there is one column. At 768px and above there are two. Do not fix the body width at a desktop size, because phone users would need to scroll sideways.
Common mistake: If your media query seems to do nothing on a phone, check that the HTML has the viewport meta tag. The first example on this page includes it.
Add small touches after the layout works:
:root {
--brand: #154a8a;
}
.button {
background: var(--brand);
color: white;
padding: 12px 20px;
border-radius: 8px;
transition: background 0.2s;
}
.button:hover,
.button:focus-visible {
background: #0c2d55;
}
--brand is a custom property. Change it once and every rule that uses it updates. Style keyboard focus as well as mouse hover, so people without a mouse can see where they are.
Learn CSS with games and online tools
Games are good for repeating one skill. They do not replace building a page. If you want to learn CSS with games, try these in order:
| Game | What it trains |
|---|---|
| CSS Diner | Selectors |
| Flexbox Froggy | Flexbox alignment |
| Grid Garden | Grid rows and columns |
You can learn CSS online without installing anything. An online editor such as CodePen shows the result beside your code. MDN Web Docs is a reliable place to look up a property. Use your own files too, because work projects use folders and linked style sheets.
After each game, close it and rebuild one idea from memory. For example, after Grid Garden make a photo gallery. That shows you whether you understood the rule or only solved the level.
Practice plan for beginners
This short plan turns a CSS tutorial for beginners into real practice:
- Days 1 and 2: style text, links and buttons. Try each selector.
- Days 3 and 4: build a profile card. Check its box size in DevTools.
- Days 5 and 6: make a navigation bar with Flexbox.
- Days 7 to 9: make a course gallery with Grid.
- Days 10 to 12: build a full responsive page for phone and desktop.
- Days 13 and 14: rebuild it without looking, then improve colour contrast and focus styles.
The days are a guide. Move on when you can make the result without copying.
As you learn CSS, change one value at a time in DevTools and predict what will happen. Then check your knowledge with the CSS interview questions. When your layouts work well, add behaviour with the JavaScript fundamentals tutorial, or see how front-end skills fit the MERN stack path.
Frequently Asked Questions
Can I learn CSS without HTML?
What should I learn first in CSS?
Should I learn Flexbox or Grid first?
Can I learn CSS only by playing games?
Why is my CSS not working?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

