::after (:after)

  • Revision slug: Web/CSS/::after
  • Revision title: ::after (:after)
  • Revision id: 472547
  • Created:
  • Creator: Tripad
  • Is current revision? No
  • Comment Added new example

Revision Content

{{ CSSRef() }}

Summary

The CSS :after pseudo-element matches a virtual last child of the selected element. Typically used to add cosmetic content to an element, by using the {{ cssxref("content") }} CSS property. This element is inline by default.

Syntax

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

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

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

Examples

Simple usage

Let's create two classes, one for boring paragraphs and one for exciting ones. We can then mark each paragraph by adding a psuedo element to the end of it.

<p class="boring-text">Here is some good old boring text.</p>
<p>Here is some moderate text that is not either boring or exciting.</p>
<p class="exciting-text">Contributing to MDN is easy and fun.
Just hit the edit button to add new live samples, or improve existing samples.</p>
.exciting-text::after {
   content:    "<- now this *is* exciting!"; 
   color:      green;
}

.boring-text::after {
   content:    "<- BORING!"; 
   color:      red;
}

{{ EmbedLiveSample('Simple_usage', '500', '200', '') }}

 

Sample Example 2

.ribbon{
    position:relative;
    width:50px;
    height:50px;
    background-color: #3BA8E7;
}
.ribbon:after{
    position:absolute;
    content: " ";
    width:50px;
    height:50px;
    top:20%;
    left:50%;
    background-color: #1578B1;
}

Live Sample : http://jsfiddle.net/Tripad/fbQTr/3/

Tooltips

The following example shows the use of the ::after pseudo-element in conjunction with the attr() CSS expression and a data-descr custom data attribute to create a pure-CSS, glossary-like tooltip. Checkout the live preview below, or you can see this example in a separate page.

<p>Here is the live example of the above code.<br />
  We have some <span data-descr="collection of words and punctuation">text</span> here with a few
  <span data-descr="small popups which also hide again">tooltips</span>.<br />
  Dont be shy, hove over to take a <span data-descr="not to be taken literally">look</span>.
</p>
    span[data-descr] {
        position: relative;
        text-decoration: underline;
        color: #00F;
        cursor: help;
    }

    span[data-descr]:hover::after {
        content: attr(data-descr);
        position: absolute;
        left: 0;
        top: 24px;
        min-width: 200px;
        border: 1px #aaaaaa solid;
        border-radius: 10px;
        background-color: #ffffcc;
        padding: 12px;
        color: #000000;
        font-size: 14px;
        z-index: 1;
    }

{{ EmbedLiveSample('Tooltips', '600', '150', '') }}

 

Specifications

Specification Status Comment
{{ Specname("CSS3 Transitions", "#animatable-properties", "") }} {{ Spec2("CSS3 Transitions")}} Allows transitions on properties defined on pseudo-elements.
{{ Specname("CSS3 Animations", "", "") }} {{ Spec2("CSS3 Animations")}} Allows animations on properties defined on pseudo-elements.
{{ SpecName('CSS3 Selectors', '#gen-content', '::after') }} {{ Spec2('CSS3 Selectors') }} Introduces the two-colon syntax.
{{ SpecName('CSS2.1', 'generate.html#before-after-content', '::after') }} {{ Spec2('CSS2.1') }} Initial definition, using the one-colon syntax

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
:after support {{ CompatVersionUnknown() }} {{ CompatGeckoDesktop("1.0") }} [1] 8.0 {{ CompatOpera("4") }} 4.0
::after support {{ CompatVersionUnknown() }} 1.5 [1] 9.0 {{ CompatOpera("7") }} 4.0
Support of animations and transitions 26 {{ CompatGeckoDesktop("2.0") }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
:after support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
::after support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Support of animations and transitions 26 {{ CompatGeckoMobile("4.0") }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}

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

See Also

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

 

Revision Source

<div>
  {{ CSSRef() }}</div>
<h2 id="Summary">Summary</h2>
<p>The CSS <code>:after</code> <a href="/en/CSS/Pseudo-elements" title="Pseudo-elements">pseudo-element</a> matches a virtual last child of the selected element. Typically used to add cosmetic content to an element, by using the {{ cssxref("content") }} CSS property. This element is inline by default.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
element:after  { <em>style properties</em> }  /* CSS2 syntax */

element::after { <em>style properties</em> }  /* CSS3 syntax */</pre>
<p>The<code> ::after </code>notation was introduced in CSS 3 in order to establish a discrimination between <a href="/en/CSS/Pseudo-classes" title="Pseudo-classes">pseudo-classes</a> and pseudo-elements. Browsers also accept the notation<code> :after </code>introduced in CSS 2.</p>
<h2 id="Examples">Examples</h2>
<h4 id="Simple_usage">Simple usage</h4>
<p>Let's create two classes, one for boring paragraphs and one for exciting ones. We can then mark each paragraph by adding a psuedo element to the end of it.</p>
<pre class="brush: html">
&lt;p class="boring-text"&gt;Here is some good old boring text.&lt;/p&gt;
&lt;p&gt;Here is some moderate text that is not either boring or exciting.&lt;/p&gt;
&lt;p class="exciting-text"&gt;Contributing to MDN is easy and fun.
Just hit the edit button to add new live samples, or improve existing samples.&lt;/p&gt;</pre>
<pre class="brush: css">
.exciting-text::after {
   content:    "&lt;- now this *is* exciting!"; 
   color:      green;
}

.boring-text::after {
   content:    "&lt;- BORING!"; 
   color:      red;
}</pre>
<p>{{ EmbedLiveSample('Simple_usage', '500', '200', '') }}</p>
<p>&nbsp;</p>
<h4>Sample Example 2</h4>
<pre class="brush: css">
.ribbon{
&nbsp;&nbsp;&nbsp; position:relative;
&nbsp;&nbsp;&nbsp; width:50px;
&nbsp;&nbsp;&nbsp; height:50px;
&nbsp;&nbsp;&nbsp; background-color: #3BA8E7;
}
.ribbon:after{
&nbsp;&nbsp;&nbsp; position:absolute;
&nbsp;&nbsp;&nbsp; content: " ";
&nbsp;&nbsp;&nbsp; width:50px;
&nbsp;&nbsp;&nbsp; height:50px;
&nbsp;&nbsp;&nbsp; top:20%;
&nbsp;&nbsp;&nbsp; left:50%;
&nbsp;&nbsp;&nbsp; background-color: #1578B1;
}
</pre>
<p><strong>Live Sample </strong>: <a href="http://jsfiddle.net/Tripad/fbQTr/3/">http://jsfiddle.net/Tripad/fbQTr/3/</a></p>
<h4 id="Tooltips">Tooltips</h4>
<p>The following example shows the use of the <code>::after</code> <a href="/en/CSS/Pseudo-elements" title="Pseudo-elements">pseudo-element</a> in conjunction with the <a href="/en-US/docs/CSS/attr" title="/en-US/docs/CSS/attr"><code>attr()</code></a> CSS expression and a <code>data-descr</code> <a href="/en/HTML/Global_attributes#attr-data-*" title="en/HTML/Global_attributes#attr-data-*">custom data attribute</a> to create a pure-CSS, glossary-like <em>tooltip</em>. Checkout the live preview below, or you can see this example in a <a href="https://developer.mozilla.org/files/4591/css-only_tooltips.html" title="css-only_tooltips.html">separate page.</a></p>
<pre class="brush: html">
&lt;p&gt;Here is the live example of the above code.&lt;br /&gt;
  We have some &lt;span data-descr="collection of words and punctuation"&gt;text&lt;/span&gt; here with a few
  &lt;span data-descr="small popups which also hide again"&gt;tooltips&lt;/span&gt;.&lt;br /&gt;
  Dont be shy, hove over to take a &lt;span data-descr="not to be taken literally"&gt;look&lt;/span&gt;.
&lt;/p&gt;
</pre>
<pre class="brush: css">
&nbsp;&nbsp; &nbsp;span[data-descr] {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;position: relative;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;text-decoration: underline;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;color: #00F;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;cursor: help;
&nbsp;&nbsp; &nbsp;}

&nbsp;&nbsp; &nbsp;span[data-descr]:hover::after {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;content: attr(data-descr);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;position: absolute;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;left: 0;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;top: 24px;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;min-width: 200px;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;border: 1px #aaaaaa solid;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;border-radius: 10px;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;background-color: #ffffcc;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;padding: 12px;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;color: #000000;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;font-size: 14px;
        z-index: 1;
&nbsp;&nbsp; &nbsp;}</pre>
<p>{{ EmbedLiveSample('Tooltips', '600', '150', '') }}</p>
<p>&nbsp;</p>
<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 Transitions", "#animatable-properties", "") }}</td>
      <td>{{ Spec2("CSS3 Transitions")}}</td>
      <td>Allows transitions on properties defined on pseudo-elements.</td>
    </tr>
    <tr>
      <td>{{ Specname("CSS3 Animations", "", "") }}</td>
      <td>{{ Spec2("CSS3 Animations")}}</td>
      <td>Allows animations on properties defined on pseudo-elements.</td>
    </tr>
    <tr>
      <td>{{ SpecName('CSS3 Selectors', '#gen-content', '::after') }}</td>
      <td>{{ Spec2('CSS3 Selectors') }}</td>
      <td>Introduces the two-colon syntax.</td>
    </tr>
    <tr>
      <td>{{ SpecName('CSS2.1', 'generate.html#before-after-content', '::after') }}</td>
      <td>{{ Spec2('CSS2.1') }}</td>
      <td>Initial definition, using the one-colon 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>:after</code> support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatGeckoDesktop("1.0") }} [1]</td>
        <td>8.0</td>
        <td>{{ CompatOpera("4") }}</td>
        <td>4.0</td>
      </tr>
      <tr>
        <td><code>::after</code> support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>1.5 [1]</td>
        <td>9.0</td>
        <td>{{ CompatOpera("7") }}</td>
        <td>4.0</td>
      </tr>
      <tr>
        <td>Support of animations and transitions</td>
        <td>26</td>
        <td>{{ CompatGeckoDesktop("2.0") }}</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</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>:after</code> support</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>::after</code> support</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td>Support of animations and transitions</td>
        <td>26</td>
        <td>{{ CompatGeckoMobile("4.0") }}</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>[1] Firefox prior to version 3.5 only implemented the CSS&nbsp;2.0 version of <code>:after</code>. Not allowed were {{cssxref("position")}}, {{cssxref("float")}}, <code>list-style-*</code> and some display properties. Firefox 3.5 removed those restrictions.</p>
<h2 id="See_Also">See Also</h2>
<ul>
  <li>{{ Cssxref("::before") }}, {{ cssxref("content") }}</li>
</ul>
<p>&nbsp;</p>
Revert to this revision