Questions tagged [pseudo-element]

CSS pseudo-elements allow for an enhanced display of content within elements or the ability to generate extra beginning or ending content for elements.

CSS pseudo-elements (literally, "false elements") allow for an enhanced display of content within elements or the ability to generate extra beginning or ending content for elements. It allows for many CSS properties typically only available to real elements to be applied directly to content. Pseudo-elements are not to be confused with pseudo-classes.

List of CSS Pseudo-Elements

This is a list of all pseudo-elements that are currently included in CSS specifications (not any editor's drafts or upcoming specifications).

  • ::first-line and ::first-letter
    Selects the first line or letter within the text of the element.

  • ::before and ::after
    Used to insert content before or after the content of the element using the content property. The content may be set to text and embellished by other properties, or it may be left blank by setting content: '', allowing the pseudo-element to be used to make boxes, display background images, etc., without any textual content being generated. You cannot use these pseudo-elements to insert additional HTML, and you cannot use them with replaced elements (see below) which do not have actual content. This is because the generated content resides within the element.

Replaced Elements and Other Elements not allowing Pseudo-Elements

Any element whose appearance and/or dimensions are determined by some external resource is considered to be a replaced element, and are outside the scope of the CSS formatting model. Because of this, some pseudo-elements cannot be applied to replaced elements, in large part because they have no "content" themselves or they get replaced with something (such as user interface controls). Elements replaced or not holding content include images (<img>), inline frames (<iframe>), line breaks (<br />), horizontal rules (<hr>), plugins (<object>), some form elements (<textarea>, <input>, and <select>), videos (<video>), audio sounds (<audio>), and canvases (<canvas>). Any other element is considered to be a non-replaced element, or is at least an element that contains content with which pseudo-elements may be used. For further information on replaced elements, see the HTML5 draft of the spec.

1919 questions
1012
votes
23 answers

Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)

Is there any way to select/manipulate CSS pseudo-elements such as ::before and ::after (and the old version with one semi-colon) using jQuery? For example, my stylesheet has the following rule: .span::after{ content:'foo' } How can I change 'foo'…
JBradwell
  • 10,255
  • 3
  • 13
  • 8
740
votes
20 answers

Can I use a :before or :after pseudo-element on an input field?

I am trying to use the :after CSS pseudo-element on an input field, but it does not work. If I use it with a span, it works OK. This works (puts the…
matra
  • 8,547
  • 4
  • 17
  • 27
397
votes
8 answers

CSS :not(:last-child):after selector

I have a list of elements, which are styled like this: ul { list-style-type: none; text-align: center; } li { display: inline; } li:not(:last-child):after { content:' |'; }
  • One
  • Two
Matt Clarkson
  • 12,834
  • 9
  • 53
  • 84
369
votes
7 answers

How to write :hover condition for a:before and a:after?

How to write :hover and :visited condition for a:before? I'm trying a:before:hover but it's not working
Jitendra Vyas
  • 134,556
  • 218
  • 544
  • 822
327
votes
9 answers

Is it possible to set the stacking order of pseudo-elements below their parent element?

I am trying to style a element with the :after pseudo element CSS selector #element { position: relative; z-index: 1; } #element::after { position:relative; z-index: 0; content: " "; position: absolute; width: 100px; …
adardesign
  • 29,076
  • 14
  • 59
  • 80
286
votes
14 answers

Can I change the height of an image in CSS :before/:after pseudo-elements?

Suppose I want to decorate links to certain file types using an image. I could declare my links as A File! then have CSS like .pdflink:after { content: url('/images/pdf.png') } Now, this works great, except…
Coderer
  • 21,290
  • 22
  • 71
  • 124
273
votes
8 answers

CSS: How to remove pseudo elements (after, before,...)?

I would like to use a switch for the layout of paragraph tags on a webpage. I use the after pseudoelement: p:after {content: url("../img/paragraph.gif");} Now I need to remove this CSS code from the page. How can this be done easily? I want to add…
Thariama
  • 47,807
  • 11
  • 127
  • 149
268
votes
4 answers

CSS content property: is it possible to insert HTML instead of Text?

Just wondering if it's possible somehow to make the CSS content property insert html code instead string on :before or :after an element like: .header:before{ content: 'Back'; } this would be quite handy...It could be done…
zanona
  • 11,379
  • 24
  • 78
  • 137
202
votes
1 answer

:after and :before pseudo-element selectors in Sass

How can I use the :before and :after pseudo-element selectors following the syntax of Sass or, alternatively, SCSS? Like this: p margin: 2em auto > a color: red :before content: "" :after content: "* * *" Of course, the above…
Marco Godínez
  • 3,100
  • 2
  • 17
  • 30
193
votes
3 answers

Combine :after with :hover

I want to combine :after with :hover in CSS (or any other pseudo selector). I basically have a list and the item with the selected class has an arrow shape applied using :after. I want the same to be true for objects that are being hovered over…
Chris
  • 24,827
  • 43
  • 179
  • 317
173
votes
2 answers

css rotate a pseudo :after or :before content:""

anyway to make a rotation work on the pseudo content:"\24B6"? I'm trying to rotate a unicode symbol.
devric
  • 2,922
  • 4
  • 19
  • 34
159
votes
10 answers

Using CSS :before and :after pseudo-elements with inline CSS?

I'm making an HTML email signature with inline CSS (i.e. CSS in style attributes), and I am curious as to whether it's possible to use the :before and :after pseudo-elements. If so, how would I implement something like this with inline CSS? td {…
Bah-Bee
  • 1,625
  • 2
  • 12
  • 8
140
votes
8 answers

Add line break to ::after or ::before pseudo-element content

I do not have access to the HTML or PHP for a page and can only edit via CSS. I've been doing modifications on a site and adding text via the ::after or ::before pseudo-elements and have found that escape Unicode should be used for things such as a…
elkdee
  • 1,511
  • 2
  • 10
  • 5
139
votes
2 answers

:after vs. ::after

Is there any functional difference between the CSS 2.1 :after and the CSS 3 ::after pseudo-selectors (other than ::after not being supported in older browsers)? Is there any practical reason to use the newer specification?
Parker Ault
  • 2,883
  • 3
  • 18
  • 23
130
votes
3 answers

Why don't :before and :after pseudo elements work with `img` elements?

I am trying to use a :before pseudo element with an img element. Consider this HTML and CSS... HTML CSS img:before { content: "hello"; } jsFiddle. This does not produce the…
alex
  • 438,662
  • 188
  • 837
  • 957
1
2 3
99 100