::after (:after)

  • Revision slug: CSS/::after
  • Revision title: ::after (:after)
  • Revision id: 347923
  • Created:
  • Creator: fusionchess
  • Is current revision? No
  • Comment Added example: CSS-only tooltips

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

.boringtext::after {
   content:    " Thank you for reading all this!"; 
   font-size:  small; 
   color:      red;
   background: gray; 
}

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.

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CSS tooltips</title>
<style type="text/css">
    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;
    }
</style>
</head>
 
<body>

    <p>This is an example <span data-descr="collection of words and punctuation">text</span> with a few <span data-descr="small popups which also hide again">tooltips</span>. Take a <span data-descr="not to be taken literally">look</span>...
    </p>

</body>
</html>

View this example in action

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>Simple usage</h4>
<pre class="brush: css">
.boringtext::after {
   content:    " Thank you for reading all this!"; 
   font-size:  small; 
   color:      red;
   background: gray; 
}</pre>
<h4>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>.</p>
<pre class="brush: html">
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;CSS tooltips&lt;/title&gt;
&lt;style type="text/css"&gt;
&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;
&nbsp;&nbsp; &nbsp;}
&lt;/style&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;

&nbsp;&nbsp; &nbsp;&lt;p&gt;This is an example &lt;span data-descr="collection of words and punctuation"&gt;text&lt;/span&gt; with a few &lt;span data-descr="small popups which also hide again"&gt;tooltips&lt;/span&gt;. Take a &lt;span data-descr="not to be taken literally"&gt;look&lt;/span&gt;...
&nbsp;&nbsp; &nbsp;&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<p><a href="https://developer.mozilla.org/files/4591/css-only_tooltips.html" title="css-only_tooltips.html">View this example in action</a></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>
<p>{{ Cssxref(":before") }}, {{ cssxref("content") }}</p>
<p>&nbsp;</p>
Revert to this revision