CSS Tutorial

CSS !important

!important wins a fight you should rarely start. Specificity and source order are cleaner tools.

A trump card, not a habit

When two declarations target the same property on the same element, CSS picks a winner with origin, specificity, then source order. !important jumps that queue. The declaration with!important beats a normal one, even if the normal one is an id selector written later.

That sounds useful. It is also how stylesheets become impossible to override. Harbor studio does not need it on a heading color. You almost always want a more specific selector, or a class that means what you mean, or a later rule in the same file.

How it looks

Write the value, a space, !important, then the semicolon. It attaches to one declaration, not to a whole rule. Spelling it ! important still works; keep it tight so people see it.

Example

<style>
  body {
    font-family: system-ui, sans-serif;
    background: #f0f9ff;
    margin: 1.25rem;
  }
  #hours {
    color: navy;
  }
  p {
    color: teal !important;
  }
</style>
<h1>Harbor studio</h1>
<p id="hours">This paragraph is teal. !important on the tag rule beats the id.</p>

An id is more specific than a tag. Without !important, #hours would win and the text would be navy. With it, the tag rule wins. That is the trick — and the mess. The next person who needs navy has to write another !important or edit this line. Specificity wars start here.

Do not copy this pattern into production. The example exists so you recognize the flag when a browser inspector shows it. Fix the real page with a class, not with a louder declaration. Preview at/css/try and then delete the flag: change p to p.hours and put that class on the paragraph instead.

Why to avoid it

!important does not make CSS “more true.” It hides the cascade so the next rule cannot speak quietly. Components, utilities, and third-party widgets all need a way in. If your button color is important, their hover state may need to be more important. The file grows a stack of flags.

Reach forInstead of !important
A class that matches the meaning.hours { color: teal; }
A more specific selectorheader p or .card .title
Source orderPut the override after the base rule in the same file
A custom propertyOverride --brand on a parent, leave the rule alone

Example

<style>
  body {
    font-family: system-ui, sans-serif;
    color: #0c4a6e;
    background: #f0f9ff;
    margin: 1.25rem;
  }
  .card p { color: #475569; }
  .card p.notice {
    color: #0d9488;
    font-weight: 700;
  }
</style>
<article class="card">
  <h1>Harbor studio</h1>
  <p>Regular copy stays slate.</p>
  <p class="notice">Desks free after 14:00 — a class won, not a flag.</p>
</article>

When it appears anyway

You will still see !important in the wild. Knowing why it showed up is more useful than pretending it never should.

  • User stylesheets and accessibility overlays. A visitor (or a browser extension) may force font size or contrast. Those rules often use !important on purpose so a site cannot shrink the type back.
  • Utility classes. Some libraries ship .hidden { display: none !important; }so the utility beats a component’s display: flex. That is a design choice of the library, not a beginner pattern to copy by hand.
  • Third-party widgets. A booked calendar iframe or a chat bubble may lock colors. You override from the outside only if the vendor left a hook. Fighting with more flags is brittle.
  • Inline style vs author sheet. An inline style="" already beats a class. An author !important in a stylesheet beats a normal inline style — another reason the flag shows up in desperate overrides.

Example

<style>
  body {
    font-family: system-ui, sans-serif;
    background: #f0f9ff;
    margin: 1.25rem;
    color: #0c4a6e;
  }
  .chip {
    display: inline-block;
    padding: 0.3rem 0.65rem;
    background: #e0f2fe;
    color: #0369a1;
    border-radius: 999px;
  }
  .chip.is-off {
    display: none;
  }
</style>
<h1>Harbor studio</h1>
<p>The sold-out chip is hidden with a class, not with !important.</p>
<span class="chip is-off">Sold out</span>
<span class="chip">Tide desk</span>

If you must debug a page that already uses the flag, the inspector’s computed pane shows which declaration won. Search the project for !important and treat each hit as a smell, not as a template. Specificity and source order are the tools this tutorial wants you to keep.

Worked examples

The short listings above show one property. These sheets paint documents you would actually ship: a lab dashboard, a clinic form, a marks table, a nav bar.

CSS does not invent meaning. HTML already said what the pieces are. Cascade, specificity, and the box model decide how they look. The numbers are classroom values — current, pH, pulse, 3-4-5 — so the style has something real to sit on.

Preview them in the CSS editor at /css/try. Change one property and watch the page paint. The tags stay the same.

Engineering

When a utility must win

!important wins a fight you should rarely start. Specificity and source order are cleaner. Use it for a utility that must beat a messy third-party sheet, then isolate it.

Here navy !important beats .lead teal. That is the demonstration, not a style you copy into every paragraph.

If everything is important, nothing is. You will need a second !important, then a third.

Example

<style>
  p { color: navy !important; }
  .lead { color: teal; }
</style>
<p class="lead">Navy wins with !important.</p>

Biology

A protocol colour you should not lock

Locking fever red with !important makes a later high-contrast mode harder. Prefer a class and a token.

Example

<style>
  .fever { color: #9f1239; }
</style>
<p class="fever">38.4 °C — class is enough</p>

FAQ: CSS !important

Common questions about this page.

What is the StudyGrid CSS tutorial?

The StudyGrid CSS tutorial is a full beginner track: selectors, the box model, text, layout, flex, grid, and responsive design. Each chapter has copy-and-preview examples.

Should I run css important examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn css important in this CSS CSS lesson (CSS !important).

Is the CSS editor the same as Try HTML?

No. Try CSS is a stylesheet preview at /css/try. Try HTML stays at /html/try. CSS lessons never open the HTML-only editor or the Python editor.

Do I need to install anything to learn CSS?

No. Open a chapter, click Try it in CSS, and the page preview updates in the browser.

Where should I start the CSS tutorial?

Start at CSS Intro, then Syntax, Selectors, and How To. After colors and the box model, continue to flex, grid, and media queries. Use Next at the bottom of each chapter.

Is the CSS tutorial free?

Yes. The CSS studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live preview editor.