CSS (Cascading Style Sheets) is the language that controls how your web pages look: colors, fonts, spacing, sizes and layout. You write rules that pick out HTML elements with a selector, then set properties such as color or font-size for them. Put those rules in one external stylesheet and you can restyle a whole site by editing a single file.
This guide covers how CSS rules are written, where to put them, the selectors you need, colors and fonts, the box model, and the cascade that decides which rule wins.
How CSS rules work and where they go
A CSS rule has two parts: a selector that chooses which elements to style, and a declaration block in curly braces with one or more property and value pairs.
p { color: #333333; font-size: 18px; }
This says: for every paragraph, make the text dark gray and 18 pixels in size. Each declaration ends with a semicolon. If a property name or value is misspelled, the browser simply ignores that line, which is why typos in CSS fail silently.
You can add comments to explain your styles. Browsers ignore anything between /* and */.
Where to put your CSS
There are three places CSS can live. For most sites, the first is the one you want.
| Method | Example | When to use it |
|---|---|---|
| External stylesheet | <link rel="stylesheet" href="css/style.css"> in the head | Almost always. One file styles every page. |
| Internal style block | <style>h1 { color: navy; }</style> in the head | Styles that only apply to one page |
| Inline style | <p style="color: red"> | Rarely. Quick tests or one-off exceptions. |
An external stylesheet is a plain text file ending in .css. Link it from the <head> of each page, and a change to one color updates the whole site at once. It is also cached by the browser, so later pages load faster.
Selectors: choosing what to style
These selectors cover most everyday needs:
- Element selector:
h2 { }styles every h2. - Class selector:
.highlight { }styles any element withclass="highlight". A class can be used on as many elements as you like, and one element can have several classes. - ID selector:
#signup { }styles the one element withid="signup". IDs should be unique on a page. - Descendant selector:
nav a { }styles links only when they are inside a nav. - Grouping:
h1, h2, h3 { }applies one rule to several selectors. - Pseudo-classes:
a:hover { }styles a link while the mouse is over it.
As a rule of thumb, use classes for styling and keep IDs for anchor links and scripts. Classes are reusable and easier to override later.
Colors, fonts and text
Colors
CSS accepts colors in several formats. All of these are valid:
- Named colors:
color: navy; - Hex codes:
color: #1a73e8; - RGB:
color: rgb(26, 115, 232); - HSL:
color: hsl(214, 82%, 51%);
Use color for text and background-color for the area behind it. Make sure there is enough contrast between the two so text is easy to read for everyone.
Fonts and text
The font-family property sets the typeface. List a few choices, ending with a generic family, so the browser has a fallback:
body { font-family: Arial, Helvetica, sans-serif; line-height: 1.6; }
Other useful text properties include font-size, font-weight (for bold), text-align, and line-height, which sets the space between lines. A line height around 1.5 to 1.7 makes body text much more comfortable to read. To use fonts beyond the ones installed on visitors' computers, see how to use Google Fonts.
The box model: spacing and size
Every element on a page is a rectangular box with four layers, from the inside out:
- Content: the text or image itself, sized with
widthandheight. - Padding: space inside the box, between the content and the border.
- Border: a line around the padding, such as
border: 1px solid #ccc; - Margin: space outside the border that pushes other elements away.
By default, padding and border are added on top of the width you set, which surprises many beginners. Adding this rule near the top of your stylesheet makes sizes behave the way you expect:
*, *::before, *::after { box-sizing: border-box; }
Padding and margin can take one to four values. padding: 20px; sets all sides, padding: 10px 20px; sets top and bottom then left and right, and four values go clockwise from the top. To center a block with a set width, use margin: 0 auto;.
The cascade: which rule wins
When two rules style the same element, the browser has to choose. It looks at three things:
- Specificity. More specific selectors win. An ID beats a class, and a class beats an element selector.
- Order. If specificity is equal, the rule that comes later wins.
- Inheritance. Some properties, like
colorandfont-family, pass down from a parent to the elements inside it. That is why setting a font onbodychanges the whole page.
If a style is not applying, your browser's developer tools will show which rule is overriding it. Try to fix the selector rather than reaching for !important, which makes later changes harder.
A starter stylesheet
Here is a short set of rules that gives a plain page a clean, readable look:
*, *::before, *::after { box-sizing: border-box; }body { margin: 0; font-family: Georgia, serif; line-height: 1.6; color: #222; }main { max-width: 760px; margin: 0 auto; padding: 24px; }h1, h2 { font-family: Arial, sans-serif; line-height: 1.2; }a { color: #1a5fb4; }img { max-width: 100%; height: auto; }
The last rule stops images from spilling out of narrow screens, a first step toward a layout that works on phones.
The easier way: style pages visually, see the CSS
Learning CSS goes faster when you can change a setting and see the result straight away. Apollo Editor, a Windows desktop HTML editor, includes a CSS Style Designer for exactly that.
- Visual controls for fonts, colors, gradients, borders, radius, margin, padding, size, display, shadows and opacity.
- Work with classes, pseudo-classes where supported, and responsive breakpoints.
- A dedicated CSS code editor with validation and error highlighting, so you can read and edit the rules directly.
- Site-wide CSS management, so one stylesheet can style every page.
Changes show in the preview promptly, and the result is ordinary CSS in your own files. Next, try CSS Flexbox vs Grid for layouts, or revisit HTML basics. You can also read more about the features.
Frequently asked questions
Where should I put my CSS?
In an external stylesheet linked from each page's head. One file can then style the whole site, and a change applies everywhere.
What is the CSS box model?
Every element is a box made of content, padding, border and margin. Understanding it makes spacing and sizing predictable.
What is the difference between a class and an id?
A class can be used on many elements. An id should be unique on a page and is often used for anchor links.
