Over a million developers have joined DZone.

CSS Twitter Bootstrap Alert Boxes

· Web Dev Zone

Twitter Bootstrap

Twitter bootstrap is a front-end framework designed to allow you to have a framework to start all your web projects on. The framework comes with so many features from grids, responsive design, typography, forms, tables, buttons, navigation, labels, alerts and progress bars just to name a few.

Twitter bootstrap is a github projects and is design for the web community by the web community. If you have something to add to it or want to download it you can see the project on github.

Twitter Bootstrap

In this post we are going to look at one of my favourite features of the bootstrap...alert boxes. These are perfect to display a small message on the screen for different scenarios.

There are a few alert boxes that come with the framework these are:

  • Messages
  • Error
  • Success
  • Information

Alert Message

All of the alert boxes in the Twitter bootstrap will use the alert class. This sets the styling which all the alert boxes will use but for the other boxes we will just change the colours to match the scenario.

.alert {
padding: 8px 35px 8px 14px;
margin-bottom: 18px;
color: #c09853;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
background-color: #fcf8e3;
border: 1px solid #fbeed5;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.alert-heading {
color: inherit;
}
.alert .close {
position: relative;
top: -2px;
right: -21px;
line-height: 18px;
}

The HTML to call these are very simple just add the CSS class to a outer div or paragraph tag.

<div class="alert">Example of an alert box.</div>

Success

The success alert box will be used when the user successfully completes a task all the CSS will do is override the colours of the standard alert box.

.alert-success {
color: #468847;
background-color: #dff0d8;
border-color: #d6e9c6;
}

When you use this CSS class you need to use both the alert CSS and the alert-success CSS.

<div class="alert alert-success">Example of the successful alert box</div>

Danger or Error

The error alert box works the same as the success box it is just used to change the colour of the background and text.

.alert-danger,
.alert-error {
color: #b94a48;
background-color: #f2dede;
border-color: #eed3d7;
}

Again you need to use both the alert CSS and the alert-error CSS.

<div class="alert alert-error">Example of the error alert box</div>

Information

The last style is the information style, used to highlight a message to the user by just using the CSS class alert-info.

.alert-info {
color: #3a87ad;
background-color: #d9edf7;
border-color: #bce8f1;
}
<div class="alert alert-info">Example of the information alert box</div>

Styling Blocks

Along with the different scenario styling Twitter bootstrap also gives you additional CSS classes to further style the alert boxes.

Adding a class of alert-block you increase the padding of the box.
.alert-block {
padding-top: 14px;
padding-bottom: 14px;
}

The next styling will reduce the margins on the paragraph tags inside the alert-block.

.alert-block > p,
.alert-block > ul {
margin-bottom: 0;
}
.alert-block p + p {
margin-top: 5px;
}

The HTML to add additional padding can be used by adding the alert-block CSS class to one of the alert boxes.

<div class="alert alert-information alert-block">
Example of the information alert box
</div>

That's all the alert boxes you currently get with the Twitter bootstrap, of course there are loads of other alert box types you would want to add so why not contribute to the project and add new ones.

Curator's note: This is a prepublished article.  Due to technical difficulties, there are some examples that could not be included.  The examples can be found by clicking the source link below.

Topics:

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}