CSS basics: colours, spacing and layout
Style a page with colours, fonts, spacing, rounded corners and hover effects.
CSS (Cascading Style Sheets) controls how HTML looks. A rule has a selector and some declarations.
The box model
Every element is a box: content, then padding (space inside), border, and margin (space outside). Most layout problems come from mixing these up.
Layout with flexbox and grid
Flexbox lines items up in one direction. Grid arranges rows and columns. The auto-fill / minmax pattern makes a layout that adapts to any screen without extra code.
Try it
Open the CSS card example and change the colours, the radius and the shadow.
Common mistakes and practice
- Forgetting the semicolon at the end of a declaration. The next rule can silently stop working.
- Writing very specific selectors such as
div.card ul li a. They are hard to override later; prefer a simple class. - Fixing every size in
px. Relative units such asremand percentages adapt better to different screens and user settings.
Practice: Style a simple card: add padding, a border radius, a soft shadow and a hover effect, then make two of them sit side by side with flexbox and stack on a narrow screen.
Practice ideas after this lesson
CSS is learnt by changing things and watching the page respond. Pick a plain page and try to make it look like a design you like, one rule at a time.
Use your browser developer tools to inspect any website and see which rules style an element. It is a great way to learn how professionals build layouts.
Practical tips
- Make a card with padding, a border radius and a shadow.
- Build a two-column layout with flexbox, then make it one column on a phone.
- Try a light and a dark colour scheme with CSS variables.
- Always check your design at phone width, because most visitors use phones.