Google HTML - CSS Style Guide
Google HTML - CSS Style Guide
Table of Contents
2 General 4 CSS
2.1 General Style Rules 4.1 CSS Style Rules
2.2 General Formatting Rules 4.2 CSS Formatting Rules
2.3 General Meta Rules 4.3 CSS Meta Rules
1 Background
This document defines formatting and style rules for HTML and CSS. It aims at improving
collaboration, code quality, and enabling supporting infrastructure. It applies to raw, working files that
use HTML and CSS, including GSS files. Tools are free to obfuscate, minify, and compile as long as
the general code quality is maintained.
2 General
2.1.1 Protocol
Always use HTTPS ( https: ) for images and other media files, style sheets, and scripts, unless the
respective files are not available over HTTPS.
https://google.github.io/styleguide/htmlcssguide.html 1/17
24/4/2021 Google HTML/CSS Style Guide
/* Recommended */
@import 'https://fonts.googleapis.com/css?family=Open+Sans';
2.2.1 Indentation
<ul>
<li>Fantastic
<li>Great
</ul>
.example {
color: blue;
}
2.2.2 Capitalization
All code has to be lowercase: This applies to HTML element names, attributes, attribute values (unless
text/CDATA ), CSS selectors, properties, and property values (with the exception of strings).
/* Not recommended */
color: #E5E5E5;
https://google.github.io/styleguide/htmlcssguide.html 2/17
24/4/2021 Google HTML/CSS Style Guide
/* Recommended */
color: #e5e5e5;
2.3.1 Encoding
Make sure your editor uses UTF-8 as character encoding, without a byte order mark.
Specify the encoding in HTML templates and documents via <meta charset="utf-8"> . Do not
specify the encoding of style sheets as these assume UTF-8.
(More on encodings and when and how to specify them can be found in Handling character encodings
in HTML and CSS.)
2.3.2 Comments
Use comments to explain code: What does it cover, what purpose does it serve, why is respective
solution used or preferred?
(This item is optional as it is not deemed a realistic expectation to always demand fully documented
code. Mileage may vary heavily for HTML and CSS code and depends on the project’s complexity.)
Highlight todos by using the keyword TODO only, not other common formats like @@ .
https://google.github.io/styleguide/htmlcssguide.html 3/17
24/4/2021 Google HTML/CSS Style Guide
Append a contact (username or mailing list) in parentheses as with the format TODO(contact) .
3 HTML
Use HTML5.
HTML5 (HTML syntax) is preferred for all HTML documents: <!DOCTYPE html> .
Although fine with HTML, do not close void elements, i.e. write <br> , not <br /> .
Use valid HTML code unless that is not possible due to otherwise unattainable performance goals
regarding file size.
Using valid HTML is a measurable baseline quality attribute that contributes to learning about technical
requirements and constraints, and that ensures proper HTML usage.
https://google.github.io/styleguide/htmlcssguide.html 4/17
24/4/2021 Google HTML/CSS Style Guide
3.1.3 Semantics
Use elements (sometimes incorrectly called “tags”) for what they have been created for. For example,
use heading elements for headings, p elements for paragraphs, a elements for anchors, etc.
Using HTML according to its purpose is important for accessibility, reuse, and code efficiency reasons.
For multimedia, such as images, videos, animated objects via canvas , make sure to offer alternative
access. For images that means use of meaningful alternative text ( alt ) and for video and audio
transcripts and captions, if available.
Providing alternative contents is important for accessibility reasons: A blind user has few cues to tell
what an image is about without @alt , and other users may have no way of understanding what
video or audio contents are about either.
(For images whose alt attributes would introduce redundancy, and for images whose purpose is
purely decorative which you cannot immediately use CSS for, use no alternative text, as in alt="" .)
https://google.github.io/styleguide/htmlcssguide.html 5/17
24/4/2021 Google HTML/CSS Style Guide
Strictly keep structure (markup), presentation (styling), and behavior (scripting) apart, and try to keep
the interaction between the three to an absolute minimum.
That is, make sure documents and templates contain only HTML and HTML that is solely serving
structural purposes. Move everything presentational into style sheets, and everything behavioral into
scripts.
In addition, keep the contact area as small as possible by linking as few style sheets and scripts as
possible from documents and templates.
Separating structure from presentation from behavior is important for maintenance reasons. It is
always more expensive to change HTML documents and templates than it is to update style sheets
and scripts.
There is no need to use entity references like — , ” , or ☺ , assuming the
same encoding (UTF-8) is used for files and editors as well as among teams.
The only exceptions apply to characters with special meaning in HTML (like < and & ) as well as
control or “invisible” characters (like no-break spaces).
https://google.github.io/styleguide/htmlcssguide.html 6/17
24/4/2021 Google HTML/CSS Style Guide
For file size optimization and scannability purposes, consider omitting optional tags. The HTML5
specification defines what tags can be omitted.
(This approach may require a grace period to be established as a wider guideline as it’s significantly
different from what web developers are typically taught. For consistency and simplicity reasons it’s best
served omitting all optional tags, not just a selection.)
Do not use type attributes for style sheets (unless not using CSS) and scripts (unless not using
JavaScript).
Specifying type attributes in these contexts is not necessary as HTML5 implies text/css and
text/javascript as defaults. This can be safely done even for older browsers.
https://google.github.io/styleguide/htmlcssguide.html 7/17
24/4/2021 Google HTML/CSS Style Guide
Use a new line for every block, list, or table element, and indent every such child element.
Independent of the styling of an element (as CSS allows elements to assume a different role per
display property), put every block, list, or table element on a new line.
Also, indent them if they are child elements of a block, list, or table element.
(If you run into issues around whitespace between list items it’s acceptable to put all li elements in
one line. A linter is encouraged to throw a warning instead of an error.)
<blockquote>
<p><em>Space</em>, the final frontier.</p>
</blockquote>
<ul>
<li>Moe
<li>Larry
<li>Curly
</ul>
<table>
<thead>
<tr>
<th scope="col">Income
<th scope="col">Taxes
<tbody>
<tr>
<td>$ 5.00
<td>$ 4.50
</table>
https://google.github.io/styleguide/htmlcssguide.html 8/17
24/4/2021 Google HTML/CSS Style Guide
While there is no column limit recommendation for HTML, you may consider wrapping long lines if it
significantly improves readability.
When line-wrapping, each continuation line should be indented at least 4 additional spaces from the
original line.
<md-progress-circular
md-mode="indeterminate"
class="md-accent"
ng-show="ctrl.loading"
md-diameter="35">
</md-progress-circular>
<md-progress-circular md-mode="indeterminate"
class="md-accent"
ng-show="ctrl.loading"
md-diameter="35">
</md-progress-circular>
Use double ( "" ) rather than single quotation marks ( '' ) around attribute values.
4 CSS
https://google.github.io/styleguide/htmlcssguide.html 9/17
24/4/2021 Google HTML/CSS Style Guide
Unless dealing with CSS validator bugs or requiring proprietary syntax, use valid CSS code.
Using valid CSS is a measurable baseline quality attribute that allows to spot CSS code that may not
have any effect and can be removed, and that ensures proper CSS usage.
Instead of presentational or cryptic names, always use ID and class names that reflect the purpose of
the element in question, or that are otherwise generic.
Names that are specific and reflect the purpose of the element should be preferred as these are most
understandable and the least likely to change.
Generic names are simply a fallback for elements that have no particular or no meaning different from
their siblings. They are typically needed as “helpers.”
Using functional or generic names reduces the probability of unnecessary document or template
changes.
/* Recommended: specific */
#gallery {}
#login {}
.video {}
/* Recommended: generic */
.aux {}
.alt {}
Use ID and class names that are as short as possible but as long as necessary.
https://google.github.io/styleguide/htmlcssguide.html 10/17
24/4/2021 Google HTML/CSS Style Guide
Using ID and class names this way contributes to acceptable levels of understandability and code
efficiency.
/* Not recommended */
#navigation {}
.atr {}
/* Recommended */
#nav {}
.author {}
Unless necessary (for example with helper classes), do not use element names in conjunction with IDs
or classes.
/* Not recommended */
ul#example {}
div.error {}
/* Recommended */
#example {}
.error {}
CSS offers a variety of shorthand properties (like font ) that should be used whenever possible,
even in cases where only one value is explicitly set.
/* Not recommended */
border-top-style: none;
font-family: palatino, georgia, serif;
font-size: 100%;
line-height: 1.6;
padding-bottom: 2em;
padding-left: 1em;
https://google.github.io/styleguide/htmlcssguide.html 11/17
24/4/2021 Google HTML/CSS Style Guide
padding-right: 1em;
padding-top: 0;
/* Recommended */
border-top: 0;
font: 100%/1.6 palatino, georgia, serif;
padding: 0 1em 2em;
4.1.7 Leading 0s
font-size: .8em;
For color values that permit it, 3 character hexadecimal notation is shorter and more succinct.
/* Not recommended */
color: #eebbcc;
/* Recommended */
color: #ebc;
4.1.9 Prefixes
In large projects as well as for code that gets embedded in other projects or on external sites use
prefixes (as namespaces) for ID and class names. Use short, unique identifiers followed by a dash.
https://google.github.io/styleguide/htmlcssguide.html 12/17
24/4/2021 Google HTML/CSS Style Guide
Using namespaces helps preventing naming conflicts and can make maintenance easier, for example
in search and replace operations.
.adw-help {} /* AdWords */
#maia-note {} /* Maia */
Do not concatenate words and abbreviations in selectors by any characters (including none at all)
other than hyphens, in order to improve understanding and scannability.
/* Not recommended: does not separate the words “demo” and “image” */
.demoimage {}
/* Recommended */
#video-id {}
.ads-sample {}
4.1.11 Hacks
Avoid user agent detection as well as CSS “hacks”—try a different approach first.
It’s tempting to address styling differences over user agent detection or special CSS filters,
workarounds, and hacks. Both approaches should be considered last resort in order to achieve and
maintain an efficient and manageable code base. Put another way, giving detection and hacks a free
pass will hurt projects in the long run as projects tend to take the way of least resistance. That is,
allowing and making it easy to use detection and hacks means using detection and hacks more
frequently—and more frequently is too frequently.
Alphabetize declarations.
Put declarations in alphabetical order in order to achieve consistent code in a way that is easy to
remember and maintain.
Ignore vendor-specific prefixes for sorting purposes. However, multiple vendor-specific prefixes for a
certain CSS property should be kept sorted (e.g. -moz prefix comes before -webkit).
https://google.github.io/styleguide/htmlcssguide.html 13/17
24/4/2021 Google HTML/CSS Style Guide
background: fuchsia;
border: 1px solid;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
color: black;
text-align: center;
text-indent: 2em;
Indent all block content, that is rules within rules as well as declarations, so to reflect hierarchy and
improve understanding.
html {
background: #fff;
color: #444;
}
End every declaration with a semicolon for consistency and extensibility reasons.
/* Not recommended */
.test {
display: block;
height: 100px
}
/* Recommended */
.test {
display: block;
height: 100px;
}
https://google.github.io/styleguide/htmlcssguide.html 14/17
24/4/2021 Google HTML/CSS Style Guide
Always use a single space between property and value (but no space between property and colon) for
consistency reasons.
/* Not recommended */
h3 {
font-weight:bold;
}
/* Recommended */
h3 {
font-weight: bold;
}
Use a space between the last selector and the declaration block.
Always use a single space between the last selector and the opening brace that begins the declaration
block.
The opening brace should be on the same line as the last selector in a given rule.
/* Recommended */
#video {
margin-top: 1em;
}
/* Not recommended */
a:focus, a:active {
https://google.github.io/styleguide/htmlcssguide.html 15/17
24/4/2021 Google HTML/CSS Style Guide
/* Recommended */
h1,
h2,
h3 {
font-weight: normal;
line-height: 1.2;
}
html {
background: #fff;
}
body {
margin: auto;
width: 50%;
}
Use single ( '' ) rather than double ( "" ) quotation marks for attribute selectors and property values.
Exception: If you do need to use the @charset rule, use double quotation marks—single quotation
marks are not permitted.
/* Not recommended */
@import url("https://www.google.com/css/maia.css");
html {
font-family: "open sans", arial, sans-serif;
}
/* Recommended */
@import url(https://www.google.com/css/maia.css);
html {
https://google.github.io/styleguide/htmlcssguide.html 16/17
24/4/2021 Google HTML/CSS Style Guide
If possible, group style sheet sections together by using comments. Separate sections with new lines.
/* Header */
#adw-header {}
/* Footer */
#adw-footer {}
/* Gallery */
.adw-gallery {}
Parting Words
Be consistent.
If you’re editing code, take a few minutes to look at the code around you and determine its style. If they
use spaces around all their arithmetic operators, you should too. If their comments have little boxes of
hash marks around them, make your comments have little boxes of hash marks around them too.
The point of having style guidelines is to have a common vocabulary of coding so people can
concentrate on what you’re saying rather than on how you’re saying it. We present global style rules
here so people know the vocabulary, but local style is also important. If code you add to a file looks
drastically different from the existing code around it, it throws readers out of their rhythm when they go
to read it. Avoid this.
https://google.github.io/styleguide/htmlcssguide.html 17/17