PatternFly Code Guide

Standards for developing flexible, durable, and sustainable HTML and CSS.

Table of contents



Golden rule

Enforce these, or your own, agreed upon guidelines at all times. Small or large, call out what's incorrect. For additions or contributions to this Code Guide, please open an issue on GitHub.

Every line of code should appear to be written by a single person, no matter the number of contributors.



<!DOCTYPE html>
    <title>Page title</title>
    <img src="images/company-logo.png" alt="Company">
    <h1 class="hello-world">Hello, world!</h1>

HTML5 doctype

Enforce standards mode and more consistent rendering in every browser possible with this simple doctype at the beginning of every HTML page.

<!DOCTYPE html>

Language attribute

From the HTML5 spec:

Authors are encouraged to specify a lang attribute on the root html element, giving the document's language. This aids speech synthesis tools to determine what pronunciations to use, translation tools to determine what rules to use, and so forth.

Read more about the lang attribute in the spec.

Head to Sitepoint for a list of language codes.

<html lang="en-us">
  <!-- ... -->

IE compatibility mode

Internet Explorer supports the use of a document compatibility <meta> tag to specify what version of IE the page should be rendered as. Unless circumstances require otherwise, it's most useful to instruct IE to use the latest supported mode with edge mode.

For more information, read this awesome Stack Overflow article.

<meta http-equiv="X-UA-Compatible" content="IE=Edge">

Character encoding

Quickly and easily ensure proper rendering of your content by declaring an explicit character encoding. When doing so, you may avoid using character entities in your HTML, provided their encoding matches that of the document (generally UTF-8).

  <meta charset="UTF-8">

CSS and JavaScript includes

Per HTML5 spec, typically there is no need to specify a type when including CSS and JavaScript files as text/css and text/javascript are their respective defaults.

HTML5 spec links

<!-- External CSS -->
<link rel="stylesheet" href="code-guide.css">

<!-- In-document CSS -->
  /* ... */

<!-- JavaScript -->
<script src="code-guide.js"></script>

Practicality over purity

Strive to maintain HTML standards and semantics, but not at the expense of practicality. Use the least amount of markup with the fewest intricacies whenever possible.

Attribute order

HTML attributes should come in this particular order for easier reading of code.

Classes make for great reusable components, so they come first. Ids are more specific and should be used sparingly (e.g., for in-page bookmarks), so they come second.

<a class="..." id="..." data-toggle="modal" href="#">
  Example link

<input class="form-control" type="text">

<img src="..." alt="...">

Boolean attributes

A boolean attribute is one that needs no declared value. XHTML required you to declare a value, but HTML5 has no such requirement.

For further reading, consult the WhatWG section on boolean attributes:

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If you must include the attribute's value, and you don't need to, follow this WhatWG guideline:

If the attribute is present, its value must either be the empty string or [...] the attribute's canonical name, with no leading or trailing whitespace.

In short, don't add a value.

<input type="text" disabled>

<input type="checkbox" value="1" checked>

  <option value="1" selected>1</option>

Reducing markup

Whenever possible, avoid superfluous parent elements when writing HTML. Many times this requires iteration and refactoring, but produces less HTML. Take the following example:

<!-- Not so great -->
<span class="avatar">
  <img src="...">

<!-- Better -->
<img class="avatar" src="...">

JavaScript generated markup

Writing markup in a JavaScript file makes the content harder to find, harder to edit, and less performant. Avoid it whenever possible.


All new contributions need to adhere to the following rules:

Accessibility improvements to existing components are also always welcome and appreciated!



Questions on the terms used here? See the syntax section of the Cascading Style Sheets article on Wikipedia.

/* Bad CSS */
.selector, .selector-secondary, .selector[type=text] {
  margin:0px 0px 15px;
  background-color:rgba(0, 0, 0, 0.5);
  box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF

/* Good CSS */
.selector[type="text"] {
  background-color: rgba(0,0,0,.5);
  box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff;
  margin-bottom: 15px;
  padding: 15px;

Declaration order

Declarations should be ordered alphabetically by property.

.declaration-order {
  background-color: #f5f5f5;
  border: 1px solid #e5e5e5;
  border-radius: 3px;
  bottom: 0;
  color: #333;
  display: block;
  float: right;
  font: normal 13px "Helvetica Neue", sans-serif;
  height: 100px;
  left: 0;
  line-height: 1.5;
  opacity: 1;
  position: absolute;
  right: 0;
  text-align: center;
  top: 0;
  width: 100px;
  z-index: 100;

Don't use @import

Compared to <link>s, @import is slower, adds extra page requests, and can cause other unforeseen problems. Avoid them and instead opt for an alternate approach:

For more information, read this article by Steve Souders.

<!-- Use link elements -->
<link rel="stylesheet" href="core.css">

<!-- Avoid @imports -->
  @import url("more.css");

Media query placement

Place media queries as close to their relevant rule sets whenever possible. Don't bundle them all in a separate stylesheet or at the end of the document. Doing so only makes it easier for folks to miss them in the future. Here's a typical setup.

.element { ... }
.element-avatar { ... }
.element-selected { ... }

@media (min-width: 480px) {
  .element { ...}
  .element-avatar { ... }
  .element-selected { ... }

Prefixed properties

PatternFly uses Autoprefixer to deal with CSS vendor prefixes.

Do not add vender prefixes to your code, Autoprefixer will do it for you on build.

/* Bad example */
.selector {
  -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
          box-shadow: 0 1px 2px rgba(0,0,0,.15);

/* Good example */
.selector { box-shadow: 0 1px 2px rgba(0,0,0,.15); }

Single declarations

In instances where a rule set includes only one declaration, consider removing line breaks for readability and faster editing. Any rule set with multiple declarations should be split to separate lines.

The key factor here is error detection—e.g., a CSS validator stating you have a syntax error on Line 183. With a single declaration, there's no missing it. With multiple declarations, separate lines is a must for your sanity.

/* Single declarations on one line */
.span1 { width: 60px; }
.span2 { width: 140px; }
.span3 { width: 220px; }

/* Multiple declarations, one per line */
.icon           { background-position: 0 0; }
.icon-account   { background-position: 0 -40px; }
.icon-home      { background-position: 0 -20px; }
.sprite {
  background-image: url(../img/sprite.png);
  display: inline-block;
  height: 15px;
  width: 16px;

Shorthand notation

Strive to limit use of shorthand declarations to instances where you must explicitly set all the available values. Common overused shorthand properties include:

Often times we don't need to set all the values a shorthand property represents. For example, HTML headings only set top and bottom margin, so when necessary, only override those two values. Excessive use of shorthand properties often leads to sloppier code with unnecessary overrides and unintended side effects.

The Mozilla Developer Network has a great article on shorthand properties for those unfamiliar with notation and behavior.

/* Bad example */
.element {
  background: red;
  background: url("image.jpg");
  border-radius: 3px 3px 0 0;
  margin: 0 0 10px;

/* Good example */
.element {
  background-color: red;
  background-image: url("image.jpg");
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
  margin-bottom: 10px;

Nesting in Less and Sass

Avoid unnecessary nesting. Just because you can nest, doesn't mean you always should. Consider nesting only if you must scope styles to a parent and if there are multiple elements to be nested.

Additional reading:

// Without nesting
.table > thead > tr > td {  }
.table > thead > tr > th {  }

// With nesting
.table > thead > tr {
  > td {  }
  > th {  }

Operators in Less and Sass

For improved readability, wrap all math operations in parentheses with a single space between values, variables, and operators.

// Bad example
.element {
  margin: 10px 0 @variable*2 10px;

// Good example
.element {
  margin: 10px 0 (@variable * 2) 10px;


Code is written and maintained by people. Ensure your code is descriptive, well commented, and approachable by others. Great code comments convey context or purpose. Do not simply reiterate a component or class name.

Be sure to write in complete sentences for larger comments and succinct phrases for general notes.

/* Bad example */
/* Modal header */
.modal-header {

/* Good example */
/* Wrapping element for .modal-title and .modal-close */
.modal-header {

Class names

It's also useful to apply many of these same rules when creating Sass and Less variable names.

/* Bad example */
.header { ... }
.red { ... }
.t { ... }

/* Good example */
.important { ... }
.tweet { ... }
.tweet-header { ... }


Additional reading:

/* Bad example */
.avatar { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
span { ... }

/* Good example */
.avatar { ... }
.tweet .avatar { ... }
.tweet-header .username { ... }


 * Component section heading

.element { ... }

 * Component section heading
 * Sometimes you need to include optional context for the entire component. Do that up here if it's important enough.

.element { ... }

/* Contextual sub-component or modifer */
.element-heading { ... }

Editor preferences

Set your editor to the following settings to avoid common code inconsistencies and dirty diffs:

Consider documenting and applying these preferences to your project's .editorconfig file. For an example, see the one in PatternFly. Learn more about EditorConfig.