Book 3 — HTML, CSS Basics

Python for All

Chapter Five — Introduction to CSS

Thanasis Troboukis  ·  All Books

Book Three · Chapter Five

Introduction to CSS

HTML tells the browser what the content is. CSS tells it how it should look. In this chapter you learn the language of visual design.

What Is CSS?

CSS stands for Cascading Style Sheets. It is a separate language from HTML, dedicated entirely to describing how elements should look: their colours, sizes, fonts, spacing, and position on the page.

The word "cascading" refers to how CSS resolves conflicts: when multiple rules apply to the same element, a set of rules determines which one wins. For now, the important idea is that CSS is a layer of visual design that sits on top of your HTML structure.

A CSS rule has two parts: a selector that targets one or more HTML elements, and a declaration block in curly braces containing one or more property: value pairs.

CSS · Try it — a first rule

HTML

CSS

Live Preview

CSS comments are written between /* ... */. Use them to annotate your stylesheets, exactly as you would with Python comments using #.

Selectors — Targeting Elements

A selector determines which elements a rule applies to. There are three essential selector types you will use constantly.

Element selectors target every instance of a tag. Writing p { ... } applies the rule to every paragraph on the page.

Class selectors target elements with a specific class attribute. A class selector begins with a dot: .byline { ... } applies only to elements with class="byline". One element can have multiple classes; one class can be applied to many elements.

ID selectors target a single element with a specific id attribute. They begin with a hash: #intro { ... }. An ID must be unique on the page — use it for one-off elements like a page header.

CSS · Try it — element, class, and id selectors

HTML

CSS

Live Preview

Colours — Named, Hex, and RGB

CSS supports several ways to specify a colour:

CSS · Try it — colour formats

HTML

CSS

Live Preview

For colour inspiration, search for "hex colour picker" in your browser, or look at how major news sites define their colour palette by using the browser's developer tools (right-click → Inspect).

Borders and Backgrounds

Two more essential CSS properties for article design:

border draws a line around an element. You specify width, style, and colour: border: 1px solid #111. You can also target individual sides: border-top, border-left, etc. — very useful for dividers and pull-quote markers.

background-color fills the element's background. Combined with padding (covered in the next chapter), it creates callout boxes, highlighted sections, and fact panels.

CSS · Try it — borders and backgrounds

HTML

CSS

Live Preview

What you have learned in this chapter: the structure of a CSS rule, element/class/ID selectors, colour formats, and border and background properties. Next chapter: typography and the box model — the tools that determine whether your article is a pleasure to read.

Chapter Navigation

Move between chapters.