Every website you’ve ever found visually appealing exists because of CSS.
Without it, the web is just unstyled text and broken layouts. CSS, or Cascading Style Sheets, is the style sheet language that controls how HTML documents look in a browser, handling everything from typography and color values to grid layout and responsive design.
It’s one of the 3 core technologies of the web, alongside HTML and JavaScript.
This guide covers what CSS is, how the browser rendering process works, and how the cascade, box model, selectors, and layout systems actually function so you can write CSS with confidence.
What is CSS?
CSS, or Cascading Style Sheets, is a style sheet language that controls the visual presentation of documents written in HTML and other markup languages. It handles layout, color, typography, spacing, and every visual property of a web page, completely separate from the document’s content structure.
Without CSS, every web page would render as unstyled plain text. The browser has no default sense of design. CSS is what tells it how big the headings are, what color the background should be, and how elements are positioned on the screen.
Håkon Wium Lie proposed the first draft of Cascading HTML Style Sheets on October 10, 1994, while working at CERN alongside Tim Berners-Lee. Bert Bos joined the effort, and together they developed the foundational CSS concept. The first official W3C Recommendation, CSS Level 1, was published on December 17, 1996 (W3C).
HTML/CSS is used by 53% of all developers worldwide, making it the second most-used technology after JavaScript, according to the 2024 Stack Overflow Developer Survey across 65,000+ respondents.
What Does CSS Stand For?
Cascading refers to the priority system CSS uses to resolve conflicting style rules. When multiple declarations target the same element, the cascade determines which one wins.
Style Sheets are the files or blocks of rules that describe how HTML elements should look. A single HTML document can link to multiple style sheets, and the cascade merges them all.
What Problem Did CSS Solve?
Before CSS, visual formatting was handled directly inside HTML using tags like <font>, <center>, and <b>. Every page repeated the same presentational markup. Changing a site’s font meant editing hundreds of individual files.
CSS introduced a clean separation: HTML defines structure and meaning, CSS defines appearance. One external stylesheet file can control the visual design of an entire site. That single principle is still the foundation of frontend development today.
What Can CSS Style?
CSS applies to any document written in a markup language that supports it. That includes HTML, XHTML, XML, and SVG. It is also used in the GTK widget toolkit for desktop application interfaces.
CSS controls 5 broad categories of visual output:
- Typography: font family, size, weight, line height, letter spacing
- Color and backgrounds: hex, RGB, HSL values, gradients, images
- Layout: positioning, flexbox, grid, float, display behavior
- Box properties: margin, border, padding, width, height
- Animation and transitions: keyframes, timing functions, transforms
How Does CSS Work in a Browser?
When a browser loads a web page, it builds 2 separate tree structures from the page’s code: the DOM from HTML, and the CSSOM from CSS. Both must be complete before anything renders on screen.
CSS is render-blocking by default. The browser pauses page rendering until it has downloaded and parsed every linked stylesheet. This directly affects Largest Contentful Paint (LCP), the Core Web Vitals metric measuring how quickly the main content becomes visible.
According to Google, an LCP under 2.5 seconds is considered good. Only 59% of mobile pages currently achieve that threshold (HTTP Archive Web Almanac, 2024), and render-blocking CSS is one of the primary causes of failure.
What is the CSSOM?
The CSS Object Model is a tree structure the browser builds by parsing all CSS rules. It maps every selector to its computed property values. The browser builds the CSSOM in parallel with the DOM, but neither can be used until both are complete.
This is why inlining critical CSS in the <head> of an HTML document speeds up initial rendering. The browser skips the network request for the CSS file and builds the CSSOM immediately from the inline rules.
What is the Render Tree?
The Render Tree is the combined output of the DOM and CSSOM. The browser walks both trees and pairs each visible DOM node with its computed CSS styles. Nodes with display: none are excluded entirely.
3 processing stages follow render tree construction:
- Layout: calculates exact position and size of every element
- Paint: fills pixels with colors, borders, shadows, and text
- Composite: layers the painted results onto the screen in correct z-order
Cumulative Layout Shift (CLS) happens when layout recalculates unexpectedly after paint, often triggered by late-loading fonts or images without declared dimensions. Good CLS scores require CSS that reserves space for all elements before they load.
Chrome DevTools Performance panel shows the full render pipeline timeline, letting developers pinpoint exactly where CSS is causing delays.
What Are the Three Ways to Add CSS to a Web Page?
There are 3 methods for attaching CSS rules to an HTML document. Each differs in scope, performance implications, and appropriate use case.
| Method | Placement | Scope | Best For |
|---|---|---|---|
| External stylesheet | <link> in <head> | Entire site | Production sites, shared styles |
| Internal stylesheet | <style> in <head> | Single page | Page-specific overrides, critical CSS |
| Inline styles | style attribute on element | Single element | Dynamic styles via JavaScript |
External Stylesheets
An external stylesheet is a separate .css file linked to an HTML document using the <link rel="stylesheet"> tag. The browser caches the file after the first load, so subsequent pages on the same site skip the download entirely.
This is the standard approach for production websites. One file controls the visual design across every page. Changing the stylesheet changes the entire site. That’s the core efficiency CSS was built for.
Internal Stylesheets
An internal stylesheet lives inside a <style> block in the document <head>. Rules apply only to that specific page, not the rest of the site.
The most practical use for internal stylesheets is critical CSS, the above-the-fold styles needed for the first visible render. Inlining these rules eliminates the render-blocking network request for the external CSS file, directly improving LCP scores.
Inline Styles
Inline styles are applied directly to an element via the style attribute: <p style="color: red;">. They carry the highest specificity of any CSS declaration, overriding external and internal stylesheets.
Overusing inline styles creates maintenance problems. Styles are scattered across the HTML rather than centralized in one file. JavaScript frameworks like React do use inline styles for dynamic, component-specific values, and that’s a legitimate use case. But for general styling, external stylesheets win.
What is the CSS Box Model?

Every HTML element is a rectangular box. The CSS box model defines the 4 concentric layers that make up that box, from innermost to outermost: content, padding, border, and margin. Every sizing and spacing calculation in CSS works from this model.
Misunderstanding the box model is the source of a large share of layout bugs in CSS. Getting it right is not optional.
What Are the Four Layers of the Box Model?
Content: the actual text, image, or element content. Width and height properties control this area by default.
Padding: transparent space between the content and the border. Background color fills the padding area.
Border: a line that wraps the padding and content. Can be styled with width, color, and dash patterns.
Margin: transparent space outside the border, separating the element from its neighbors. Margins collapse between adjacent elements in ways padding and borders do not.
What is box-sizing and Why Does It Matter?
The box-sizing property controls how width and height calculations work.
content-box is the default. A declared width of 300px applies only to the content area. Padding and border add to that, making the actual rendered width larger than 300px. This trips up most new CSS developers.
border-box includes padding and border inside the declared width. A 300px element stays 300px regardless of its padding or border values. Most developers apply this globally:
::before, *::after { box-sizing: border-box; }
Bootstrap and Tailwind both set border-box globally in their base stylesheets. That alone eliminates a whole category of sizing bugs.
What Are CSS Selectors?

A CSS selector is the part of a CSS rule that identifies which HTML elements the declaration block applies to. Selectors are how CSS targets specific elements in the DOM without modifying the HTML structure itself.
The State of CSS 2024 survey found that filter effects are the most widely used CSS feature, with over 75% of developers having used them, partly because selector targeting makes applying them so straightforward (web.dev, 2024).
What Are the Main CSS Selector Types?
There are 6 core selector categories:
- Element selectors: target all instances of a tag, e.g., p, h1, div
- Class selectors: target elements with a matching class attribute, e.g., .card
- ID selectors: target a single element by its unique ID, e.g., #header
- Attribute selectors: match elements based on attribute presence or value, e.g., [type=”text”]
- Pseudo-class selectors: target elements in specific states, e.g., :hover, :focus, :has()
- Pseudo-element selectors: target sub-parts of elements, e.g., ::before, ::first-line
Combinators connect selectors to express relationships: descendant (space), child (>), adjacent sibling (+), and general sibling (~).
What is CSS Specificity?
Specificity is the scoring system CSS uses to decide which rule wins when 2 or more declarations target the same element and property. Each selector type carries a point value:
| Selector Type | Specificity Score | Example |
|---|---|---|
| Inline style | 1,0,0,0 | style=”color: red” |
| ID selector | 0,1,0,0 | #header |
| Class / pseudo-class / attribute | 0,0,1,0 | .card, :hover |
| Element / pseudo-element | 0,0,0,1 | p, ::before |
The !important declaration overrides all specificity scores. It is not a specificity level itself, but a hard override. Using it excessively is a common mistake that makes CSS increasingly difficult to maintain.
What Are Pseudo-Classes and Pseudo-Elements?
Pseudo-classes match elements based on state or position, not markup. The :has() pseudo-class was the top-rated new CSS feature in the 2024 State of CSS survey, with 36% of respondents calling it the best addition of the year.
:has() is a parent selector. It selects an element based on what it contains. div:has(img) targets any div that contains an image. Before this, CSS had no way to style a parent based on its children.
Pseudo-elements generate virtual sub-parts of elements. ::before and ::after insert generated content before or after an element’s actual content without touching the HTML. They require a content property to render.
What is the CSS Cascade and Inheritance?

The cascade is the algorithm CSS uses to resolve conflicts between style rules. When 2 rules declare different values for the same property on the same element, the cascade determines which declaration applies. This is not arbitrary. The cascade follows a strict priority order.
Understanding the cascade is what separates developers who fight CSS from those who work with it.
What is the Cascade Order?
The cascade evaluates rules in 3 layers, from lowest to highest priority:
- Browser defaults: the user-agent stylesheet every browser ships with
- User styles: custom stylesheets applied by the visitor’s browser or operating system
- Author styles: CSS written by the developer, which overrides both layers above
Within author styles, specificity resolves conflicts. If two author rules have equal specificity, the one that appears later in the source order wins.
How Does CSS Inheritance Work?
Inheritance is a separate mechanism from the cascade. Some CSS properties pass their computed values down to child elements automatically. Others do not.
Inherited by default: color, font-family, font-size, line-height, text-align, visibility
Not inherited by default: margin, padding, border, background, width, height, display
4 keyword values control inheritance explicitly on any property:
- inherit
: forces the property to inherit from its parent
- initial
: resets to the CSS specification's default value
- unset
: inherits if the property naturally inherits, otherwise sets to initial
- revert
: rolls back to the browser's user-agent stylesheet value
What Are CSS Layout Systems?
CSS provides 5 distinct layout systems, each suited to different structural problems. Choosing the wrong one for the job creates messy, brittle code. The right choice reduces markup complexity and makes responsive design much cleaner to maintain.
Grid and Flexbox now have 95%+ global browser support across all major browsers, making legacy fallbacks unnecessary for the vast majority of projects (DEV Community, 2025).
What is Flexbox?

Flexbox is a one-dimensional layout system. It arranges items in either a row or a column. It does not handle both dimensions simultaneously.
It excels at:
- Navigation bars and horizontal menus
- Centering elements (vertically and horizontally) with minimal code
- Distributing space among items in a single direction
- Card rows that wrap onto new lines with flex-wrap
Flexbox shipped in Chrome in 2012 and became the first reliable tool for vertical centering without hacks. Before it, developers relied on negative margins, table-cell display tricks, or absolute positioning workarounds.
What is CSS Grid?

CSS Grid is a two-dimensional layout system. It controls rows and columns simultaneously. That single distinction makes it the right choice for full-page layouts, where Flexbox handles component-level alignment.
According to the 2024 State of CSS survey, 78% of developers now use Grid regularly, up from 62% just 3 years prior. CSS Grid has been supported in every major browser since 2017.
The key Grid properties:
- grid-template-columns and grid-template-rows: define the track structure
- grid-template-areas: named regions for positioning elements by name instead of coordinates
- gap: spacing between rows and columns in a single declaration
- subgrid: lets nested grids align to their parent’s tracks
The New York Times uses CSS Grid for its article and section page layouts, taking advantage of named grid areas to maintain consistent structure across responsive breakpoints.
What Are CSS Positioning and Float?
CSS positioning removes elements from normal document flow and places them at specific coordinates.
5 values control the position property:
- static: default, in normal flow, unaffected by top/right/bottom/left
- relative: offset from its normal position without affecting surrounding elements
- absolute: positioned relative to the nearest non-static ancestor
- fixed: positioned relative to the viewport, stays in place on scroll
- sticky: behaves like relative until it hits a scroll threshold, then acts fixed
Float was the dominant CSS layout tool before Flexbox. Today it is mostly useful for text wrapping around images. Using float for full-page layout in new projects is a strong sign a codebase needs updating.
What is the CSS Visual Formatting Model?
The CSS visual formatting model is the system that controls how elements generate boxes, how those boxes are positioned, and how they interact with each other during rendering. It sits above the box model and governs the entire layout engine.
Block formatting context (BFC) and inline formatting context are the 2 primary layout environments. Understanding which context an element creates determines how its children stack, float, and overflow.
What is Block Formatting Context?
A block formatting context is an isolated layout region. Elements inside a BFC do not affect elements outside it, and vice versa. A new BFC is created by any of these conditions:
- The root element ()
- float set to anything other than none
- overflow set to hidden, auto, or scroll
- display: flow-root (the modern, side-effect-free option)
- display: flex, grid, or table
BFC is how you contain floats without clearfix hacks. display: flow-root creates a BFC with no visual side effects, which is the clean solution most developers should reach for now.
How Do display, visibility, and overflow Work?
display: none removes the element from layout entirely. It takes up no space. Screen readers cannot access it.
visibility: hidden hides the element visually but keeps its space in the layout. Screen readers can still reach it. The difference matters for accessibility and animation.
The overflow property controls what happens when content is larger than its container. 4 values: visible (default, content spills out), hidden (clips content), scroll (always shows scrollbars), auto (adds scrollbars only when needed).
What Are Stacking Contexts and z-index?
Z-index only works on positioned elements (position set to anything other than static). A higher z-index places an element visually in front of a lower one, but only within the same stacking context.
Stacking contexts are created by opacity values below 1, transform properties, filter, will-change, and others. A child element cannot visually escape its parent stacking context regardless of its z-index value. This is the source of most z-index confusion in production code.
What Are CSS Units and Values?
CSS units fall into 2 categories: absolute and relative. Absolute units produce a fixed output regardless of context. Relative units scale based on something else, such as a parent element, the root element, or the viewport.
Most browsers default the root font size to 16px. That means 1rem equals 16px unless the developer or user changes it (MDN, 2024).
What Are Absolute and Relative CSS Units?
| Unit | Type | Relative To | Best For |
|---|---|---|---|
| px | Absolute | Screen pixel | Borders, shadows, fixed sizes |
| rem | Relative | Root element font size | Font sizes, spacing, layout |
| em | Relative | Parent element font size | Component-level scaling |
| vw / vh | Relative | Viewport width / height | Full-screen sections, fluid type |
| % | Relative | Parent element dimension | Fluid widths, responsive grids |
What is the Difference Between rem and em?
Bootstrap 5 and Tailwind CSS both use rem units internally by default. That industry shift reflects the growing emphasis on accessibility and scalable design systems (OpenReplay, 2024).
rem is predictable. It always calculates from the root. Change html { font-size } once and every rem value on the page scales.
em compounds. A 1.2em font inside a parent with 1.2em produces 1.44em at the rendered size. This nesting behavior is useful for modular components but tricky in deeply nested markup.
What Are CSS Custom Properties?
CSS custom properties (variables) store reusable values. They are declared with a double-dash prefix and accessed with the var() function:
:root { --color-primary: #0057ff; --spacing-base: 1rem; }
Custom properties are scoped to the element they are declared on. Declaring them on :root makes them globally available. They also respond to media queries, which Sass variables cannot.
What Are CSS Media Queries?

A CSS media query applies a block of style declarations only when a specified condition is true. The condition checks properties of the device or viewport, such as width, height, orientation, or user preferences like dark mode.
Mobile devices now account for over 60% of all global web traffic, according to Statcounter data covering August 2024 to August 2025. Media queries are the primary tool for serving different layouts to different screen sizes. Google completed its rollout of mobile-first indexing on July 5, 2024, meaning the mobile version of any page is now the version Google crawls and ranks.
What is the Syntax of a CSS Media Query?
A media query uses the @media rule followed by a media type and one or more feature conditions:
@media screen and (min-width: 768px) { .container { max-width: 1200px; } }
Media types: screen, print, all (default).
Common media features: width, min-width, max-width, orientation, resolution, prefers-color-scheme, prefers-reduced-motion.
What is Mobile-First CSS Design?
Mobile-first means writing base CSS for small screens and using min-width media queries to add styles for larger screens. Desktop-first reverses this, using max-width queries to strip styles down for smaller screens.
Mobile-first advantages:
- Forces prioritization of core content
- Produces smaller CSS payloads on mobile (only base styles load)
- Aligns with Google’s mobile-first index
Common breakpoint values in production: 480px (large phones), 768px (tablets), 1024px (small desktop), 1440px (large desktop).
What Are CSS Container Queries?
Container queries apply styles based on the size of a parent container rather than the viewport. A card component can change its own layout depending on how wide its containing section is, not how wide the browser window is.
This makes component-level responsive design possible without JavaScript or duplicating media query logic. The State of CSS 2024 survey ranked @container as the second most-liked new CSS feature at 17% alongside CSS nesting (web.dev, 2024).
What Are CSS Preprocessors?
A CSS preprocessor is a scripting language that extends CSS with features the native language does not have. You write preprocessor syntax, then compile it to standard CSS that browsers can read.
The State of CSS 2024 survey found Sass at 67% usage and PostCSS at 38% among developers who use a pre or post-processor, while 19% now use no preprocessing tool at all, signaling a growing reliance on native CSS (CSS-Tricks, 2024).
What is Sass?

Sass (Syntactically Awesome Style Sheets) is the most widely used CSS preprocessor. First released in 2006, it ships with 2 syntaxes: the original indented .sass format and the more common .scss format, which mirrors standard CSS syntax.
Core Sass features:
- Variables: store reusable values with $primary-color: #333;
- Nesting: write child selectors inside parent rules
- Mixins: reusable blocks of declarations that accept parameters
- Extends: share rule sets between selectors
- Functions: mathematical and string operations
Bootstrap uses Sass for its entire component framework. Teams building on Bootstrap inherit Sass as a dependency whether they choose it or not.
What is the Difference Between Sass, LESS, and PostCSS?
LESS (Leaner Style Sheets) appeared in 2009. It uses JavaScript for compilation and was the basis of earlier Bootstrap versions. Its syntax is closer to vanilla CSS than Sass’s original indented format, which made it easier to adopt for developers already writing CSS.
PostCSS is different in kind from Sass and LESS. It does not add a new syntax. It processes existing CSS through a plugin pipeline, applying transformations after the fact. Autoprefixer, the tool that adds vendor prefixes automatically, runs as a PostCSS plugin.
| Tool | Type | Compilation | Unique Strength |
|---|---|---|---|
| Sass / SCSS | Preprocessor | Before browser | Mixins, functions, modules |
| LESS | Preprocessor | Before browser | JavaScript-based, low setup |
| PostCSS | Post-processor | After writing CSS | Plugin-based, modular toolchain |
| Stylus | Preprocessor | Before browser | Node.js-native, optional syntax |
Do CSS Preprocessors Still Make Sense in 2024?
Native CSS now covers variables, nesting, and color functions. Many features that made Sass essential 5 years ago are now in the browser.
Sass still leads on mixins, @extend, and complex functions. No native CSS equivalent exists for these yet. The decision comes down to project complexity: small sites with modern CSS rarely need a preprocessor. Large design systems with component tokens and shared logic still benefit from Sass.
What is CSS Performance and How is It Measured?
CSS performance is how much impact a site’s stylesheets have on page load speed, rendering time, and Core Web Vitals scores. Most CSS issues fall into 3 areas: render-blocking delivery, unused rules increasing file size, and layout instability caused by late-loading assets.
The UK government’s GOV.UK platform reduced CSS size by up to 40% on some pages by switching from single bundled stylesheets to per-page stylesheets containing only the styles each page actually needed, with incremental improvements in timing metrics across multiple pages (GDS, December 2023).
What is Render-Blocking CSS and How Does It Affect LCP?
Render-blocking CSS is any stylesheet the browser must download and parse before it can show any page content. Most linked CSS files are render-blocking by default.
According to Chrome for Developers, the fix has 3 tiers:
- Delete: remove CSS rules no longer needed
- Defer: load non-critical CSS asynchronously using media
attributes or JavaScript
- Inline: place critical above-the-fold CSS directly in the
to eliminate the network request entirely
Only 59% of mobile pages achieve Google’s good LCP threshold of 2.5 seconds or less (HTTP Archive Web Almanac, 2024). Render-blocking CSS is one of the most direct causes of LCP failures across the web.
How Does Unused CSS Affect Page Speed?
Google PageSpeed Insights flags the “Reduce Unused CSS” warning whenever any CSS file contains more than 2KB of rules that are not applied to the current page. Frameworks like Bootstrap and Tailwind ship with thousands of unused utility classes unless configured correctly.
Tools for identifying unused CSS:
- Chrome DevTools Coverage tab: shows exact line-by-line usage during page load
- PurgeCSS: removes unused selectors from CSS files as part of a build step
- Google Lighthouse audit: flags unused CSS with an estimated savings in kilobytes
Minification removes whitespace, comments, and redundant characters from CSS files. Serving minified CSS via Gzip or Brotli compression typically reduces file sizes by 60 to 80%. A CSS Minifier handles this in one step without any build tool setup.
How Does CSS Affect Cumulative Layout Shift?
Cumulative Layout Shift measures unexpected visual movement of page elements after they first render. CSS causes CLS in 3 common ways.
Web fonts without size fallbacks. When a custom font loads late, the browser swaps it in, resizing text and pushing content down. The CSS font-display: swap property mitigates this but does not eliminate it entirely without careful fallback font matching.
Images without declared dimensions. An tag with no width and height attributes causes the browser to reserve no space for it. When the image loads, the layout shifts. The CSS fix: set aspect-ratio on image containers.
A good CLS score is under 0.1, according to Google’s Core Web Vitals thresholds. CSS decisions made at the component level directly control whether a site hits that target.
FAQ on CSS
What does CSS stand for?
CSS stands for Cascading Style Sheets. It is a style sheet language used to control the visual presentation of HTML documents, covering layout, color, typography, and spacing across web pages.
What is CSS used for in web design?
CSS controls how HTML elements look in a browser. It handles font properties, color values, box model spacing, grid layout, responsive design, and animations, separating visual presentation from document structure.
What is the difference between CSS and HTML?
HTML defines a document’s structure and content. CSS defines its visual appearance. HTML provides the elements; CSS applies the style rules that control how those elements render in the browser.
What is the CSS cascade?
The cascade is the algorithm CSS uses to resolve conflicting style rules. It evaluates browser defaults, user styles, and author styles in order, then uses selector specificity and source order to determine which declaration applies.
What is the CSS box model?
The box model defines every HTML element as a rectangular box with 4 layers: content, padding, border, and margin. The box-sizing property controls whether padding and border are included inside the declared width.
What is the difference between Flexbox and CSS Grid?
Flexbox is a one-dimensional layout system for rows or columns. CSS Grid is two-dimensional, controlling rows and columns simultaneously. Use Flexbox for component-level alignment and Grid for full-page or complex layout structures.
What are CSS media queries?
Media queries apply style rules only when a specified condition is true, such as a minimum viewport width. They are the primary tool for mobile-first design, letting one stylesheet serve multiple screen sizes.
What is a CSS preprocessor?
A CSS preprocessor like Sass or LESS extends CSS with variables, nesting, mixins, and functions. The preprocessor code compiles to standard CSS before the browser reads it. Sass holds 67% usage among developers who use any preprocessor (State of CSS 2024).
What are CSS custom properties?
CSS custom properties, also called variables, store reusable values declared with a double-dash prefix. They are accessed via the var() function, respond to media queries, and scope to the element they are declared on.
Does CSS affect SEO?
CSS affects SEO through above-the-fold rendering speed and Core Web Vitals scores. Render-blocking stylesheets delay Largest Contentful Paint. Unresolved layout shifts hurt Cumulative Layout Shift, both of which are direct Google ranking signals.
Conclusion
This conclusion is for an article presenting what CSS is, how it works, and why it sits at the core of every web page you build or interact with.
The cascade, selector specificity, the box model, and layout systems like Flexbox and Grid are not isolated concepts. They form a connected system, and understanding how they relate makes debugging and writing style declarations much faster.
From responsive typography to CSS animation, the language keeps expanding. Native custom properties, container queries, and the :has()` pseudo-class have changed what is possible without a preprocessor.
Performance matters too. Render-blocking stylesheets, unused CSS rules, and layout instability all affect Core Web Vitals scores directly.
CSS rewards the developers who understand it, not just the ones who memorize it.
