CSS Formatter
Paste CSS. Format it, check it as you type. Nothing leaves your browser.
- 0
- Rules
- 0
- Declarations
- 0
- Errors
On this page
About CSS Formatter
Updated
What it does
CSS Formatter beautifies CSS. It puts your CSS into a readable, standard layout: one declaration per line, clear indents and space between rules. It also checks your CSS as you type: a missing brace, colon or semicolon, an unclosed comment or quote. It rings the exact spot, gives the line and column, and says how to fix it.
How to use it
Type or paste your CSS into the editor and press Format. Lines that Format changed stay marked until you edit them, and Show before shows your text as it was. Press Example to try it with a sample.
Summarize CSS
The Result pane counts the rules, selectors, declarations, properties and media queries in your CSS. Open Notes to see things worth fixing next to their lines: properties your browser does not know, values it does not accept, properties set twice in one rule, and empty rules.
Customization
Open Options to choose the indent, how many blank lines to keep, whether each selector gets its own line, a blank line between rules, and spaces around combinators. The tool remembers your choices.
Common CSS errors
Missing semicolon
The most common CSS error is a missing semicolon. A semicolon separates one declaration from the next. Without it, the next declaration becomes part of the value, and both stop working. This is very different from JavaScript, where a missing semicolon is often not an error.
.my-class {
color: red
background-color: blue;
}Missing colon
A colon separates a property from its value. Without it, the browser skips the declaration.
.my-class {
color red;
background-color: blue;
}Missing closing brace
A CSS rule must be enclosed in curly braces. If the closing brace is missing, the next rules are read as part of this one.
.my-class {
color: red;
background-color: blue;
.next {
margin: 0;
}Incorrect property name
Property names must match the specification exactly. A misspelled property is skipped. The following does not set the color to red, because the property is color, not colour. The tool shows this as a note.
.my-class {
colour: red;
background-color: blue;
}About CSS
CSS is a language that describes the style of an HTML document. CSS describes how HTML elements are displayed.
CSS has been the standard for styling web pages since the late 1990s. It adds colors, background images, borders, spacing and much more to web pages. It sets the size and position of page elements, and creates different layouts for different screen sizes.
CSS syntax
CSS is a rule-based language. You define rules that apply groups of styles to particular elements or groups of elements on your web page. The following example shows a simple CSS rule:
p {
color: red;
text-align: center;
}The rule begins with a selector. The selector p means the rule applies to all <p> elements on the page. Then comes a declaration block in curly braces. Inside it are one or more declarations, separated by semicolons. Each declaration is a property name and a value, separated by a colon.
CSS selectors
Selectors find (or select) the HTML elements you want to style. There are five kinds. See CSS selectors on MDN for all of them.
Simple selectors select elements by name, id or class:
/* All <p> elements */
p {
text-align: center;
}
/* The element with id="demo" */
#demo {
font-size: 150%;
}
/* All elements with class="intro" */
.intro {
font-weight: bold;
}Combinator selectors select elements by their relationship to other elements:
/* All <p> elements inside <div> elements */
div p {
color: red;
}
/* All <p> elements whose direct parent is a <div> */
div > p {
color: red;
}Pseudo-class selectors select elements in a certain state:
/* Links under the mouse pointer */
a:hover {
color: red;
}
/* Every <p> that is the first of its type */
p:first-of-type {
color: red;
}Pseudo-element selectors style a part of an element:
/* The first letter of every <p> */
p::first-letter {
font-size: xx-large;
}
/* The first line of every <p> */
p::first-line {
color: #ff0000;
}Attribute selectors select elements by an attribute or its value:
/* Links with a target attribute */
a[target] {
background-color: yellow;
}
/* Links with target="_blank" */
a[target="_blank"] {
background-color: yellow;
}CSS properties
These are some of the most common CSS properties. You do not need to memorize them, but it helps to know what exists. For the full list, see MDN.
- background sets the background of an element, usually a color or an image.
- border sets the border of an element.
- color sets the color of the text.
- font sets the font family, size, style and weight of the text.
.example {
background: #ffffff url("tree.png") no-repeat right top;
border: 1px solid black;
color: red;
font: normal 400 20px/1.4 "Times New Roman", Times, serif;
}CSS units
Many CSS properties take a length, such as width, margin, padding and font-size. Lengths come in two kinds.
Absolute units have a fixed size:
pxpixels. 1px is 1/96 of an inch; on high-resolution screens it covers several device pixels.cmcentimeters,mmmillimeters,ininches (1in = 96px = 2.54cm).ptpoints (1pt = 1/72 of 1in),pcpicas (1pc = 12pt).
Relative units depend on something else:
emthe font size of the element (2em is twice the current font size).remthe font size of the root element.%a share of the same value on the parent element.vwandvh1% of the width or height of the viewport, the browser window.vminandvmax1% of the smaller or larger side of the viewport.chthe width of the "0" (zero) character,exthe x-height of the font.lhthe line height of the element,rlhthe line height of the root element.
Every property also accepts the keywords inherit (take the value of the parent), initial (the default value of the property) and unset (inherit if the property inherits, else initial).
CSS functions
Functions compute a value. Common ones are:
rgb()andhsl()colors, with an optional alpha value for transparency.url()a link to an image, font or other file.calc()math with mixed units, such ascalc(100% - 2rem).min(),max()andclamp()the smallest, largest or a bounded value.var()the value of a custom property, such asvar(--brand).
CSS specificity
When more than one rule applies to an element, the browser picks one by specificity, from strongest to weakest:
- Inline styles, attached directly to the element:
<h1 style="color: blue"> - Id selectors:
#para1 - Class, attribute and pseudo-class selectors:
.center - Element and pseudo-element selectors:
p - The universal selector
*adds nothing.
When two rules are equally specific, the later one wins. Inherited values and browser defaults apply only when no rule sets the property.
/* Loses: an element selector */
p {
color: red;
}
/* Wins: an id selector */
#intro {
color: green;
}CSS comments
Comments explain the code and help when you edit it later. Browsers ignore them. A CSS comment starts with /* and ends with */, and can span several lines:
/* A single-line comment */
/* A comment
over several
lines */CSS box model
Every HTML element is a box. The box model has four layers, from the inside out: the content, the padding, the border and the margin. It lets you put a border around elements and space between them.
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}CSS media queries
Media queries change styles depending on the device: its type (such as screen or print) or its features (such as the width of the viewport or the resolution). Use them in CSS with @media and @import, in HTML with the media attribute of <link>, <style> and <source>, and in JavaScript with window.matchMedia().
/* On screens 992px wide or less, make the background blue */
@media screen and (max-width: 992px) {
body {
background-color: blue;
}
}Other CSS resources
- CSS SelectorsSelectors find (or select) the HTML elements you want to style.
- CSS UnitsCSS has several different units for expressing a length.
- CSS ColorsThe ways to write a color in CSS: names, HEX, RGB, HSL and more.
- CSS Media QueriesMedia queries check things such as the width of the viewport, the orientation and the resolution.
- CSS Properties IndexAn alphabetical index of CSS properties, with syntax and examples.
- ListamaticOne list, many options. Ways to style a list with CSS.
- FloatutorialLearn to float elements on a page with this CSS tutorial.
- A Complete Guide to FlexboxFlexbox is a CSS layout module for flexible one-dimensional layouts.
- A Complete Guide to GridCSS Grid Layout is a two-dimensional system that handles both columns and rows.
- Awesome CSS LearningA short list of the best CSS learning resources.
- New CSS TechniquesNew CSS techniques you should know.
CSS FAQ
What is CSS?
CSS stands for Cascading Style Sheets. CSS describes how HTML elements are displayed on screen, on paper, or in other media.
What is the difference between CSS and HTML?
HTML creates the structure of a web page. CSS styles the web page.
What is the difference between CSS and JavaScript?
CSS styles the web page. JavaScript adds behavior to the web page.
What is the difference between CSS and Bootstrap?
CSS is a language that describes the style of an HTML document. Bootstrap is a framework with ready-made CSS and JavaScript files.
What is the difference between CSS and Sass, SCSS or Less?
Sass, SCSS and Less are CSS preprocessors. They add features such as variables, nesting and mixins, and compile to plain CSS. SCSS is the Sass syntax that looks like CSS. This tool formats plain CSS.
What is the difference between CSS and Tailwind?
CSS is a language that describes the style of an HTML document. Tailwind is a CSS framework of small utility classes that you combine in your HTML. It generates a CSS file from the classes you use.
Where is the best place to learn CSS?
How old is CSS?
CSS was first proposed by Håkon Wium Lie on October 10, 1994. The first CSS specification was published on December 17, 1996.
What is the current version of CSS?
CSS no longer has one version number. After CSS 2.1, CSS was split into separate modules, such as Selectors, Color and Grid, and each module has its own level. People still say CSS3 for the modules after CSS 2.1, but there is no CSS4.
Is CSS being actively developed?
Yes. The CSS Working Group at the W3C keeps adding features, such as nesting, container queries and new color functions.
Who owns CSS?
CSS is maintained by the W3C, through its CSS Working Group.
Who invented CSS?
CSS was invented by Håkon Wium Lie and Bert Bos.
Should I still learn CSS today?
Yes. CSS is one of the core technologies of the web, together with HTML and JavaScript. Every framework still produces CSS in the end.
How does the CSS Formatter work?
The CSS Formatter uses the css_beautify function from js-beautify to format your CSS.
How does the CSS Formatter validate CSS?
It reads your CSS character by character and checks the structure: braces, comments, quotes, colons and semicolons. Then it asks your browser, through CSS.supports(), whether it knows each property and value. Unknown properties and values show as notes, because a new property can be valid before every browser knows it.
Is there an industry standard way of formatting CSS?
There is no official standard. Most style guides put each declaration on its own line, use one space after the colon, and put the opening brace on the same line as the selector. That is what this tool does.
Is it OK to omit the last semicolon in a CSS rule?
Yes, the last semicolon in a rule is optional. It is still good practice to write it, so that adding a new line later does not break the rule. The formatter keeps your semicolons as they are.
Should I use single or double quotes in CSS?
There is no official standard. You can use single or double quotes for strings, and single, double or no quotes for URLs. Pick one and use it everywhere. The formatter keeps your quotes as they are.
Should I sort declarations in alphabetical order?
There is no official standard. Some teams sort declarations by name, others group them by type (position, box, text). The formatter does not change the order, because order can matter in CSS.