What Is CSS?
CSS is the language used to control the appearance and layout of web pages. HTML describes the structure of the content, while CSS determines how that content should look.
CSS means Cascading Style Sheets
CSS stands for Cascading Style Sheets.
It is a stylesheet language used to describe the presentation of HTML and other structured documents.
CSS can control colours, fonts, spacing, borders, positioning, page layout, animations and how a website adapts to different screen sizes.
What does CSS look like?
Suppose a web page contains this HTML:
We could style that heading using CSS:
The HTML identifies the content as a heading. The CSS controls how that heading is displayed.
CSS is written as rules
A CSS rule contains a selector and one or more declarations.
Selector
Identifies which HTML elements should receive the styling.
Declaration
Specifies the property that should change and the value it should receive.
Properties and values control the design
In this declaration:
| Part | Meaning |
|---|---|
| color | The CSS property |
| blue | The value assigned to the property |
A colon separates the property from the value, and declarations normally end with a semicolon.
CSS selectors choose what to style
CSS provides several ways to target HTML elements.
Classes make styles reusable
An HTML element can be given a class:
CSS can then target that class:
The same class can be applied to multiple elements, allowing the styling to be reused.
CSS controls colour
Colours can be represented in several ways.
Your Freelance Coder site, for example, uses:
CSS controls text appearance
CSS can control the typeface, size, weight, spacing and alignment of text.
HTML elements are treated like boxes
One of the most important CSS concepts is the box model.
An element can have:
Content
The text, image or other content inside the element.
Padding
Space between the content and the border.
Border
A line surrounding the element and its padding.
Margin
Space outside the element separating it from surrounding elements.
CSS controls dimensions
Widths and heights can be expressed using pixels, percentages, viewport units and other CSS units.
CSS can arrange elements with Flexbox
Flexbox is useful for arranging elements along a row or column and controlling their alignment and spacing.
CSS Grid can create page layouts
This creates two equal-width columns.
CSS Grid is particularly useful for layouts involving rows and columns.
CSS can adapt a website to different screens
Media queries allow styles to change depending on properties such as the width of the screen.
A two-column desktop layout could therefore become a single-column layout on a phone.
CSS can respond to user interaction
Pseudo-classes such as :hover can apply styles when a user interacts with an element.
This is the same basic idea behind the green hover effects used throughout Freelance Coder.
CSS can create smooth transitions
Instead of the colour changing instantly, the browser transitions smoothly between the two colours.
CSS can also create animations
CSS animations can change properties over time without requiring JavaScript for every visual effect.
Why is it called "Cascading" Style Sheets?
More than one CSS rule can potentially apply to the same element.
The browser must therefore determine which declaration wins.
The result depends on factors including:
The cascade is a core part of CSS. It provides a defined system for resolving competing style declarations.
Some CSS properties are inherited
Certain properties can pass from a parent element to its descendants.
Text inside many child elements may inherit these properties automatically unless another rule overrides them.
CSS can be added in several ways
| Method | Example |
|---|---|
| External stylesheet | styles.css |
| Internal stylesheet | <style> ... </style> |
| Inline style | style="color: blue;" |
Larger websites usually benefit from reusable stylesheets rather than repeating styles directly inside individual HTML elements.
CSS can live in its own file
A stylesheet might be saved as:
HTML can then load it using:
This separation allows one stylesheet to control the appearance of many pages.
HTML, CSS and JavaScript have different roles
HTML
Defines the structure and meaning of the content.
CSS
Defines the presentation, design and layout.
JavaScript
Adds programming logic, behaviour and interactivity.
Together
They form the core technologies behind much of modern front-end web development.
Structure and presentation can be separated
HTML might contain:
CSS can separately define how it looks:
The HTML describes the button. CSS controls its presentation.
CSS does not replace HTML
CSS normally styles an existing document structure.
Without HTML or another document language providing the content and structure, there is little for ordinary CSS to style.
HTML describes what the content is. CSS describes how that content should be presented.
What is CSS used for?
Structure becomes design.
CSS is the stylesheet language used to control how web content is presented. It works alongside HTML to control colours, typography, spacing, dimensions, layouts, responsive behaviour, hover effects and animations, making it one of the fundamental technologies of the modern web.