What is CSS?
Definition & History
CSS (Cascading Style Sheets) is the language used to describe the presentation of a document written in HTML or XML. It defines how elements are rendered on screen, paper, or in other media.
- 1994 – First CSS 1 specification released.
- 1998 – CSS 2 added positioning, media queries, and more.
- 2011 – CSS 3 split into modular “levels” (Flexbox, Grid, Animations, etc.).
Hello World – Minimal HTML + CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>CSS Demo</title> <style> body { font-family: Arial, sans-serif; background: #f4f4f4; margin: 2rem; } h1 { color: #f97316; } </style> </head> <body> <h1>Hello, CSS!</h1> <p>This paragraph inherits the body’s font and background.</p> </body> </html>
Create a file called index.html, paste the snippet above, open it in a browser and
verify the colours and fonts. Experiment by changing the color and
background values.
Including CSS
Three ways to add CSS
- Inline style –
<h1 style="color:red;">…</h1> - Embedded style sheet – inside
<style>tags in the<head>. - External stylesheet – linked with
<link rel="stylesheet" href="styles.css">.
External stylesheet best practice
<head>
<link rel="stylesheet" href="styles.css">
</head>
The external file keeps markup clean and enables caching across pages.
/* Global reset */ *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: 'Open Sans', sans-serif; background: #fafafa; color: #333; line-height: 1.6; }
Create an index.html that links to an external styles.css file. Add a few
HTML elements (headings, paragraphs, a list) and style them globally (fonts, colours, spacing).
Selectors
Basic selector types
- Element selector –
p { … } - Class selector –
.card { … } - ID selector –
#main { … } - Universal selector –
* { … }
Combinators & hierarchy
| Combinator | Example | Meaning |
|---|---|---|
Descendant ( ) |
ul li |
any li inside a ul |
Child (>) |
nav > a |
direct children only |
Adjacent sibling (+) |
h2 + p |
the first p after an h2 |
General sibling (~) |
h2 ~ p |
any p after an h2 |
Attribute selectors
a[href^="https"] { text-decoration: underline; }
input[type="text"] { border: 1px solid #ccc; }
/* Element + class */ nav .nav-item { display: block; padding: 0.5rem 1rem; color: #666; } /* ID selector */ #hero { background: url('hero.jpg') center/cover; color: #fff; text-align: center; padding: 4rem 0; } /* Attribute selector */ a[target="_blank"] { position: relative; } a[target="_blank"]::after { content: "↗"; font-size: 0.8rem; margin-left: 0.2rem; }
Write a stylesheet that styles:
- All
h2elements with a bottom border. - Any link that points to an external site (starts with
http) with a small arrow after it. - Paragraphs that are the first child of a section with a different text colour.
Box Model & Positioning
CSS Box Model
Every element is a rectangular box with four parts (from inside‑out):
- Content – the actual text or image.
- Padding – space inside the border.
- Border – outlines the element.
- Margin – space outside the border.
.box { width: 200px; padding: 20px; border: 2px solid #f97316; margin: 30px; background: #fff; }
Box‑sizing
Set box-sizing: border-box; so width includes padding and border – easier
layout calculations.
Positioning schemes
| Position | Key properties | Behaviour |
|---|---|---|
static |
— | Normal flow (default). |
relative |
top / right / bottom / left |
Shifted relative to its original spot. |
absolute |
top / right / … |
Removed from flow; positioned against nearest positioned ancestor. |
fixed |
top / … |
Anchored to the viewport. |
sticky |
top / … |
Acts like relative until scroll reaches the threshold, then becomes
fixed. |
Example – sticky header
header {
position: sticky;
top: 0;
background: #111;
color: #fff;
padding: 1rem;
z-index: 10;
}
Create a page with a header, a sidebar and a content area. Use position: fixed for the
header,
position: sticky for a sub‑navigation inside the sidebar, and practice padding/margin
changes to see how the box model works.
Flexbox
Why Flexbox?
Flexbox provides a one‑dimensional layout model (row or column) that makes aligning, spacing and reordering items trivial.
Key properties
display: flex;– establishes a flex container.flex-direction–row(default) orcolumn.justify-content– alignment on the main axis (e.g.center,space-between).align-items– alignment on the cross axis.flex,flex-grow,flex-shrink,flex-basis– control item sizing.gap– space between flex items (modern browsers).
.nav { display: flex; justify-content: space-between; align-items: center; background: #111827; padding: 1rem 2rem; gap: 1.5rem; } .nav a { color: #e2e8f0; text-decoration: none; } .nav a:hover { color: #38bdf8; }
Simple responsive navigation bar
<nav class="nav">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
Build a three‑column layout using Flexbox that collapses into a single column on screens narrower
than 600 px (use a media query to change flex-direction).
CSS Grid
Two‑dimensional layout
CSS Grid lets you define rows and columns and place items precisely in that matrix.
Defining a grid container
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr); /* 4 equal columns */
grid-template-rows: auto 200px auto; /* three rows */
gap: 1rem;
}
Placing items
.item‑a { grid-column: 1 / 3; grid-row: 1; }
.item‑b { grid-column: 3 / 5; grid-row: 1 / 3; }
.item‑c { grid-column: 1; grid-row: 2; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 150px; gap: 1rem; } .grid > div { background: #38bdf8; color: #fff; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; } .item‑large { grid-column: 1 / span 2; background: #f97316; }
Responsive grid example
@media (max-width: 800px) {
.grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 500px) {
.grid { grid-template-columns: 1fr; }
}
Design a simple photo‑gallery using CSS Grid: a 4‑column layout on desktop, 2‑column on tablets, and 1‑column on mobile. Add a hover overlay that shows the image caption.
Typography & Fonts
Font‑family stacks
body {
font-family: 'Open Sans', Helvetica, Arial, sans-serif;
}
Web‑safe vs web‑fonts
- Web‑safe – fonts that are pre‑installed on most OSes (Arial, Times, Verdana, …).
- Web‑fonts – delivered via
@font-faceor services like Google Fonts.
Google Fonts example
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
body {
font-family: 'Roboto', sans-serif;
}
Text properties
| Property | Values |
|---|---|
font-size |
16px, 1rem, clamp(1rem,2vw,2rem) |
font-weight |
400, 700, bold |
line-height |
1.5, 150% |
letter-spacing |
0.05em |
text-align |
left, center, justify |
Responsive typography with clamp()
h1 {
font-size: clamp(2rem, 5vw, 4rem);
}
Import a Google Font, set a typographic scale (h1‑h6) using clamp(), and create a
paragraph with a custom line‑height and letter‑spacing. Verify the result at different viewport
widths.
Custom Properties (CSS Variables)
Defining & using variables
:root {
--primary: #f97316;
--bg: #fafafa;
--text: #111827;
}
body {
background: var(--bg);
color: var(--text);
}
a {
color: var(--primary);
}
Scope & inheritance
Variables obey the normal cascade – a variable defined on a parent is available to its children unless overridden.
Theme switching (simple JavaScript example)
/* CSS */
:root {
--bg: #fafafa;
--text: #111;
}
.dark {
--bg: #111;
--text: #fafafa;
}
/* JS */
document.getElementById('theme-btn')
.addEventListener('click', () => document.body.classList.toggle('dark'));
Build a colour‑scheme switcher: define a light and a dark theme using custom properties, add a button that toggles the theme, and ensure all elements (background, text, links, borders) adapt correctly.
Transitions & Animations
CSS Transitions
.btn {
background: var(--primary);
color: #fff;
padding: 0.75rem 1.5rem;
border: none;
cursor: pointer;
transition: background 0.3s ease, transform 0.2s;
}
.btn:hover {
background: #fb923c;
transform: translateY(-2px);
}
Keyframe animations
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.icon {
animation: spin 2s linear infinite;
}
Animating multiple properties
.pulse {
animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.1); opacity: .7; }
100% { transform: scale(1); opacity: 1; }
}
Create a button that on hover fades its background colour, slides upwards, and adds a subtle box‑shadow. Also make an SVG icon that continuously rotates using a keyframe animation.
Responsive Design
Media Queries
@media (max-width: 992px) {
.grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
.grid { grid-template-columns: 1fr; }
}
Mobile‑first approach
Start with the default (mobile) styles, then use min-width breakpoints to add features for
larger screens.
/* Base (mobile) */
.container { padding: 1rem; }
/* Tablet */
@media (min-width: 600px) {
.container { padding: 2rem; }
}
/* Desktop */
@media (min-width: 992px) {
.container { padding: 3rem; }
}
Fluid typography with clamp()
html {
font-size: clamp(14px, 1vw, 18px);
}
Responsive images
<img src="small.jpg"
srcset="small.jpg 480w, medium.jpg 768w, large.jpg 1200w"
sizes="(max-width: 600px) 480px,
(max-width: 900px) 768px,
1200px"
alt="Responsive example">
Turn the three‑column layout from Lecture 5 into a fully responsive layout that collapses to two columns at 800 px and to a single column at 500 px. Add a responsive navigation bar that switches to a hamburger menu (you can reuse the existing sidebar toggle).
SCSS & Pre‑processors
Why preprocess?
- Variables (
$primary: #f97316;) - Nesting – clearer hierarchy.
- Mixins – reusable blocks of CSS.
- Functions – compute values.
- Partial files & imports for modular code.
Variables & nesting example
$primary: #f97316;
$spacing: 1rem;
body {
font-family: 'Open Sans', sans-serif;
color: #111;
a {
color: $primary;
&:hover { color: darken($primary, 10%); }
}
.section {
padding: $spacing * 2;
}
}
Mixin for clearfix
@mixin clearfix {
&::after {
content: "";
display: table;
clear: both;
}
}
.container { @include clearfix; }
Using partials
// _variables.scss
$primary: #f97316;
// main.scss
@import 'variables';
@import 'components/buttons';
Set up a simple SCSS project (you can use an online compiler like SassMeister). Create a _buttons.scss partial with a
mixin that generates primary, secondary and disabled button styles. Import the partial into a main
stylesheet and demonstrate the three button variants in HTML.
Capstone Project – Responsive Landing Page
Build a **single‑page landing site** that showcases everything you’ve learned:
- Semantic HTML structure (header, hero, features, testimonial, footer).
- External stylesheet using CSS variables for a colour scheme.
- Flexbox for the navigation bar and testimonial carousel.
- CSS Grid for the feature cards.
- Responsive typography with
clamp(). - Transition effects on buttons and links.
- Keyframe animation for a decorative element.
- Responsive media queries for mobile, tablet, desktop.
- Optional SCSS source files (if you prefer a pre‑processor).
Project skeleton
index.html
styles/
│─ main.css
│─ components/
│ └─ _buttons.css
└─ utils/
└─ _variables.css
assets/
├─ img/
└─ fonts/
Starter HTML (feel free to expand)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Creative Agency</title> <link rel="stylesheet" href="styles/main.css"> </head> <body> <!-- NAVIGATION --> <header class="site-header"> <div class="logo">Creative</div> <nav class="nav"> <a href="#">Home</a> <a href="#services">Services</a> <a href="#contact">Contact</a> </nav> </header> <!-- HERO SECTION --> <section class="hero"> <h1>Design <span class="highlight">Beautiful</span> Experiences</h1> <p>We craft digital products that delight users and drive results.</p> <a class="btn btn-primary" href="#">Get Started</a> </section> <!-- FEATURES (GRID) --> <section id="services" class="features"> <h2>What We Offer</h2> <div class="grid"> <article class="card"> <h3>UX Design</h3> <p>Research‑driven design that puts users first.</p> </article> <article class="card"> <h3>Web Development</h3> <p>Fast, responsive, and SEO‑friendly sites.</p> </article> <article class="card"> <h3>Brand Strategy</h3> <p>Building memorable visual identities.</p> </article> </div> </section> <!-- FOOTER --> <footer class="site-footer"> <p>© 2026 Creative Agency. All rights reserved.</p> </footer> </body> </html>
What you’ll prove
- Use of CSS custom properties for theming.
- Flexbox navigation and Grid feature cards.
- Responsive typography and layout with media queries.
- Transitions on interactive elements and a keyframe animation.
- Clean, semantic HTML markup.
- (Optional) SCSS file structure and compilation.
- Explore CSS Houdini APIs.
- Learn about CSS-in-JS libraries (styled‑components, Emotion).
- Integrate your CSS into a modern front‑end framework (React, Vue, Svelte).
- Get familiar with build tools (PostCSS, autoprefixer, minifiers).
Extend the landing page with a sticky navigation bar, a dark‑mode toggle (using custom properties), and a testimonial carousel built with CSS scroll‑snap. Validate the final HTML with validator.w3.org – you should have zero errors.