Skip to content

Commit

Permalink
Add recommendation to avoid using !important
Browse files Browse the repository at this point in the history
This recommendation was adopted internally to align with our infrastructure conformance checks and best practices.

While !important is a legitimate feature of CSS, its use increased complexity and maintenance costs when styles were reused and shared across multiple projects.
  • Loading branch information
tonyruscoe authored Jan 13, 2022
1 parent 8d4a22b commit e6233c7
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions htmlcssguide.html
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,27 @@ <h4 id="Hexadecimal_Notation" class="numbered">Hexadecimal Notation</h4>
color: #ebc;
</code></pre>

<h4 id="Important_Declarations" class="numbered">Important Declarations</h4>

<p>Avoid using <code>!important</code> declarations.</p>

<p>These declarations break the natural cascade of CSS and make it difficult to
reason about and compose styles. Use
<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity">selector specificity</a>
to override properties instead.</p>

<pre><code class="language-css bad">/* Not recommended */
.example {
font-weight: bold !important;
}
</code></pre>

<pre><code class="language-css good">/* Recommended */
.example {
font-weight: bold;
}
</code></pre>

<h4 id="Hacks" class="numbered">Hacks</h4>

<p>Avoid user agent detection as well as CSS “hacks”—try a different approach
Expand All @@ -713,8 +734,8 @@ <h4 id="Declaration_Order" class="numbered">Declaration Order</h4>

<p>Alphabetize declarations.</p>

<p>Put declarations in alphabetical order in order to achieve consistent code in a
way that is easy to remember and maintain.</p>
<p>Put declarations in alphabetical order in order to
achieve consistent code in a way that is easy to remember and maintain.</p>

<p>Ignore vendor-specific prefixes for sorting purposes. However, multiple
vendor-specific prefixes for a certain CSS property should be kept sorted (e.g.
Expand Down

0 comments on commit e6233c7

Please sign in to comment.