Accent colour
 
Shortcuts: press ?
 

CSS Explained — The Language That Gives Websites Their Look

Muhammad Tahir November 26, 2026

Introduction

Open any website and look past the words and pictures. The spacing between paragraphs, the colour of the links, the size of the headings, the way the menu sits at the top — none of that comes from the text itself. It comes from a separate set of instructions that tells the browser how everything should look. That is CSS, short for Cascading Style Sheets, and it is the reason the web looks like something designed rather than something typed.

The early web had no CSS. When Tim Berners-Lee put the first website online in 1991, pages were plain text with links, rendered in whatever typeface and size the browser chose. By 1993, Mosaic had added images, but a page still looked like a typed document with pictures pasted in. There was no way to say "make this heading blue" or "put these two columns side by side." If you wanted a page to look a certain way, you abused the tools you had: tables meant for data became page layouts, invisible images became spacers, and font tags piled up until the markup was unreadable.

In 1994, a Norwegian engineer named Håkon Wium Lie, working at CERN alongside Berners-Lee, proposed a fix: split the job in two. HTML would describe what a page contains, and a separate stylesheet language would describe how it looks. The idea took hold, and in December 1996 the World Wide Web Consortium published the first version, CSS1. It was basic by modern standards — fonts, colours, margins, a little positioning — but it changed the shape of the web: for the first time, the look of a page lived in one place, separate from the words.

This article explains how CSS works, in plain language, with real history: the box model, selectors and the cascade, layout from tables to Flexbox and Grid, responsive design, type and colour, and the ways CSS still trips people up thirty years later.

What CSS Actually Does

At its simplest, CSS is a list of rules. Each rule has two parts: a selector, which says which parts of the page it applies to, and a set of declarations, which say what should change. A rule might say "all paragraphs use a 16-pixel font" or "the heading at the top is dark blue and centred." The browser reads the rules and paints the page to match.

The power of the system is that one stylesheet can control an entire site. A news website with ten thousand pages does not need the colour of its links written into every page. It writes the rule once — links are blue, underlines appear on hover — and every page follows it. Change the rule, and all ten thousand pages change at once. That is the payoff of separating style from content: the design lives in one file, the content in another.

It also means a page can have more than one look. The same article can be printed with a stylesheet that hides the navigation and uses a serif font, or read on a phone with one that stacks everything into a single column. The words do not change — only the instructions for presenting them. In the early days this felt like a party trick. Now it is how the entire web works.

The "cascading" in the name is the part people find confusing, and it is worth understanding early. When several rules apply to the same element — one from the browser's defaults, one from the site's stylesheet, one written directly on the element — the browser needs a way to decide which one wins. The cascade is that decision process. Most beginner frustration with CSS comes from this: you write a rule, nothing happens, and the reason is almost always that another rule with higher priority is winning quietly somewhere else.

The Box Model: Everything Is a Rectangle

The first idea to grasp about CSS layout is that the browser sees every element on a page as a rectangle. A paragraph is a rectangle, a heading is a rectangle, an image is a rectangle — even a single word sits in its own small rectangle. The page is a stack of boxes, and CSS is mostly the art of deciding how big each box is and where it sits.

Each box has four layers. In the middle is the content itself. Around that is padding, empty space inside the box that pushes the content away from the edges. Around the padding is the border, a line that can be visible or invisible. And around the border is the margin, empty space outside the box that pushes other boxes away. Change any of those four numbers and the page shifts.

This sounds simple, until you meet the question that has caused more arguments than any other in CSS: when you say a box is 300 pixels wide, does that mean the content is 300 pixels wide, or the whole box including padding and border? For years, browsers disagreed. Internet Explorer counted the padding and border inside the 300 pixels; other browsers added them on top, so a box meant to be 300 pixels wide came out at 340. Developers spent the late 1990s and early 2000s working around this split, known as the box model bug. It was fixed, eventually, by a setting called box-sizing: border-box, which makes the width you state mean the width of the whole box. Today, most developers start every project by turning that setting on for the whole page. It is a good example of how CSS grew: not from a clean plan, but from fixes to problems real people hit while building real sites.

Margins have their own quirk. When two boxes stack vertically, their margins do not add up — they collapse, and the larger one wins. A paragraph with a 20-pixel bottom margin followed by a heading with a 30-pixel top margin ends up 30 pixels apart, not 50. This surprises everyone the first time they meet it. It exists because the gap between two paragraphs should be the gap the designer asked for, not the sum of two gaps. Reasonable or not, it is one of those details you simply have to learn, and every working web developer has a story about the afternoon they lost to margin collapse.

Selectors and the Cascade: Who Wins

Selectors are how CSS points at things. The simplest is an element name: p means every paragraph. A class selector, written with a dot, points at any element carrying that class: .warning means every element marked as a warning, whether a paragraph, a heading, or a box. An ID selector, written with a hash, points at one specific element: #main-nav means the element with that ID, and there should only be one of it on the page.

Classes are the workhorse of real CSS. A designer might define .button once — padding, rounded corners, a background colour — and apply it to every button on the site by adding the word "button" to each one's class list. Change the class, change every button. This is the same instinct behind reusable components everywhere in software, and it is why large sites stay consistent: the design is defined once and reused a thousand times.

Selectors can also be combined: "links inside the footer", "the first paragraph after a heading", "every third row of this table". These combinations, called combinators, let a stylesheet describe a page's structure without extra markup. They are powerful — and the source of the most common beginner mistake: a selector so specific it is hard to override later.

When two rules conflict, the browser decides in three steps. First, origin: rules the author wrote beat the browser's defaults. Second, specificity: a more specific selector beats a less specific one — an ID beats a class, a class beats an element name. Third, order: when two rules are equally specific, the one that appears later in the stylesheet wins.

The escape hatch is !important, a marker you can add to a declaration to make it beat everything else. It works, and it is almost always a bad idea. Once a stylesheet is full of !important markers, nothing can be predicted any more, because every rule claims to be the exception. Experienced developers treat it as a last resort, for code they cannot change. If you find yourself reaching for it often, the problem is usually that your selectors are too specific, not that the cascade is broken.

Layout: From Tables to Flexbox and Grid

For the first decade of the web, nobody had a good way to lay out a page. CSS1 offered floats, a feature meant for wrapping text around images, like a photo in a magazine article. Developers pressed floats into service as a layout tool: float one box left, another right, and you had columns. It worked, after a fashion, but it was fragile. Floated boxes escaped their containers, columns refused to match heights, and a layout that looked right in one browser broke in another. Clearing floats — forcing a container to acknowledge the boxes inside it — became a small industry of tricks, remembered today as the clearfix hack.

The situation improved in stages. Positioning let a designer pin a box to an exact spot, useful for menus and pop-ups but useless for the main layout, because a pinned box does not know or care what is around it. Inline-block let boxes sit side by side like words in a sentence, but it brought its own quirks, including mysterious gaps from the whitespace between elements in the markup.

The real change came in the 2010s, with two layout systems designed for the job from the start. The first was Flexbox, a system for arranging items in a row or a column. You declare a container flexible, and its children line up, share the available space, and can be centred, spaced evenly, or pushed to one end with a single rule. Centring something vertically, a running joke in CSS for years, became one line. Flexbox is ideal for the small pieces of a page: a navigation bar, a row of buttons, a card with an image and text.

The second was CSS Grid, which arrived in browsers in early 2017 — Chrome and Firefox shipped it within weeks of each other. Grid is for the page itself: you draw a grid of rows and columns, then place each part of the page into the cells you choose. A header across the top, a sidebar on the left, the main content in the middle, a footer at the bottom — described in a handful of lines, without floats or hacks. Between Flexbox for components and Grid for pages, the layout problems that had eaten years of developers' lives largely went away.

Both systems share a design idea worth noticing: the layout is described in the stylesheet, not in the markup. The HTML says "this is the navigation, this is the article, this is the sidebar." The CSS decides where they go. Move the sidebar from left to right and you change one rule, not the whole page. That separation is what makes a site possible to redesign without rewriting.

Responsive Design: One Page, Every Screen

For years, websites assumed a desktop screen. Pages were built at a fixed width, usually around 960 pixels, and anyone on a smaller screen scrolled sideways. Then the iPhone arrived in 2007, and suddenly a large share of visitors were looking at the web through a screen the size of a playing card. Fixed-width pages were unreadable on them, so sites built separate mobile versions — m.example.com, a stripped-down copy — and sent phone users there. It worked, but it meant building and maintaining two sites.

In May 2010, a designer named Ethan Marcotte published an article in A List Apart called "Responsive Web Design". His proposal was simple: instead of building two sites, build one site that adapts. Use flexible grids that shrink, images that scale, and media queries — rules that only apply when the screen meets a condition, like "narrower than 600 pixels". A three-column layout on a desktop becomes one column on a phone, and the text stays readable throughout.

The idea spread fast. By 2012, responsive design was the default way to build a new site, and the separate mobile site faded away. The habit that made it work is designing mobile-first — starting with the small screen and adding layout as the screen grows, rather than starting with the desktop and cutting things away. It forces a designer to decide what actually matters, because on a phone there is no room for anything that does not.

Responsive design also changed how designers think about images. A photograph that looks fine at 1200 pixels wide is a waste of bandwidth on a phone, where it shows at 350 pixels. Modern HTML lets a page offer the browser several versions of an image and pick the right one. Small details, but on a slow mobile connection they are the difference between a page that loads in a second and one that takes ten.

Type and Colour: The Look of the Words

Typography is where CSS shows its range. A stylesheet controls the font family, size, weight, line spacing, letter spacing, and alignment of every piece of text. The early web was limited to a handful of fonts every computer had — Arial, Times New Roman, Verdana — because a page could only use a font the reader's machine already owned. If you wanted a distinctive typeface, you used an image of text, which search engines could not read and screen readers could not speak.

Web fonts changed that. A site can now bundle its own fonts and send them to the browser along with the page, and the browser renders the text in exactly the typeface the designer chose. Google Fonts, launched in 2010, made this free and easy, and today most sites use at least one web font. The trade-off is performance: every font file is a download, and a page loading five weights of three typefaces is slower than one loading a single font. Good designers pick their fonts like their words — few, and with intent.

Colour in CSS is more flexible than most people expect. Colours can be named (red, steelblue), written as hex codes (#336699), or described as red-green-blue values with an optional transparency. Transparency is the quiet revolution: a box can be half-transparent, letting the background show through, which makes overlays, shadows, and layered effects possible without images. Gradients — a smooth blend from one colour to another — are also drawn by CSS now, not by image files, which keeps pages light and edges sharp at any size.

The details of type and colour are where a site's character lives. Two pages with the same words can feel entirely different: one set in a warm serif with generous line spacing feels like a book; the same words in a tight, bold sans-serif with a dark background feel like a poster. The difference between a good site and a forgettable one is often a handful of small typographic choices — a little more space between lines, a slightly smaller heading, a colour used sparingly.

Modern CSS: Variables, Nesting, and Less Code

CSS keeps growing. Custom properties, usually called CSS variables, let a stylesheet define a value once — a brand colour, a spacing unit — and reuse it everywhere, so changing the brand colour means changing one line. Nesting, long available in tools like Sass, finally arrived in plain CSS in 2023, letting rules sit inside other rules the way the page structure suggests. Container queries let a component adapt to its own container, not just the screen.

Alongside the language, the working methods matured. Teams adopted naming systems like BEM, which gives every class a structured name so large stylesheets stay readable, and built reusable component libraries — buttons, cards, forms — that work the same way on every page. It is the same instinct behind design patterns in software: name the recurring solutions, and stop solving the same problem from scratch.

Where It Goes Wrong

CSS has a reputation for being fiddly, and the reputation is earned. The cascade that makes it powerful also makes it unpredictable: a rule written months ago in a stylesheet you forgot about can override the rule you just wrote, and finding it means tracing through thousands of lines. Large projects fight this with strict conventions and tools that warn about unused rules.

Browser differences still bite, though far less than they did in the 2000s, when a page could look different in every browser. Today the differences are small — a form control here, a new feature there — but they still exist, and developers test in more than one browser for that reason. The other classic failure is performance: a page with a dozen web fonts, huge background images, and animations on everything will feel slow, and no amount of clever CSS fixes a page that downloads too much.

Frequently Asked Questions

Is CSS a programming language?

Not in the usual sense. It has no loops, no conditions of its own, and no way to store data beyond custom properties. It is declarative: you describe the result you want, and the browser works out how to produce it. You are describing a look, not writing steps.

Why does my CSS rule sometimes do nothing?

Almost always, another rule with higher specificity is winning, or the rule is not reaching the element at all — a typo in a class name, a stylesheet that failed to load, or a selector that does not match the markup. Browser developer tools show exactly which rules apply to an element and which ones lost; that panel is the fastest way to find the answer.

Should I use Flexbox or Grid?

Use both, for different jobs. Flexbox is for one-dimensional layout — a row or column of items, like a navigation bar. Grid is for two-dimensional layout — rows and columns together, like the structure of a page. Most real pages use Grid for the skeleton and Flexbox for the pieces inside it.

Do I still need to learn CSS with tools like Tailwind around?

Yes. Tools like Tailwind, which provide ready-made classes, are built on top of CSS, and they help only if you understand what their classes do. When a layout breaks — and it will — the fix is a CSS idea, not a tool idea. The tools change every few years; the language underneath is stable.

The Bottom Line

CSS is the reason the web looks designed. It took a plain-text medium and gave it type, colour, spacing, and layout — first crudely, with tables and font tags, then properly, with a language of its own. The box model, the cascade, Flexbox, Grid, and responsive design are not separate tricks. They are one idea, refined over thirty years: describe what a page contains once, and describe how it should look separately, so the same content works on a desktop, a phone, and a printed page.

It is also a language that rewards patience. The rules are few, the quirks are well known, and most confusion comes from the cascade doing exactly what it was told. Learn the box model, learn specificity, and learn Flexbox and Grid, and the rest is practice. The web's best-looking pages were built by people who understood a small set of ideas and applied them carefully, one rule at a time.

Was this article helpful?

Muhammad Tahir

Author

 

Get new articles by email

 

Comments