Introduction
A .css file is a style sheet that sets how web pages look: colours, fonts,
spacing and layout. The structure of a page stays in
HTML, which loads the style sheet with a
link element. Håkon Wium Lie proposed CSS in 1994, and the
World Wide Web Consortium published the first
level as a recommendation in 1996. It now grows through separate modules, each with its
own specification.
Tools such as Sass produce .css files from other languages, and
bundlers commonly strip them of spaces and comments for delivery.
Structure
A style sheet is plain text, normally UTF-8. A file in another encoding can name it with
@charset, which must be the very first thing in the file. Comments are
written /* */; there is no line comment.
The file is a list of rules. A rule has a selector, which picks elements, and a block
of declarations in braces, each a property, a colon, a value and a semicolon. At-rules
start with @, such as @media, @import,
@font-face and @keyframes. Custom properties have names
starting with two hyphens and are read with var(). Rules can also be nested
inside other rules.
When several rules set the same property on an element, the cascade decides which wins, by origin and importance, then specificity, then which comes last.
/* Cards use the site's accent colour. */
:root {
--accent: #0b6e4f;
}
.card > h2 {
color: var(--accent);
margin-block: 0 0.5rem;
}
@media (min-width: 40rem) {
.card {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
Not described
Known, not yet covered
- Reference: this overview has no sub-pages. It says what a rule, an at-rule and a custom property are, but does not list the selectors and combinators, the properties, the value types and units, the colour syntaxes, the functions (
calc(),var(),clamp()), or the at-rules in full; the CSS specifications and the MDN reference linked below do. - Reference: the cascade is named (origin, importance, specificity, order) but how specificity is counted, cascade layers (
@layer), scoping (@scope), container queries, nesting rules and the way an invalid declaration is dropped are not described. The modules are mentioned, but there is no list of them or of what is stable. - Viewer: a style sheet is coloured by shape alone. Property names are not checked against any list, so a misspelt property is coloured like a real one, and every plain word in a value (
red,grid,and) gets the same colour, so a colour name is not told from another keyword. - Viewer: some forms are misread.
unicode-range: U+0025-00FFis split into a word and two numbers and a nested rule’s&is left plain. - Viewer: the file is not validated or parsed into a tree: there is no outline, no list of problems and no specificity, and
@import,url()files and@font-facesources are not followed. Only the first 1 MiB of a file is read and coloured.
Links
Software
- Firefox – web browser with style sheet inspection tools (Windows, macOS, Linux)
- Google Chrome – web browser with style sheet inspection tools (Windows, macOS, Linux)
- Visual Studio Code – free code editor with built-in CSS support (Windows, macOS, Linux)
Developers
- PostCSS – parses CSS and transforms it with plugins (JavaScript)
- Lightning CSS – parser, transformer and minifier (Rust)
- CSSTree – CSS parser and tools (JavaScript)
- Sass – language that compiles to CSS (JavaScript)
Reference
- CSS current work – the W3C’s list of every CSS specification and its status
- CSS Syntax Module Level 3 – how a style sheet is tokenised and parsed
- MDN CSS reference – Mozilla’s documentation of properties and selectors