Favorites

CSS Notification Boxes

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Notification Boxes

Why notification boxes matter

Notification boxes, also called alert boxes, are coloured panels that tell visitors something important. You've seen them everywhere: a red box when you mistype your password, a green one when your order goes through, a yellow one warning that your session is about to expire.

A good example is a sign-up form. If someone makes a mistake while registering, an error box can explain exactly what went wrong. If everything is correct, a success box can confirm they're registered and can now log in.

In this tutorial we'll build the four most common types of notification box:

  1. Error: something went wrong and needs fixing.
  2. Success: an action worked.
  3. Warning: something needs care or attention.
  4. Notice: general, helpful information.

Here's what we're aiming for:

Results
Error

Your password must be at least 8 characters long.

Success

You're registered! You can now log in.

Warning

Your session will expire in 5 minutes.

Notice

We've updated our privacy policy.

Step 1: the HTML

Every box uses the same structure. A wrapper holds an icon, a short title and the message itself:

<div class="alert alert--error" role="alert">
  <svg class="alert__icon" ...></svg>
  <div class="alert__body">
    <strong class="alert__title">Error</strong>
    <p>Your password must be at least 8 characters long.</p>
  </div>
</div>

Each box gets two classes. alert holds the styles every box shares, and a second class such as alert--error picks the type. The double dash is a common naming habit meaning "a variation of".

What's role="alert"?

The role attribute tells screen readers (software that reads pages aloud for people with low vision) what an element is for. Without it, a box that pops up after a form is sent might never be announced at all.

  • role="alert" means "read this out straight away, even if it interrupts". Use it for errors and urgent warnings.
  • role="status" means "read this out at the next natural pause". Use it for success messages and notices.
<div class="alert alert--success" role="status"> ... </div>

Tip: these roles matter most for messages that appear after the page loads, for example when JavaScript checks a form. Screen readers will announce text added inside an element with one of these roles.

Step 2: the basic box

Now for the shared styles. Notice that the colours aren't set here; instead the rule uses var() to read custom properties (CSS variables), which each box type will fill in during step 3.

.alert {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px;
  margin: 0 0 16px;
  border: 1px solid var(--alert-border);
  border-left: 5px solid var(--alert-accent);
  border-radius: 8px;
  background: var(--alert-bg);
  color: #1f2937;
  font: 1rem/1.5 system-ui, sans-serif;
}

.alert__title {
  display: block;
  font-weight: 700;
  color: var(--alert-title);
}

.alert__body p {
  margin: 0;
}

Line by line

LineWhat it does
display: flex;Places the icon and text side by side in a row
align-items: flex-start;Keeps the icon level with the first line of text, even for long messages
gap: 12px;Adds space between the icon and the text
padding: 14px 16px;Space inside the box: 14px top and bottom, 16px left and right
margin: 0 0 16px;Space below each box, so stacked boxes don't touch
border and border-leftA thin border all round, then a thicker coloured bar down the left side
border-radius: 8px;Gently rounded corners
backgroundA pale tint that changes with each type
color: #1f2937;Dark grey message text, easy to read on every tint
fontShorthand for a 1rem size, 1.5 line height and the device's own system font

What changed since the old version? The original tutorial used an 11px Tahoma font and light grey #555 text. On today's high-resolution screens that's hard to read, so we now use 1rem (usually 16px), which also respects visitors who've set a larger text size in their browser.

Step 3: colours for each type

Here's where the custom properties pay off. Each type only needs to set four colours, and the shared rule from step 2 does the rest:

.alert--error {
  --alert-bg: #fef2f2;
  --alert-border: #fecaca;
  --alert-accent: #dc2626;
  --alert-title: #991b1b;
}
.alert--success {
  --alert-bg: #f0fdf4;
  --alert-border: #bbf7d0;
  --alert-accent: #16a34a;
  --alert-title: #166534;
}
.alert--warning {
  --alert-bg: #fffbeb;
  --alert-border: #fde68a;
  --alert-accent: #d97706;
  --alert-title: #92400e;
}
.alert--notice {
  --alert-bg: #eff6ff;
  --alert-border: #bfdbfe;
  --alert-accent: #2563eb;
  --alert-title: #1e40af;
}

Each type uses one colour family in four strengths: a very pale background, a soft border, a strong accent for the side bar and icon, and a dark shade for the title so it stays readable.

Tip: want a fifth type, such as "tip" in purple? Copy one block, rename it .alert--tip and change the four colours. You don't need to touch the base styles at all.

Step 4: adding icons

The original version of this tutorial used four separate PNG image files as background icons. Today, the simplest approach is inline SVG: a small piece of code placed straight into your HTML that draws the icon. SVG (Scalable Vector Graphics) stays sharp at any size and needs no extra files to download.

Here's the error icon, a circle with a cross:

<svg class="alert__icon" width="22" height="22" viewBox="0 0 24 24"
     fill="none" stroke="currentColor" stroke-width="2"
     stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  <circle cx="12" cy="12" r="10"/>
  <path d="M15 9l-6 6M9 9l6 6"/>
</svg>

Swap the shapes inside for the other three icons:

<!-- Success: circle with a tick -->
<circle cx="12" cy="12" r="10"/><path d="M8 12.5l2.5 2.5L16 9.5"/>

<!-- Warning: triangle with an exclamation mark -->
<path d="M12 3L2 21h20L12 3z"/><path d="M12 10v5M12 18h.01"/>

<!-- Notice: circle with an "i" -->
<circle cx="12" cy="12" r="10"/><path d="M12 11v6M12 7h.01"/>

Then add one CSS rule so each icon picks up its box's accent colour:

.alert__icon {
  flex-shrink: 0;            /* stops the icon squashing on narrow screens */
  color: var(--alert-accent);
}

This works because the SVG uses stroke="currentColor", which means "draw in the current text colour". Set the icon's color and the lines change to match.

Why aria-hidden="true"? The icon is decorative, since the title already says "Error". This attribute tells screen readers to skip it, so visitors don't hear the same thing twice.

Extra: a dismiss button

Notices and success messages are often closable. Add a button as the last item inside the box:

<button class="alert__close" type="button" aria-label="Dismiss">&times;</button>

&times; draws a neat × symbol, and aria-label gives the button a proper name for screen readers, since "×" on its own doesn't mean much when read aloud.

.alert__close {
  margin-left: auto;   /* pushes the button to the far right */
  padding: 0 4px;
  background: none;
  border: 0;
  font-size: 1.5rem;
  line-height: 1;
  color: inherit;
  opacity: 0.6;
  cursor: pointer;
}
.alert__close:hover,
.alert__close:focus-visible {
  opacity: 1;
}

CSS alone can't remove an element, so a few lines of JavaScript do the actual closing:

<script>
  document.querySelectorAll('.alert__close').forEach(function (button) {
    button.addEventListener('click', function () {
      button.closest('.alert').remove();
    });
  });
</script>
Results
Notice

Click the × to close this box.

Key rule: don't make error messages dismissible, and never make them disappear on a timer. Visitors need to be able to read an error for as long as it takes to fix the problem.

Making them accessible

Accessibility means making sure everyone can use your site, including people with low vision, colour blindness, or who use a keyboard instead of a mouse. Notification boxes carry important messages, so it's worth getting this right.

Don't rely on colour alone

About 1 in 12 men and 1 in 200 women have some form of colour blindness, and red and green are the hardest pair to tell apart. That's why every box here has three clues: a colour, a different icon shape, and a written title like "Error" or "Success".

Keep text readable

Text needs enough contrast (difference in brightness) against its background. All the colour pairs in this tutorial meet the WCAG accessibility guideline of at least 4.5 to 1 for normal text. If you change the colours, check them with your browser's developer tools or an online contrast checker.

Put the message near the problem

For forms, a box at the top saying "There were 2 problems" is helpful, but also place a short message right next to each field that needs fixing. Visitors shouldn't have to hunt for what went wrong.

The complete code

Here's all the CSS together, ready to copy into your stylesheet:

.alert {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px;
  margin: 0 0 16px;
  border: 1px solid var(--alert-border);
  border-left: 5px solid var(--alert-accent);
  border-radius: 8px;
  background: var(--alert-bg);
  color: #1f2937;
  font: 1rem/1.5 system-ui, sans-serif;
}
.alert__icon  { flex-shrink: 0; color: var(--alert-accent); }
.alert__title { display: block; font-weight: 700; color: var(--alert-title); }
.alert__body p { margin: 0; }

.alert--error   { --alert-bg: #fef2f2; --alert-border: #fecaca; --alert-accent: #dc2626; --alert-title: #991b1b; }
.alert--success { --alert-bg: #f0fdf4; --alert-border: #bbf7d0; --alert-accent: #16a34a; --alert-title: #166534; }
.alert--warning { --alert-bg: #fffbeb; --alert-border: #fde68a; --alert-accent: #d97706; --alert-title: #92400e; }
.alert--notice  { --alert-bg: #eff6ff; --alert-border: #bfdbfe; --alert-accent: #2563eb; --alert-title: #1e40af; }

.alert__close {
  margin-left: auto;
  padding: 0 4px;
  background: none;
  border: 0;
  font-size: 1.5rem;
  line-height: 1;
  color: inherit;
  opacity: 0.6;
  cursor: pointer;
}
.alert__close:hover,
.alert__close:focus-visible { opacity: 1; }

And the HTML for all four boxes:

<div class="alert alert--error" role="alert">
  <svg class="alert__icon" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="10"/><path d="M15 9l-6 6M9 9l6 6"/></svg>
  <div class="alert__body">
    <strong class="alert__title">Error</strong>
    <p>Write your error message here.</p>
  </div>
</div>

<div class="alert alert--success" role="status">
  <svg class="alert__icon" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="10"/><path d="M8 12.5l2.5 2.5L16 9.5"/></svg>
  <div class="alert__body">
    <strong class="alert__title">Success</strong>
    <p>Write your success message here.</p>
  </div>
</div>

<div class="alert alert--warning" role="alert">
  <svg class="alert__icon" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3L2 21h20L12 3z"/><path d="M12 10v5M12 18h.01"/></svg>
  <div class="alert__body">
    <strong class="alert__title">Warning</strong>
    <p>Write your warning message here.</p>
  </div>
</div>

<div class="alert alert--notice" role="status">
  <svg class="alert__icon" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="10"/><path d="M12 11v6M12 7h.01"/></svg>
  <div class="alert__body">
    <strong class="alert__title">Notice</strong>
    <p>Write your notice message here.</p>
  </div>
  <button class="alert__close" type="button" aria-label="Dismiss">&times;</button>
</div>

Wrapping up

With one shared rule, four sets of colour variables and some lightweight SVG icons, you have a complete set of notification boxes that are easy to read, easy to extend and friendly to every visitor. Try adding your own type, or adjust the colours to match your brand.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.