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
153
votes
4 answers

Using :before CSS pseudo element to add image to modal

I have a CSS class Modal which is absolutely positioned, z-indexed above it's parent, and nicely positioned with JQuery. I want to add a caret image (^) to the top of the modal box and was looking at using the :before CSS pseudo selector to do this…
RSG
  • 7,013
  • 6
  • 36
  • 51
152
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
  • 3,268
  • 3
  • 22
  • 24
144
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
  • 479,566
  • 201
  • 878
  • 984
119
votes
8 answers

What is the difference between a pseudo-class and a pseudo-element in CSS?

Things like a:link or div::after... Information on the difference seems scarce.
tybro0103
  • 48,327
  • 33
  • 144
  • 170
117
votes
8 answers

Editing input type="search" Pseudo-element Button ('x')

I'm trying to make a search bar that will look nice. What I did is, I made an image of an search bar and I'm adding the image to the back-ground of the input and I'm editing the place and the size that the font will appear. The only thing that I…
user3139952
  • 1,171
  • 2
  • 8
  • 4
117
votes
6 answers

Should I use single or double colon notation for pseudo-elements?

Since IE7 and IE8 don't support the double-colon notation for pseudo-elements (e.g. ::after or ::first-letter), and since modern browsers support the single-colon notation (e.g. :after) for backwards compatibility, should I use solely the…
113
votes
8 answers

CSS pseudo elements in React

I'm building React components. I have added CSS inline in the components as suggested in this brilliant presentation by one of the people behind React. I've been trying all night to find a way to add CSS pseudo classes inline, like on the slide…
Michael Johansen
  • 4,688
  • 5
  • 29
  • 47
112
votes
5 answers

CSS :after not adding content to certain elements

I'm having trouble understanding the behavior of the CSS :after property. According to the spec (here and here): As their names indicate, the :before and :after pseudo-elements specify the location of content before and after an element's document…
eykanal
  • 26,437
  • 19
  • 82
  • 113
111
votes
6 answers

How to remove only underline from a:before?

I have a set of styled links using the :before to apply an arrow. It looks good in all browser, but when I apply the underline to the link, I don't want to have underline on the :before part (the arrow). See jsfiddle for example:…
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
105
votes
8 answers

Changing CSS pseudo-element styles via JavaScript

Is it possible to change a CSS pseudo-element style via JavaScript? For example, I want to dynamically set the color of the scrollbar like so: document.querySelector("#editor::-webkit-scrollbar-thumb:vertical").style.background =…
木川 炎星
  • 3,893
  • 13
  • 42
  • 51
98
votes
2 answers

How do I add / insert a before or after pseudo element into Chrome's Inspector?

I can add a regular style rule via the + sign (New Style Rule) but I can't add one under the "Pseudo ::before Element" or "Pseudo ::after Element" sections of the Style Inspector. If I try to add the ::before or ::after element into the HTML via…
Andrew Tibbetts
  • 2,874
  • 3
  • 23
  • 28
93
votes
4 answers

Apply webkit scrollbar style to specified element

I am new to pseudo-elements that are prefixed with a double colon. I came across a blog article discussing styling of scrollbars using some webkit only css. Can the pseudo-element CSS be applied to individual elements? /* This works by applying…
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
93
votes
8 answers

width and height doesn't seem to work on :before pseudo-element

Here is a fiddle.

foo bar baz

and a.infolink::before { content: '?'; background: blue; color: white; width: 20ex; height: 20ex; } The '?' appears but clearly does not have 20ex size. Why…
spraff
  • 32,570
  • 22
  • 121
  • 229
89
votes
2 answers

Why are double quotes shown only for the first element?

I am wondering why the browser shows double open quotes only for the first element. The second element has single quotes instead. a::before { content: open-quote; } Google
Pavan Tiwari
  • 3,077
  • 3
  • 31
  • 71
88
votes
4 answers

Change SVG fill color in :before or :after CSS

I have a SVG graphic put like this: a::before { content: url(filename.svg); } When I hover over the tag, I really want the SVG to change the fill color, without loading a new SVG file, as I have now: a:hover::before { content:…
mnsth
  • 2,169
  • 1
  • 18
  • 22