Engineering/Coding Standards/HTML And CSS/CSS/CSS Styling/

CSS Styling | CSS-01

Use valid CSS where possible · CSS-01.1 · MUST

Unless dealing with CSS validator bugs or requiring proprietary syntax, use valid CSS code.

Use tools such as the W3C CSS validator to test.

Using valid CSS is a measurable baseline quality attribute that allows us to spot CSS code that may not have any effect and can be removed, and that ensures proper CSS usage.

Use meaningful or generic class names · CSS-01.2 · MUST

Instead of presentational or cryptic names, always use 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.

/* Not recommended: meaningless */
.yee-1901 {}

/* Not recommended: presentational */
.button-green {}
.text-large {}
/* Recommended: specific */
.gallery {}
.login {}
.video {}

/* Recommended: generic */
.aux {}
.alt {}

Use class names that are as short as possible but as long as necessary · CSS-01.3 · MUST

Try to convey what a class is about while being as brief as possible.

Using class names this way contributes to acceptable levels of understandability and code efficiency.

/* Not recommended */
.navigation {}
.atr {}
/* Recommended */
.nav {}
.author {}

Separate words in class names by a hyphen · CSS-01.4 · MUST

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 {}

/* Not recommended: uses underscore instead of hyphen */
.error_status {}
/* Recommended */
.video-id {}
.ads-sample {}

Prefix selectors with an application-specific prefix · CSS-01.5 · COULD

In large projects as well as for code that gets embedded in other projects or on external sites use prefixes (as namespaces) for class names. Use short, unique identifiers followed by a dash.

Using namespaces helps preventing naming conflicts and can make maintenance easier, for example in search and replace operations.

.adw-help {} /* AdWords */
.maia-note {} /* Maia */

Avoid qualifying class names with type selectors · CSS-01.6 · MUST

Unless necessary (for example with helper classes), do not use element names in conjunction with classes.

Avoiding unnecessary ancestor selectors is useful for performance reasons.

/* Not recommended */
ul.example {}
div.error {}
/* Recommended */
.example {}
.error {}

Avoid ID selectors · CSS-01.7 · MUST

ID attributes are expected to be unique across an entire page, which is difficult to guarantee when a page contains many components worked on by many different engineers. Class selectors should be preferred in all situations.

/* Not recommended */
#example {}
/* Recommended */
.example {}

Use shorthand properties where possible · CSS-01.8 · MUST

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.

Using shorthand properties is useful for code efficiency and understandability.

/* Not recommended */
.example {
  border-top-style: none;
  font-family: palatino, georgia, serif;
  font-size: 100%;
  line-height: 1.6;
  padding-bottom: 2em;
  padding-left: 1em;
  padding-right: 1em;
  padding-top: 0;
}
/* Recommended */
.example {
  border-top: 0;
  font: 100%/1.6 palatino, georgia, serif;
  padding: 0 1em 2em;
}

Omit unit specification after "0" values, unless required · CSS-1.9 · MUST

Do not use units after 0 values unless they are required.

flex: 0px; /* This flex-basis component requires a unit. */
flex: 1 1 0px; /* Not ambiguous without the unit, but needed in IE11. */
margin: 0;
padding: 0;

Always include leading "0"s in values · CSS-1.10 · MUST

Put a 0 in front of values or lengths between -1 and 1.

font-size: 0.8em;

Use 3 character hexadecimal notation where possible · CSS-1.11 · MUST

For color values that permit it, 3 character hexadecimal notation is shorter and more succinct.

/* Not recommended */
color: #eebbcc;
/* Recommended */
color: #ebc;

Use HTTPS for embedded resources where possible · CSS-1.12 · MUST

Always use HTTPS (https:) for images and other media files, style sheets, and scripts, unless the respective files are not available over HTTPS.

/* Not recommended: omits the protocol */
@import '//fonts.googleapis.com/css?family=Open+Sans';

/* Not recommended: uses HTTP */
@import 'http://fonts.googleapis.com/css?family=Open+Sans';
/* Recommended */
@import 'https://fonts.googleapis.com/css?family=Open+Sans';

Use only lowercase · CSS-1.13 · MUST

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;
/* Recommended */
color: #e5e5e5;

Avoid using !important declarations · CSS-1.14 · SHOULD

These declarations break the natural cascade of CSS and make it difficult to reason about and compose styles. Use selector specificity to override properties instead.

/* Not recommended */
.example {
  font-weight: bold !important;
}
/* Recommended */
.example {
  font-weight: bold;
}

Avoid user agent detection as well as CSS "hacks" · CSS-1.15 · SHOULD

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.