HTML builds the structure, CSS paints the style. Together they are the
fastest way to see your work on screen — you write a line, refresh the browser, and it is there. Every
website you have ever visited starts with these two.
Difficulty: Very beginner friendlyGreat for: Websites, portfolios, school projectsRun with: Just open the .html file in a browser9 chapters · 25+ examples
* { box-sizing: border-box; } /* always start with this */
.box {
width: 300px; /* content */
padding: 20px; /* space inside */
border: 2px solid #2563eb;
margin: 24px auto; /* outer space - auto centres it */
border-radius: 12px;
}
/* spacing shortcuts */
.a { padding: 12px 24px; } /* vertical horizontal */
.b { margin: 8px 0 16px 0; } /* top right bottom left */
.c { padding-top: 10px; padding-left: 6px; }
06
Flexbox & Grid
Two systems that solved layout forever
Flexbox arranges items in one direction — a row or a column. Grid
builds a proper two-dimensional layout with rows and columns. Between them they replace every old
float-and-clear hack.
CSSflex.css
/* a navigation bar: logo left, links right */
.navbar {
display: flex;
align-items: center; /* vertical centring */
justify-content: space-between; /* horizontal split */
gap: 16px; /* space between items */
padding: 12px 20px;
}
/* cards that wrap onto the next line on small screens */
.card-row {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card-row > .card {
flex: 1 1 260px; /* grow | shrink | ideal width */
}
/* perfectly centred content in the middle of a section */
.hero {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 60vh;
text-align: center;
}
CSSgrid.css
/* a responsive card grid with NO media queries */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 20px;
}
/* a fixed two-column page layout */
.page {
display: grid;
grid-template-columns: 260px 1fr; /* sidebar + content */
grid-template-areas:
"sidebar header"
"sidebar main";
gap: 20px;
}
.page > .sidebar { grid-area: sidebar; }
.page > .header { grid-area: header; }
.page > .main { grid-area: main; }
/* centre one thing on screen - the shortest way */
.center { display: grid; place-items: center; }
1
gap is the modern way to space flex and grid items. Do not add margins to every child.
2
justify-content works along the main axis, align-items across it. In a row, justify is horizontal; add flex-direction: column and they swap.
3
repeat(auto-fit, minmax(260px, 1fr)) is the single most useful line of CSS on this page — it makes a grid that reshapes itself for any screen size.
4
place-items: center centres in both directions in one line, and it is the fastest way to centre anything.
07
Responsive design
Most of your visitors are on a phone — build for that first
CSSresponsive.css
/* 1. fluid by default - never a fixed width on a main container */
.container {
width: 100%;
max-width: 1100px;
margin: 0 auto;
padding: 0 20px; /* always a little breathing room */
}
/* 2. images never overflow their box */
img { max-width: 100%; height: auto; display: block; }
/* 3. responsive text with clamp(min, preferred, max) */
h1 { font-size: clamp(1.8rem, 6vw, 3.5rem); }
/* 4. media queries: extra rules for small screens
write desktop first, then adjust downward */
@media (max-width: 900px) {
.page { grid-template-columns: 1fr; }
.page > .sidebar { display: none; }
}
@media (max-width: 600px) {
.navbar { flex-direction: column; align-items: flex-start; }
.card-row > .card { flex: 1 1 100%; }
}
/* 5. respect the visitor's motion preference */
@media (prefers-reduced-motion: reduce) {
* { animation: none !important; transition: none !important; }
}
Test on a real size
1
Open DevTools (F12), press Ctrl+Shift+M and pick a phone. Resize slowly and watch for layout breaks.
2
Horizontal scrolling is the number one mobile bug. Fix it with max-width: 100% on media, overflow-wrap: break-word on long text, and no fixed pixel widths wider than the screen.
3
Body text should be at least 16px, and anything tappable at least 44×44 pixels. Small targets are the most common accessibility failure.
08
Mistakes & how to fix them
Almost every beginner bug is on this list
What you see
Usually caused by
The fix
Nothing has any style
The <link> to your CSS is wrong
Open the Network tab — a 404 means the path is wrong
Page scrolls sideways
A fixed width, or a wide image/table
max-width: 100%; check with outline: 1px solid red;
Margins collapse oddly
Vertical margins of parent and child overlap
Use padding, or a flex/grid parent
Background colour cut off
Parent has no height because children float
Use flexbox or grid instead of float
CSS rule does nothing
Lower specificity, or a stray typo
Check spelling; avoid !important; inspect in DevTools
Text cannot wrap
A long word or URL
overflow-wrap: break-word;
Image is squashed
Forced width and height
Set one dimension and height: auto
Layout jumps while loading
Images without dimensions
Add width/height or an aspect ratio
How to debug any layout problem in 60 seconds
1
Right-click the misbehaving element → Inspect. You now see its exact box, padding and margin in colour.
2
Edit the CSS live in the Styles panel. Nothing is saved, so experiment freely and copy out what works.
3
Temporarily add outline: 1px solid red; to see the real size of a box. Outline never shifts the layout, unlike border.
4
Press Ctrl+F in the Console and search your class name — it tells you if the element exists at all.
09
HTML & CSS cheat sheet
Keep this open while you practise
Do this
Write this
Notes
Start a page
<!DOCTYPE html>
Always the first line
Language
<html lang="en">
Helps screen readers
Page title
<title>…</title>
Shown in the browser tab
Mobile ready
<meta name="viewport" …>
Never remove it
Link CSS
<link rel="stylesheet" href="style.css">
Inside <head>
Heading
<h1> … <h6>
One h1 per page
Paragraph
<p>text</p>
Blocks of text
Link
<a href="…">text</a>
New tab: target="_blank" rel="noopener"
Image
<img src="…" alt="…">
Alt text is required
Bullet list
<ul><li>…</li></ul>
Numbered: <ol>
Divider
<hr>
Void element
Group for styling
<div class="card">
Only when no tag fits
Style an element
class="name"
Reusable — prefer over id
Reset box model
* { box-sizing: border-box; }
Put it first
Text
font-size: 1rem; line-height: 1.6;
Readable defaults
Colour
color: #0f172a;
Text colour (US spelling)
Background
background: #f8fafc;
Not color
Round corners
border-radius: 14px;
Pill shape: 999px
Shadow
box-shadow: 0 4px 14px rgba(0,0,0,.08);
x y blur colour
Space inside
padding: 20px;
Between content and border
Space outside
margin: 20px auto;
auto centres a fixed-width block
Row layout
display: flex; gap: 16px;
Add flex-wrap: wrap
Centre anything
display: grid; place-items: center;
Both directions
Responsive grid
repeat(auto-fit, minmax(260px, 1fr))
No media queries needed
Fluid text
clamp(1.8rem, 6vw, 3.5rem)
Scales with the screen
Small screens
@media (max-width: 600px) { }
Tablet: 900px
Smooth hover
transition: all .2s ease;
Add to the base rule
Practice project: a study-hub card
HTMLcard.html
<section class="grid">
<article class="card">
<span class="badge">Class 10</span>
<h2>Premium Materials</h2>
<p>Hand-picked notes for every subject, updated for 2026.</p>
<a class="btn" href="index.materials.html">Browse notes</a>
</article>
<article class="card">
<span class="badge">Practice</span>
<h2>Mock Tests</h2>
<p>Real exam questions with instant scoring.</p>
<a class="btn" href="index.mock-tests.html">Start a test</a>
</article>
</section>