Revision Content

{{ CSSRef() }}

Summary

::before creates a pseudo-element that is the first child of the element matched. Often used to add cosmetic content to an element, by using the {{ cssxref("content") }} property. This element is inline by default.

Here is the above code example running live. Note that there are no icons used, and the checkmark as actually the ::before that has been styled in CSS. Go ahead and get some stuff done.

{{ EmbedLiveSample('To-do_list', '400', '300', '') }}

Notes

Although the positioning fixes in Firefox 3.5 do not allow content to be generated as a separate previous sibling (as per the CSS spec stating "The :before and :after pseudo-elements elements interact with other boxes... as if they were real elements inserted just inside their associated element."), they can be used to provide a slight improvement on tableless layouts (e.g., to achieve centering) in that, as long as the content to be centered is wrapped in a further child, a column before and after the content can be introduced without adding a previous or following sibling (i.e., it is perhaps more semantically correct to add an additional span as below, than it would to add an empty <div/> before and after). (And always remember to add a width to a float, since, otherwise, it will not float!)

<style type="text/css">
#floatme { float: left; width: 50%; }
.example:before {
content: "Floated Before"; /* To get an empty column, just indicate a hex code for a non-breaking space: \a0 as the content (use \0000a0 when following such a space with other characters) */
float: left;
width: 25%
}
.example:after {
content: "Floated After";
float: right;
width:25%
}
/* For styling */
.example:before, .example:after, .first {
background: yellow;
color: red;
}
</style>
<div class="example">
<span id="floatme">"Floated Before" should be generated on the left of the
viewport and not allow overflow in this line to flow under it. Likewise
should "Floated After" appear on the right of the viewport and not allow this
line to flow under it.</span>
</div>

[1] Firefox prior to version 3.5 only implemented the CSS 2.0 version of :before. Not allowed were {{cssxref("position")}}, {{cssxref("float")}}, list-style-* and some display properties. Firefox 3.5 removed those restrictions.