::before (:before)

  • Revision slug: CSS/::before
  • Revision title: ::before (:before)
  • Revision id: 343953
  • Created:
  • Creator: HMax13
  • Is current revision? No
  • Comment

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.

{{ fx_minversion_note("3.5", "Firefox prior to version 3.5 only implemented the CSS 2.0 version of :before. Not allowed were position, float, list-style-* and some display properties. Firefox 3.5 removed those restrictions. (Note this is independent from the :: notation issue mentioned below.)") }}

Syntax

element:before  { style properties }  /* CSS2 syntax */

element::before { style properties }  /* CSS3 syntax */

The ::before notation was introduced in CSS 3 in order to establish a discrimination between pseudo-classes and pseudo-elements. Browsers also accept the notation :before introduced in CSS 2.

Note:  Microsoft Internet Explorer 8 only supports the :before notation.

Examples

q:before { content: "»"; }
q:after { content: '«'; }
<q>Some quotes</q>, he said, <q>are better than none</q>.

Result:    »Some quotes«, he said, »are better than none«.

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>

Specifications

Specification Status Comment
{{ SpecName('CSS3 Selectors', '#gen-content', '::before') }} {{ Spec2('CSS3 Selectors') }} Use the two-colon syntax.
{{ SpecName('CSS2.1', 'generate.html#before-after-content', '::before') }} {{ Spec2('CSS2.1') }} Initial definition, using the one-colo syntax

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
:before support {{ CompatVersionUnknown() }} {{ CompatGeckoDesktop("1.0") }} 8.0 {{ CompatOpera("4") }} 4.0
::before support {{ CompatVersionUnknown() }} 1.5 9.0 {{ CompatOpera("7") }} 4.0
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
:before support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
::before support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

See also

  • {{ Cssxref(":after") }}, {{ cssxref("content") }}

Revision Source

<div>
  {{ CSSRef() }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p><code>:before </code>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.</p>
<p>{{ fx_minversion_note("3.5", "Firefox prior to version 3.5 only implemented the CSS&nbsp;2.0 version of :before. Not allowed were position, float, list-style-* and some display properties. Firefox 3.5 removed those restrictions. (Note this is independent from the :: notation issue mentioned below.)") }}</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
element:before  { <em>style properties</em> }  /* CSS2 syntax */

element::before { <em>style properties</em> }  /* CSS3 syntax */
</pre>
<p>The<code> ::before </code>notation was introduced in CSS 3 in order to establish a discrimination between pseudo-classes and pseudo-elements. Browsers also accept the notation<code> :before </code>introduced in CSS 2.</p>
<div class="note">
  <strong>Note:</strong>&nbsp; Microsoft Internet Explorer 8 only supports the<code> :before </code>notation.</div>
<h2 id="Examples">Examples</h2>
<pre class="brush:css">
q:before { content: "»"; }
q:after { content: '«'; }</pre>
<pre class="brush:html">
&lt;q&gt;Some quotes&lt;/q&gt;, he said, &lt;q&gt;are better than none&lt;/q&gt;.</pre>
<p>Result:&nbsp; &nbsp; »Some quotes«, he said, »are better than none«.</p>
<h2 id="Notes" name="Notes">Notes</h2>
<p>Although the positioning fixes in Firefox 3.5 do not allow content to be generated as a separate previous sibling (as per the <a class="external" href="http://www.w3.org/TR/CSS21/generate.html#before-after-content">CSS spec</a> 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)&nbsp;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 &lt;div/&gt; before and after). (And always remember to add a width to a float, since, otherwise, it will not float!)</p>
<pre class="brush: html">
&lt;style type="text/css"&gt;

#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;
}


&lt;/style&gt;
&lt;div class="example"&gt;
&lt;span id="floatme"&gt;"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.&lt;/span&gt;
&lt;/div&gt;
</pre>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{ SpecName('CSS3 Selectors', '#gen-content', '::before') }}</td>
      <td>{{ Spec2('CSS3 Selectors') }}</td>
      <td>Use the two-colon syntax.</td>
    </tr>
    <tr>
      <td>{{ SpecName('CSS2.1', 'generate.html#before-after-content', '::before') }}</td>
      <td>{{ Spec2('CSS2.1') }}</td>
      <td>Initial definition, using the one-colo syntax</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td><code>:before</code> support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatGeckoDesktop("1.0") }}</td>
        <td>8.0</td>
        <td>{{ CompatOpera("4") }}</td>
        <td>4.0</td>
      </tr>
      <tr>
        <td><code>::before</code> support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>1.5</td>
        <td>9.0</td>
        <td>{{ CompatOpera("7") }}</td>
        <td>4.0</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td><code>:before</code> support</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>::before</code> support</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>{{ Cssxref(":after") }}, {{ cssxref("content") }}</li>
</ul>
Revert to this revision