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.

2189 questions
1129
votes
28 answers

Selecting and manipulating CSS pseudo-elements such as ::before and ::after using JavaScript

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
  • 11,433
  • 3
  • 15
  • 8
856
votes
21 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
  • 9,763
  • 4
  • 20
  • 31
464
votes
11 answers

Is there a way to use SVG as content in a pseudo element ::before or ::after

I would like to use ::before to place SVG images before some selected elements: #mydiv::before { content: ''; display: block; width: 22px; height: 10px; margin: 10px 5px 0 10px; } Above code just displays the…
Sunny
  • 9,245
  • 10
  • 49
  • 79
420
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
  • 14,106
  • 10
  • 57
  • 85
406
votes
7 answers

How can I write a ':hover' condition for 'a:before' and 'a:after'?

How can I write :hover and :visited condition for a:before? I'm trying a:before:hover, but it's not working.
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852
386
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
  • 33,973
  • 15
  • 62
  • 84
359
votes
16 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
  • 25,844
  • 28
  • 99
  • 154
313
votes
9 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
  • 50,002
  • 13
  • 138
  • 166
293
votes
5 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
  • 12,345
  • 25
  • 86
  • 141
232
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,440
  • 2
  • 21
  • 30
228
votes
4 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
  • 26,744
  • 48
  • 193
  • 345
201
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
  • 3,555
  • 4
  • 22
  • 36
196
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,995
  • 2
  • 13
  • 8
166
votes
5 answers

Can I have multiple :before pseudo-elements for the same element?

Is it possible to have multiple :before pseudos for the same element? .circle:before { content: "\25CF"; font-size: 19px; } .now:before{ content: "Now"; font-size: 19px; color: black; } I am trying to apply the above styles to…
ChrisOdney
  • 6,066
  • 10
  • 38
  • 48
162
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,731
  • 2
  • 11
  • 5
1
2 3
99 100