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
128
votes
3 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
  • 4,805
  • 10
  • 35
  • 44
127
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
  • 6,555
  • 6
  • 31
  • 50
110
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…
103
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
  • 13,936
  • 12
  • 52
  • 91
103
votes
4 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
  • 23,724
  • 17
  • 75
  • 107
98
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
  • 43,805
  • 32
  • 138
  • 167
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
  • 2,716
  • 2
  • 25
  • 56
86
votes
7 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 guys behind React. I've been trying all night to find a way to add CSS pseudo classes inline, like on the slide titled…
Michael Johansen
  • 3,702
  • 2
  • 20
  • 34
83
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
  • 37,770
  • 19
  • 83
  • 106
78
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,454
  • 2
  • 19
  • 27
76
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,416
  • 2
  • 23
  • 37
75
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,433
  • 9
  • 35
  • 49
73
votes
3 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
  • 46,891
  • 62
  • 180
  • 311
70
votes
6 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
  • 29,265
  • 19
  • 105
  • 197
66
votes
5 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
  • 661
  • 1
  • 5
  • 4