<% @side_nav = "css" %>
<% @page_title = "Alert Boxes" %>

<%= @page_title %>

Alerts are a handy element you can drop into a form or inline on a page to communicate success, warnings, failure or just information. They'll conform to 100% of the container width you put them in.

This is a standard alert (div.alert-box). ×
This is a success alert (div.alert-box.success). ×
This is an alert (div.alert-box.alert.round). ×
This is a secondary alert (div.alert-box.secondary). ×

Build With Predefined HTML Classes

There are two ways to build alerts in Foundation 4: with our predefined HTML classes or with our structure and mixins. Building an alert using our predefined class is super-easy, you only need a block-level element with a class of .alert-box (we usually use a <div>). You may also include an anchor with a class of .close to create a close box. Here is the markup you'll need:

<%= code_example '
×
', :html %>

For these styles to come across, make sure you have the default Foundation CSS package or that you've selected alerts from a custom package. These should be linked up following our default HTML page structure.


Build with Mixins

We've included SCSS mixins used to style alert boxes. To use these mixins, you'll need to have the extension installed or grab _foundation-global.scss, _global.scss and _alert-boxes.scss from Github and throw them into a Foundation folder in your project directory. From there, you can import the files at the top of your own SCSS stylesheet, like so:

<%= code_example ' @import "foundation/foundation-global", "foundation/components/global", "foundation/components/alerts"; ', :css %>

If you are using the mixins, you may include the styles on whatever class or ID you'd like, just make sure you follow our markup structure:

<%= code_example '
×
', :html %>
Quick Mixin

You can build your alert using our global mixin by including it on your custom class or ID in your stylesheet. The mixin contains options for changing the background color, which also controls the border and text color. The rest of the defaults can be modified using the available variables. The global mixin looks like this:

<%= code_example ' /* Using the default styles */ .your-class-name { @include alert; } /* Using the available options */ .your-class-name { @include alert(#ff6c3c); } ', :css %>
This is a default alert. ×
This is an alert with a custom background set. ×
Base Mixin

Use this mixin to create the base styles for alert boxes. This will include styles that create the basic structure of an alert box.

<%= code_example ' .your-class-name { @include alert-base; } ', :css %>
This is the base mixin.
Background Mixin

You can control the background, border and text styles for the alert using this mixin. We base the border and text style on the background color you choose using some sweet Sass logic.

<%= code_example ' .your-class-name { @include alert-base; @include alert-style($bg); }', :css %>
This is base and bg mixins.

You can set $bg color to any of our predefined color variables or any hexadecimal color of your choosing. To set this value to something other than our default, use @include alert-bg(#hexcolor);

Close Box Mixin

You can include a close box with each of the alert boxes by adding an anchor right before the closing </div>. This anchor should have a class of .close in order to work with foundation.alerts.js, which contains the interaction for closing.

<%= code_example ' .your-class-name { @include alert-base; @include alert-style; .close { @include alert-close; } }', :css %>
This is base, bg and close mixins. ×
Radius Mixin

We've also created a global radius mixin that you can use to add border-radius to any element you'd like.

<%= code_example ' .your-class-name { @include alert-base; @include alert-style($success-color); @include radius($radius); }', :css %>
This is base, bg and radius mixins. ×
Default SCSS Variables
<%= code_example ' /* We use this to control alert padding. */ $alert-padding-top: emCalc(11px); $alert-padding-left: $alert-padding-top; $alert-padding-right: $alert-padding-top + emCalc(10px); $alert-padding-bottom: $alert-padding-top + emCalc(1px); /* We use these to control text style. */ $alert-font-weight: bold; $alert-font-size: emCalc(14px); $alert-font-color: #fff; $alert-font-color-alt: darken($secondary-color, 60%); /* We use this for close hover effect. */ $alert-function-factor: 10%; /* We use these to control border styles. */ $alert-border-style: solid; $alert-border-width: 1px; $alert-border-color: darken($primary-color, $alert-function-factor); $alert-bottom-margin: emCalc(20px); /* We use these to style the close buttons */ $alert-close-color: #333; $alert-close-position: emCalc(5px); $alert-close-font-size: emCalc(22px); $alert-close-opacity: 0.3; $alert-close-opacity-hover: 0.5; $alert-close-padding: 5px 4px 4px; /* We use this to control border radius */ $alert-radius: $global-radius; ', :css %>

Note: emCalc(); is a function we wrote to convert px to em.


Using the JavaScript

You don't need ths JS to create alert boxes with Foundation. The only reason you'll need to include foundation.alerts.js is if you want to add the ability to close an alert. You'll also need to make sure to include zepto.js and foundation.js above the alert plugin. Above your closing </body> tag include the following line of code and make sure you have the JS in your directory:

Read how to install Foundation JavaScript

Required Foundation Library: foundation.alerts.js

Then, you'll need to add a data-attribute to make the JS work properly on that element. That looks like:

<%= code_example '
×
', :html %>
<%= render "_sidebar-components.html.erb" %>