Lably
Skip to content

CSS

Style sheet language

  • 28 min read
  • 6 chapters
  • 1 lab on Lably

Learn it on Lably

CSS is designed to enable the separation of content and presentation, including layout, colors, and fonts. This separation can improve content accessibility, since the content can be written without concern for its presentation; provide more flexibility and control in the specification of presentation characteristics; enable multiple web pages to share formatting by specifying the relevant CSS in a separate .css file, which reduces complexity and repetition in the structural content; and enable the .css file to be cached to improve the page load speed between the pages that share the file and its formatting.

Separation of formatting and content also makes it feasible to present the same markup page in different styles for different rendering methods, such as on-screen, in print, by voice (via speech-based browser or screen reader), and on Braille-based tactile devices. CSS also has rules for alternative formatting if the content is accessed on a mobile device.

The name cascading comes from the specified priority scheme to determine which declaration applies if more than one declaration of a property match a particular element. This cascading priority scheme is predictable.

The CSS specifications are maintained by the World Wide Web Consortium (W3C). Internet media type (MIME type) text/css is registered for use with CSS by RFC 2318 (March 1998). The W3C operates a free CSS validation service for CSS documents.

In addition to HTML, other markup languages support the use of CSS including XHTML, plain XML, SVG, and XUL. CSS is also used in the GTK widget toolkit.

Syntax

CSS has a simple syntax and uses a number of English keywords to specify the names of various style properties.

Style sheet

A style sheet consists of a list of rules. Each rule or rule-set consists of one or more selectors, and a declaration block.

Selector

In CSS, selectors declare which part of the markup a style applies to by matching tags and attributes in the markup itself.

Selector types

Selectors may apply to the following:

  • all elements of a specific type, e.g. the second-level headers h2
  • elements specified by attribute, in particular:
    • id: an identifier unique within the document, denoted in the selector language by a hash prefix e.g. #id
    • class: an identifier that can annotate multiple elements in a document, denoted by a dot prefix e.g. .classname (the phrase "CSS class", although sometimes used, is a misnomer, as element classes—specified with the HTML class attribute—is a markup feature that is distinct from browsers' CSS subsystems, and the related W3C/WHATWG standards work on document styles; see RDF and microformats for the origins of the "class" system of the Web content model)
  • elements depending on how they are placed relative to others in the document tree.

Classes and IDs are case-sensitive, start with letters, and can include alphanumeric characters, hyphens, and underscores. A class may apply to any number of instances of any element. An ID may only be applied to a single element.

Pseudo-classes

Pseudo-classes are used in CSS selectors to permit formatting based on information that is not contained in the document tree.

One example of a widely used pseudo-class is :hover, which identifies content only when the user "points to" the visible element, usually by holding the mouse cursor over it. It is appended to a selector as in a:hover or #elementid:hover.

A pseudo-class classifies document elements, such as :link or :visited, whereas a pseudo-element makes a selection that may consist of partial elements, such as ::first-line or ::first-letter. Note the distinction between the double-colon notation used for pseudo-elements and the single-colon notation used for pseudo-classes.

Combinators

Multiple simple selectors may be joined using combinators to specify elements by location, element type, id, class, or any combination thereof. The order of the selectors is important. For example, div .myClass {color: red;} applies to all elements of class myClass that are inside div elements, whereas .myClass div {color: red;} applies to all div elements that are inside elements of class myClass. This is not to be confused with concatenated identifiers such as div.myClass {color: red;} which applies to div elements of class myClass.

Summary of selector syntax

The following table provides a summary of selector syntax indicating usage and the version of CSS that introduced it.

Declaration block

A declaration block consists of a pair of braces ({}) enclosing a semicolon-separated list of declarations.

Declaration

Each declaration itself consists of a property, a colon (:), and a value. Optional white-space may be around the declaration block, declarations, colons, and semi-colons for readability.

Properties

Properties are specified in the CSS standard. Each property has a set of possible values. Some properties can affect any type of element, and others apply only to particular groups of elements.

Values

Values may be keywords, such as "center" or "inherit", or numerical values, such as 200px (200 pixels), 50vw (50 percent of the viewport width) or 80% (80 percent of the parent element's width).

Color values can be specified with keywords (e.g. "red"), hexadecimal values (e.g. #FF0000, also abbreviated as #F00), RGB values on a 0 to 255 scale (e.g. rgb(255, 0, 0)), RGBA values that specify both color and alpha transparency (e.g. rgba(255, 0, 0, 0.8)), or HSL or HSLA values (e.g. hsl(0 100% 50%), hsl(0 100% 50% / 0.8)).

Non-zero numeric values representing linear measures must include a length unit, which is either an alphabetic code or abbreviation, as in 200px or 50vw; or a percentage sign, as in 80%. Some units – cm (centimetre); in (inch); mm (millimetre); pc (pica); and pt (point) – are absolute, which means that the rendered dimension does not depend upon the structure of the page; others – em (em); ex (ex) and px (pixel) – are relative, which means that factors such as the font size of a parent element can affect the rendered measurement. These eight units were a feature of CSS 1 and retained in all subsequent revisions. The proposed CSS Values and Units Module Level 3 will, if adopted as a W3C Recommendation, provide seven further length units: ch; Q; rem; vh; vmax; vmin; and vw.

Use

Before CSS, nearly all presentational attributes of HTML documents were contained within the HTML markup. That is, all font colors, background styles, element alignments, borders, and sizes had to be explicitly described (often repeatedly) within the HTML. CSS lets authors move much of that information to another file, the style sheet, resulting in considerably simpler HTML. And additionally, as more and more devices are able to access responsive web pages, different screen sizes and layouts begin to appear. Customizing a website for each device size is costly and increasingly difficult. The modular nature of CSS means that styles can be reused in different parts of a site or even across sites, promoting consistency and efficiency.

For example, headings (h1 elements), sub-headings (h2), sub-sub-headings (h3), etc., are defined structurally using HTML. In print and on the screen, choice of font, size, color and emphasis for these elements is presentational.

Before CSS, document authors who wanted to assign such typographic characteristics to, say, all h2 headings had to repeat HTML presentational markup for each occurrence of that heading type. This made documents more complex, larger, and more error-prone and difficult to maintain. CSS allows the separation of presentation from structure. CSS can define color, font, text alignment, size, borders, spacing, layout and many other typographic characteristics, and can do so independently for on-screen and printed views. CSS also defines non-visual styles, such as reading speed and emphasis for aural text readers. The W3C has now deprecated the use of all presentational HTML markup.

For example, under pre-CSS HTML, a heading element defined with red text would be written as:

<span id="mwAws"></span><span class="p" id="mwAww"><</span><span class="nt" id="mwAw0">h1</span><span class="p" id="mwAw4">><</span><span class="nt" id="mwAw8">font</span> <span class="na" id="mwAxA">color</span><span class="o" id="mwAxE">=</span><span class="s" id="mwAxI">"red"</span><span class="p" id="mwAxM">></span>Chapter 1.<span class="p" id="mwAxQ"></</span><span class="nt" id="mwAxU">font</span><span class="p" id="mwAxY">></</span><span class="nt" id="mwAxc">h1</span><span class="p" id="mwAxg">></span>

Using CSS, the same element can be coded using style properties instead of HTML presentational attributes:

<span id="mwAxw"></span><span class="p" id="mwAx0"><</span><span class="nt" id="mwAx4">h1</span> <span class="na" id="mwAx8">style</span><span class="o" id="mwAyA">=</span><span class="s" id="mwAyE">"color: red;"</span><span class="p" id="mwAyI">></span>Chapter 1.<span class="p" id="mwAyM"></</span><span class="nt" id="mwAyQ">h1</span><span class="p" id="mwAyU">></span>

The advantages of this may not be immediately clear but the power of CSS becomes more apparent when the style properties are placed in an internal style element or, even better, an external CSS file. For example, suppose the document contains the style element:

<span id="mwAyk"></span><span class="p" id="mwAyo"><</span><span class="nt" id="mwAys">style</span><span class="p" id="mwAyw">></span> <span class="w" id="mwAy0"> </span><span class="nt" id="mwAy4">h1</span><span class="w" id="mwAy8"> </span><span class="p" id="mwAzA">{</span> <span class="w" id="mwAzE"> </span><span class="k" id="mwAzI">color</span><span class="p" id="mwAzM">:</span><span class="w" id="mwAzQ"> </span><span class="kc" id="mwAzU">red</span><span class="p" id="mwAzY">;</span> <span class="w" id="mwAzc"> </span><span class="p" id="mwAzg">}</span> <span class="p" id="mwAzk"></</span><span class="nt" id="mwAzo">style</span><span class="p" id="mwAzs">></span>

All h1 elements in the document will then automatically become red without requiring any explicit code. If the author later wanted to make h1 elements blue instead, this could be done by changing the style element to:

<span id="mwA0E"></span><span class="p" id="mwA0I"><</span><span class="nt" id="mwA0M">style</span><span class="p" id="mwA0Q">></span> <span class="w" id="mwA0U"> </span><span class="nt" id="mwA0Y">h1</span><span class="w" id="mwA0c"> </span><span class="p" id="mwA0g">{</span> <span class="w" id="mwA0k"> </span><span class="k" id="mwA0o">color</span><span class="p" id="mwA0s">:</span><span class="w" id="mwA0w"> </span><span class="kc" id="mwA00">blue</span><span class="p" id="mwA04">;</span> <span class="w" id="mwA08"> </span><span class="p" id="mwA1A">}</span> <span class="p" id="mwA1E"></</span><span class="nt" id="mwA1I">style</span><span class="p" id="mwA1M">></span>

rather than by laboriously going through the document and changing the color for each individual h1 element.

The styles can also be placed in an external CSS file, as described below, and loaded using syntax similar to:

<span id="mwA1k"></span><span class="p" id="mwA1o"><</span><span class="nt" id="mwA1s">link</span> <span class="na" id="mwA1w">href</span><span class="o" id="mwA10">=</span><span class="s" id="mwA14">"path/to/file.css"</span> <span class="na" id="mwA18">rel</span><span class="o" id="mwA2A">=</span><span class="s" id="mwA2E">"stylesheet"</span> <span class="na" id="mwA2I">type</span><span class="o" id="mwA2M">=</span><span class="s" id="mwA2Q">"text/css"</span><span class="p" id="mwA2U">></span>

This further decouples the styling from the HTML document and makes it possible to restyle multiple documents by simply editing a shared external CSS file.

Specificity

Specificity refers to the relative weights of various rules. It determines which styles apply to an element when more than one rule could apply. Based on the specification, a simple selector (e.g. H1) has a specificity of 1, class selectors have a specificity of 1,0, and ID selectors have a specificity of 1,0,0. Because the specificity values do not carry over as in the decimal system, commas are used to separate the "digits" (a CSS rule having 11 elements and 11 classes would have a specificity of 11,11, not 121).

Thus the selectors of the following rule result in the indicated specificity:

Examples

Consider this HTML fragment:

<span id="mwBBY"></span><span class="cp" id="mwBBc"><!DOCTYPE html></span> <span class="p" id="mwBBg"><</span><span class="nt" id="mwBBk">html</span><span class="p" id="mwBBo">></span> <span class="p" id="mwBBs"><</span><span class="nt" id="mwBBw">head</span><span class="p" id="mwBB0">></span> <span class="p" id="mwBB4"><</span><span class="nt" id="mwBB8">meta</span> <span class="na" id="mwBCA">charset</span><span class="o" id="mwBCE">=</span><span class="s" id="mwBCI">"utf-8"</span><span class="p" id="mwBCM">></span> <span class="p" id="mwBCQ"><</span><span class="nt" id="mwBCU">style</span><span class="p" id="mwBCY">></span> <span class="w" id="mwBCc"> </span><span class="p" id="mwBCg">#</span><span class="nn" id="mwBCk">xyz</span><span class="w" id="mwBCo"> </span><span class="p" id="mwBCs">{</span><span class="w" id="mwBCw"> </span><span class="k" id="mwBC0">color</span><span class="p" id="mwBC4">:</span><span class="w" id="mwBC8"> </span><span class="kc" id="mwBDA">blue</span><span class="p" id="mwBDE">;</span><span class="w" id="mwBDI"> </span><span class="p" id="mwBDM">}</span> <span class="w" id="mwBDQ"> </span><span class="p" id="mwBDU"></</span><span class="nt" id="mwBDY">style</span><span class="p" id="mwBDc">></span> <span class="p" id="mwBDg"></</span><span class="nt" id="mwBDk">head</span><span class="p" id="mwBDo">></span> <span class="p" id="mwBDs"><</span><span class="nt" id="mwBDw">body</span><span class="p" id="mwBD0">></span> <span class="p" id="mwBD4"><</span><span class="nt" id="mwBD8">p</span> <span class="na" id="mwBEA">id</span><span class="o" id="mwBEE">=</span><span class="s" id="mwBEI">"xyz"</span> <span class="na" id="mwBEM">style</span><span class="o" id="mwBEQ">=</span><span class="s" id="mwBEU">"color: green;"</span><span class="p" id="mwBEY">></span>To demonstrate specificity<span class="p" id="mwBEc"></</span><span class="nt" id="mwBEg">p</span><span class="p" id="mwBEk">></span> <span class="p" id="mwBEo"></</span><span class="nt" id="mwBEs">body</span><span class="p" id="mwBEw">></span> <span class="p" id="mwBE0"></</span><span class="nt" id="mwBE4">html</span><span class="p" id="mwBE8">></span>

In the above example, the declaration in the style attribute overrides the one in the <style> element because it has a higher specificity, and thus, the paragraph appears green:

To demonstrate specificity

Inheritance

Inheritance is a key feature in CSS; it relies on the ancestor-descendant relationship to operate. Inheritance is the mechanism by which properties are applied not only to a specified element but also to its descendants. Inheritance relies on the document tree, which is the hierarchy of XHTML elements in a page based on nesting. Descendant elements may inherit CSS property values from any ancestor element enclosing them. In general, descendant elements inherit text-related properties, but their box-related properties are not inherited. Properties that can be inherited are color, font, letter spacing, line-height, list-style, text-align, text-indent, text-transform, visibility, white-space and word-spacing. Properties that cannot be inherited are background, border, display, float and clear, height and width, margin, min- and max-height and -width, outline, overflow, padding, position, text-decoration, vertical-align and z-index.

Inheritance can be used to avoid declaring certain properties over and over again in a style sheet, allowing for shorter CSS.

Inheritance in CSS is not the same as inheritance in class-based programming languages, where it is possible to define class B as "like class A, but with modifications". With CSS, it is possible to style an element with "class A, but with modifications". However, it is not possible to define a CSS class B like that, which could then be used to style multiple elements without having to repeat the modifications.

Example

Given the following style sheet:

<span id="mwBGk"></span><span class="nt" id="mwBGo">p</span><span class="w" id="mwBGs"> </span><span class="p" id="mwBGw">{</span> <span class="w" id="mwBG0"> </span><span class="k" id="mwBG4">color</span><span class="p" id="mwBG8">:</span><span class="w" id="mwBHA"> </span><span class="kc" id="mwBHE">blue</span><span class="p" id="mwBHI">;</span> <span class="p" id="mwBHM">}</span>

Suppose there is a p element with an emphasizing element (<em>) inside:

<span id="mwBHg"></span><span class="p" id="mwBHk"><</span><span class="nt" id="mwBHo">p</span><span class="p" id="mwBHs">></span> This is to <span class="p" id="mwBHw"><</span><span class="nt" id="mwBH0">em</span><span class="p" id="mwBH4">></span>illustrate<span class="p" id="mwBH8"></</span><span class="nt" id="mwBIA">em</span><span class="p" id="mwBIE">></span> inheritance <span class="p" id="mwBII"></</span><span class="nt" id="mwBIM">p</span><span class="p" id="mwBIQ">></span>

If no color is assigned to the em element, the emphasized word "illustrate" inherits the color of the parent element, p. The style sheet p has the color blue, hence, the em element is likewise blue:

This is to illustrate inheritance

Whitespace

The whitespace between properties and selectors is ignored. This code snippet:

<span id="mwBI0"></span><span class="nt" id="mwBI4">body</span><span class="p" id="mwBI8">{</span><span class="k" id="mwBJA">overflow</span><span class="p" id="mwBJE">:</span><span class="kc" id="mwBJI">hidden</span><span class="p" id="mwBJM">;</span><span class="k" id="mwBJQ">background</span><span class="p" id="mwBJU">:</span><span class="mh" id="mwBJY">#000000</span><span class="p" id="mwBJc">;</span><span class="k" id="mwBJg">background-image</span><span class="p" id="mwBJk">:</span><span class="nb" id="mwBJo">url</span><span class="p" id="mwBJs">(</span><span class="sx" id="mwBJw">images/bg.gif</span><span class="p" id="mwBJ0">);</span><span class="k" id="mwBJ4">background-repeat</span><span class="p" id="mwBJ8">:</span><span class="kc" id="mwBKA">no-repeat</span><span class="p" id="mwBKE">;</span><span class="k" id="mwBKI">background-position</span><span class="p" id="mwBKM">:</span><span class="kc" id="mwBKQ">left</span><span class="w" id="mwBKU"> </span><span class="kc" id="mwBKY">top</span><span class="p" id="mwBKc">;}</span>

is functionally equivalent to this one:

<span id="mwBKs"></span><span class="nt" id="mwBKw">body</span><span class="w" id="mwBK0"> </span><span class="p" id="mwBK4">{</span> <span class="w" id="mwBK8"> </span><span class="k" id="mwBLA">overflow</span><span class="p" id="mwBLE">:</span><span class="w" id="mwBLI"> </span><span class="kc" id="mwBLM">hidden</span><span class="p" id="mwBLQ">;</span> <span class="w" id="mwBLU"> </span><span class="k" id="mwBLY">background-color</span><span class="p" id="mwBLc">:</span><span class="w" id="mwBLg"> </span><span class="mh" id="mwBLk">#000000</span><span class="p" id="mwBLo">;</span> <span class="w" id="mwBLs"> </span><span class="k" id="mwBLw">background-image</span><span class="p" id="mwBL0">:</span><span class="w" id="mwBL4"> </span><span class="nb" id="mwBL8">url</span><span class="p" id="mwBMA">(</span><span class="sx" id="mwBME">images/bg.gif</span><span class="p" id="mwBMI">);</span> <span class="w" id="mwBMM"> </span><span class="k" id="mwBMQ">background-repeat</span><span class="p" id="mwBMU">:</span><span class="w" id="mwBMY"> </span><span class="kc" id="mwBMc">no-repeat</span><span class="p" id="mwBMg">;</span> <span class="w" id="mwBMk"> </span><span class="k" id="mwBMo">background-position</span><span class="p" id="mwBMs">:</span><span class="w" id="mwBMw"> </span><span class="kc" id="mwBM0">left</span><span class="w" id="mwBM4"> </span><span class="kc" id="mwBM8">top</span><span class="p" id="mwBNA">;</span> <span class="p" id="mwBNE">}</span>

Indentation

One common way to format CSS for readability is to indent each property and give it its own line. In addition to formatting CSS for readability, shorthand properties can be used to write out the code faster, which also gets processed more quickly when being rendered:

<span id="mwBNw"></span><span class="nt" id="mwBN0">body</span><span class="w" id="mwBN4"> </span><span class="p" id="mwBN8">{</span> <span class="w" id="mwBOA"> </span><span class="k" id="mwBOE">overflow</span><span class="p" id="mwBOI">:</span><span class="w" id="mwBOM"> </span><span class="kc" id="mwBOQ">hidden</span><span class="p" id="mwBOU">;</span> <span class="w" id="mwBOY"> </span><span class="k" id="mwBOc">background</span><span class="p" id="mwBOg">:</span><span class="w" id="mwBOk"> </span><span class="mh" id="mwBOo">#000</span><span class="w" id="mwBOs"> </span><span class="nb" id="mwBOw">url</span><span class="p" id="mwBO0">(</span><span class="sx" id="mwBO4">images/bg.gif</span><span class="p" id="mwBO8">)</span><span class="w" id="mwBPA"> </span><span class="kc" id="mwBPE">no-repeat</span><span class="w" id="mwBPI"> </span><span class="kc" id="mwBPM">left</span><span class="w" id="mwBPQ"> </span><span class="kc" id="mwBPU">top</span><span class="p" id="mwBPY">;</span> <span class="p" id="mwBPc">}</span>

Sometimes, multiple property values are indented onto their own line:

<span id="mwBPs"></span><span class="p" id="mwBPw">@</span><span class="k" id="mwBP0">font-face</span><span class="w" id="mwBP4"> </span><span class="p" id="mwBP8">{</span> <span class="w" id="mwBQA"> </span><span class="nt" id="mwBQE">font-family</span><span class="o" id="mwBQI">:</span><span class="w" id="mwBQM"> </span><span class="s1" id="mwBQQ">'Comic Sans'</span><span class="o" id="mwBQU">;</span> <span class="w" id="mwBQY"> </span><span class="nt" id="mwBQc">font-size</span><span class="o" id="mwBQg">:</span><span class="w" id="mwBQk"> </span><span class="nt" id="mwBQo">20px</span><span class="o" id="mwBQs">;</span> <span class="w" id="mwBQw"> </span><span class="nt" id="mwBQ0">src</span><span class="o" id="mwBQ4">:</span><span class="w" id="mwBQ8"> </span><span class="nt" id="mwBRA">url</span><span class="o" id="mwBRE">(</span><span class="s1" id="mwBRI">'first.example.com'</span><span class="o" id="mwBRM">),</span> <span class="w" id="mwBRQ"> </span><span class="nt" id="mwBRU">url</span><span class="o" id="mwBRY">(</span><span class="s1" id="mwBRc">'second.example.com'</span><span class="o" id="mwBRg">),</span> <span class="w" id="mwBRk"> </span><span class="nt" id="mwBRo">url</span><span class="o" id="mwBRs">(</span><span class="s1" id="mwBRw">'third.example.com'</span><span class="o" id="mwBR0">),</span> <span class="w" id="mwBR4"> </span><span class="nt" id="mwBR8">url</span><span class="o" id="mwBSA">(</span><span class="s1" id="mwBSE">'fourth.example.com'</span><span class="o" id="mwBSI">);</span> <span class="p" id="mwBSM">}</span>

Positioning

CSS 2.1 defines three positioning schemes:

Normal flow
Inline items are laid out in the same way as the letters in words in the text, one after the other across the available space until there is no more room, then starting a new line below. Block items stack vertically, like paragraphs and like the items in a bulleted list. Normal flow also includes the relative positioning of block or inline items and run-in boxes.
Floats
A floated item is taken out of the normal flow and shifted to the left or right as far as possible in the space available. Other content then flows alongside the floated item.
Absolute positioning
An absolutely positioned item has no place in, and no effect on, the normal flow of other items. It occupies its assigned position in its container independently of other items.

Position property

There are five possible values of the position property. If an item is positioned in any way other than static, then the further properties top, bottom, left, and right are used to specify offsets and positions.The element having position static is not affected by the top, bottom , left or right properties.

Static

The default value places the item in the normal flow.

Relative

The item is placed in the normal flow, and then shifted or offset from that position. Subsequent flow items are laid out as if the item had not been moved.

Absolute

Specifies absolute positioning. The element is positioned in relation to its nearest non-static ancestor.

Fixed

The item is absolutely positioned in a fixed position on the screen even as the rest of the document is scrolled

Float and clear

The float property may have one of three values. Absolutely positioned or fixed items cannot be floated. Other elements normally flow around floated items, unless they are prevented from doing so by their clear property.

left
The item floats to the left of the line that it would have appeared in; other items may flow around its right side.
right
The item floats to the right of the line that it would have appeared in; other items may flow around its left side.
clear
Forces the element to appear underneath ('clear') floated elements to the left (clear:left), right (clear:right) or both sides (clear:both).

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and information technology

Source and licence. Adapted from the Wikipedia article “CSS” (this version, 2026-09-28) by its contributors, shared under CC BY-SA 4.0. Lablypedia has left out its tables, references and most pictures, and linked it to other Lablypedia articles.