Saved

Press the star next to a tool's name to save it.

All my tools

CSS Cheat Sheet

This css cheat sheet lists the selectors you use most in stylesheets: types, classes, attributes, combinators, pseudo-classes, nesting and keyframes. Search it, filter by topic or print it.

Restored your last values ·

Your search stays in your browser. Nothing is uploaded.

Commands

–

No commands match that search.

Recent results

This css cheat sheet follows the selectors reference on MDN Web Docs and lists how to pick the elements you want to style. [1] It covers simple selectors, the combinators that relate elements to each other, pseudo-classes and pseudo-elements, nesting and keyframes.

How to use the css cheat sheet

  1. Type in the search box to filter by any word in a name, a command or its note.
  2. Choose a topic to see one group instead of the whole list.
  3. Read the command in the right-hand column and its meaning on the left.
  4. Press Print for a clean copy of whatever is shown.

What is in each topic

The css cheat sheet is split into 5 topics. The table shows how many entries each topic holds and which ones they are, so you can jump straight to the right group with the topic box.

TopicEntriesWhat it covers
Selectors7element; class; id; attribute; starts with; everything; group
Combinators4descendant; child; next sibling; later siblings
Pseudo-classes and elements6on hover; first child; every second child; contains; before content; first line
Nesting3nested rule; nested pseudo-class; nested element
Animation2define keyframes; keyframe percentages

Simple selectors

A type selector such as p selects every element of that name, a class selector .card selects elements with that class and an ID selector #header selects the one element with that id. Attribute selectors such as [type="text"] match by attribute, where *= matches a substring, ^= a prefix and $= a suffix. The universal selector * selects every element, and a comma makes a selector list so one rule can apply to several selectors. [1]

Prefer classes for styling. IDs must be unique and are harder to override, which makes later changes awkward.

Combinators and pseudo-classes

A space is the descendant combinator, so article p matches any paragraph inside an article at any depth. The greater-than sign matches direct children only, a plus sign matches the element immediately after another, and a tilde matches every later sibling. [1] Use the child combinator when you want to limit how far a rule reaches.

Pseudo-classes pick elements by state or position, such as :hover, :focus, :checked, :first-child, :nth-child(2n), :is() and :has(). Pseudo-elements such as ::before, ::after, ::first-letter and ::placeholder style a part of an element. [1]

Nesting and keyframes

CSS nesting lets you write a rule inside another. The ampersand refers to the parent selector, so .card { & .title { } } styles titles inside cards. [1] Writing &:hover inside .card is the same as .card:hover.

Animations are defined with @keyframes. Inside, from and to mark the start and end, and percentages such as 50 percent mark steps in between. [1] Apply the animation to an element with the animation properties. Test nested rules in the browsers you support, because older ones may need the flat form.

Reading a combined selector

Selectors can be chained into one. In nav.main > ul > li:first-child a[href^="https"]::after, the rule picks a link whose address starts with https, inside the first list item of a list that is a direct child of a nav with the class main, and styles the content generated after it. Read such a selector from right to left: the last part says which element is styled, and the parts before it say where that element must sit. [1]

When a rule does not apply, check each part in turn in the browser tools, and look for another rule with a more specific selector that overrides it. A selector list with a comma, such as article h1, article h2, is a short way to avoid repeating a block of declarations.

Who this page helps

Developers use a cheat sheet to look up a command they have not typed for a while, to learn the names of the options and to compare similar commands. Beginners can read down a topic to see what is possible, and experienced users can search for the one flag they forgot.

Tips for getting the most from the css cheat sheet

  • Keep selectors short and based on classes so they are easy to override later.
  • Use the child combinator when a rule should not reach into deeper levels.
  • Test nested rules in the browsers you support before relying on them.

Common mistakes

Most problems come from running a command in the wrong place or with the wrong target. Read a command before you press Enter, replace every placeholder with your own value, and try destructive commands on a test copy first. When a command behaves differently from the note, check the version you have installed and the official documentation.

Where the entries come from

Each command was checked against the documentation listed below this article. Where a command is standard but is not shown on the page we read, the article says it is our own working. Options and defaults change between releases, so run the help command for the version you have installed before you rely on a flag.

What this page does not do

It lists the commands most people need and does not try to cover every option. For the complete syntax, error messages and advanced features, open the official documentation linked in the sources, and keep it beside the cheat sheet while you learn.

Printing and checking

Filter by topic before you print if you only need one section, and the printed page will be shorter. The notes are short on purpose, so keep your textbook or the official documentation open for the full detail and for anything that changes between versions.

What next

Use this often? Add CSS Cheat Sh to your Home screen.

Frequently asked questions

What is in this css cheat sheet?

Simple selectors, combinators, common pseudo-classes and pseudo-elements, nesting with the ampersand and keyframe animations.

What is the difference between a descendant and a child selector?

A descendant selector, a space, matches at any depth. A child selector, written with a greater-than sign, matches only direct children.

What does the ampersand do in nested CSS?

It refers to the parent selector, so you can attach pseudo-classes or extra classes to the same element inside the nested rule.

Should I style with IDs or classes?

Classes are usually better. They can be reused, and they are easier to override than IDs.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools