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
86
votes
2 answers

What is the difference between pseudo-classes and pseudo-elements?

What is the difference between div::after {} and div:after {} ? When do we have to use :: over :? Double colon and single-colon notation is to distinguish between pseudo-classes and pseudo-elements. What is the actual meaning of the above…
Mizuki
  • 2,153
  • 1
  • 17
  • 29
80
votes
2 answers

Why isn't it possible to combine vendor-specific pseudo-elements/classes into one rule set?

In CSS it is possible to style placeholder text within an input using a combination of vendor-specific pseudo-classes and pseudo-elements (to get the best cross-browser coverage). These all share the same basic properties (ie: text styling and color…
johnkavanagh
  • 4,614
  • 2
  • 25
  • 37
77
votes
4 answers

How to make a hover effect for pseudo elements?

I have a this setup:
Button
and this for CSS: #button { color: #fff; display: block; height: 25px; margin: 0 10px; padding: 10px; text-indent: 20px; width: 12%; } #button:before { …
chasethesunnn
  • 2,149
  • 5
  • 25
  • 42
77
votes
6 answers

How to get pseudo element?

I need to get :after and assign it to variable. It is possible? querySelectorAll doesn't work. alert(some_div_with_pseudo.querySelectorAll('::after')[0]) // undefined
A. Petrov
  • 902
  • 1
  • 6
  • 6
77
votes
4 answers

Can you add line breaks to the :after pseudo element?

I want to append a
to a particular class. Using the :after pseudo class simply displays
as text. Is there a way to make this work? It's internal for IE8 so browser issues aren't a problem. If I do cats…
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
74
votes
6 answers

Vertically centering content of :before/:after pseudo-elements

I'm trying to achieve something similar to this picture: I have an image (as part of a slideshow) wrapped in a div, and with :before and :after pseudo-elements, I display two controls to move onto the next (>>) or previous (<<) images of the…
dcastro
  • 66,540
  • 21
  • 145
  • 155
66
votes
12 answers

Center a Pseudo Element

First time really using the pseudo :after selector. Not sure if the problem I'm running into is a limitation of it or I'm just missing something obvious. Here's my live code. li.current:after { border-width: 1px 1px 0 0; content: ' '; …
EnigmaRM
  • 7,523
  • 11
  • 46
  • 72
65
votes
5 answers

Event listener on a CSS pseudo-element, such as ::after and ::before?

I have a div element with a CSS pseudo-element ::before used as a close button (instead of using an actual button). How do I apply an event listener to only the pseudo-element? HTML
CSS #box:before { background-image:…
jenswirf
  • 7,087
  • 11
  • 45
  • 65
65
votes
2 answers

How do I override CSS set on a pseudo element?

I know that has been asked before, but I find no clean way of overriding this CSS: .ui-input-search:after { content: ""; height: 18px; left: 0.3125em; margin-top: -9px; opacity: 0.5; position: absolute; top: 50%; …
frequent
  • 27,643
  • 59
  • 181
  • 333
64
votes
1 answer

CSS :before on inline SVG

Update Thanks porneL for pointing out the relation between generated content and replaced elements. I found this article which deals with this very topic: Interestingly enough, a W3C document titled "CSS3 Generated and Replaced Content Module"…
danbal
  • 1,481
  • 3
  • 14
  • 19
62
votes
3 answers

Pseudo elements and SELECT tag

Does the select tag allows to use the :after selector in order to create a pseudo element after it? I've tried on Chrome, Safari and Firefox on Mac and it doesn't seem to work.
Patrick D'appollonio
  • 2,722
  • 1
  • 18
  • 34
61
votes
11 answers

How to close the new html tag by clicking on its ::backdrop

I didn't find any built in solution or workaround for closing the html5 element by clicking on its background(::backdrop), although it's clearly a basic functionality.
mantramantramantra
  • 663
  • 1
  • 5
  • 8
61
votes
11 answers

“text-decoration” and the “:after” pseudo-element, revisited

I'm re-asking this question because its answers didn't work in my case. In my stylesheet for printed media I want to append the url after every link using the :after pseudo-class. a:after { content: " <" attr(href) ">"; text-decoration:…
palm3D
  • 7,970
  • 6
  • 28
  • 33
60
votes
4 answers

Can a ::before selector be used with a