Questions tagged [css-content]

CSS Generated Content allows us to insert and move content around a document. We can use this to create footnotes, endnotes, and section notes, as well as counters and strings, which can be used for running headers and footers, section numbering, and lists.

CSS Generated content, automatic numbering, and lists

CSS Generated Content allows us to insert and move content around a document. We can use this to create footnotes, endnotes, and section notes, as well as counters and strings, which can be used for running headers and footers, section numbering, and lists.

Authors specify the style and location of generated content with the :before and :after pseudo-elements. As their names indicate, the :before and :after pseudo-elements specify the location of content before and after an element's document tree content. The content property, in conjunction with these pseudo-elements, specifies what is inserted.

For example, the following rule inserts the string "Note: " before the content of every P element whose "class" attribute has the value "note":

p.note:before {
    content: "Note: ";
}

Examples

/* Keywords that cannot be combined with other values */
content: normal;
content: none;

/* <url> value */
content: url("http://www.example.com/test.png");

/* <image> value */
content: linear-gradient(#e66465, #9198e5);

/* values below can only be applied to generated content using ::before and ::after */

/* <string> value */ 
content: "prefix";

/* <counter> values */
content: counter(chapter_counter);
content: counters(section_counter, ".");

/* attr() value linked to the HTML attribute value */
content: attr(value string);

/* Language- and position-dependent keywords */
content: open-quote;
content: close-quote;
content: no-open-quote;
content: no-close-quote;

/* Except for normal and none, several values
   can be used simultaneously */
content: open-quote chapter_counter;

/* Global values */
content: inherit;
content: initial;
content: unset;

Resource

177 questions
1124
votes
10 answers

Adding HTML entities using CSS content

How do you use the CSS content property to add HTML entities? Using something like this just prints   to the screen instead of the non-breaking space: .breadcrumbs a:before { content: ' '; }
nickf
  • 537,072
  • 198
  • 649
  • 721
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
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
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
146
votes
4 answers

How can I add white space before an element's content using CSS?

None of the following code works: p:before { content: " "; } p:before { content: " "; } How do I add white space before an element's content? Note: I need to color the border-left and the margin-left for semantic use and use the space as a…
Hugolpz
  • 17,296
  • 26
  • 100
  • 187
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
140
votes
10 answers

Vertically aligning CSS :before and :after content

I am trying to centre the link with the image, but can't seem to move the content vertically in any way.

More Information

File Name The icon is 22 x 22px .pdf { font-size: 12px; } .pdf:before { …
theorise
  • 7,245
  • 14
  • 46
  • 65
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
58
votes
3 answers

CSS:after encoding characters in content

I am using the following CSS, which seems to be working: a.up:after{content: " ↓";} a.down:after{content: " ↑";} The characters however cannot seem to be encoded like this, as the output is literal and shows the actual…
theorise
  • 7,245
  • 14
  • 46
  • 65
51
votes
1 answer

CSS Pseudo Element Counters: can you increment an alphabet letter "a", "b", "c", etc instead of a number?

As defined here: http://www.w3.org/TR/CSS21/generate.html#propdef-counter-increment You can use code like the following to increment numbers in pseudo elements. H1:before { content: "Chapter " counter(chapter) ". "; counter-increment:…
HandiworkNYC.com
  • 10,914
  • 25
  • 92
  • 154
51
votes
5 answers

CSS data attribute new line character & pseudo-element content value

Is it possible to have a new line in a data attribute ? I am trying to do something like this: CSS: [data-foo]:after { content: attr(data-foo); background-color: black; } HTML
foo
I found…
Snaker.Wood
  • 609
  • 2
  • 7
  • 13
37
votes
3 answers

Is it possible to select css generated content?

Let's say I have mark up:
Hello
..with CSS: div:after { content: attr(data-generated); } This produces the text: Hello world! - FIDDLE div:after { content: attr(data-generated); }
Danield
  • 121,619
  • 37
  • 226
  • 255
31
votes
3 answers

Why do I need an empty `content` property on an ::after pseudo-element?

I got http://jsfiddle.net/8p2Wx/2/ from a previous question I asked and I see these lines: .cf:before, .cf:after { content:""; display:table; } .cf:after { clear:both; } If I take away content:"", it ruins the effect, and I don't…
qwertymk
  • 34,200
  • 28
  • 121
  • 184
29
votes
5 answers

Why do the :before and :after pseudo-elements require a 'content' property?

Given the following scenario, why does the :after selector require a content property to function? .test { width: 20px; height: 20px; background: blue; position:relative; } .test:after { width: 20px; height:…
D_C
  • 926
  • 3
  • 11
  • 28
23
votes
2 answers

Is it somehow possible to style an iframes before/after pseudo-element?

As the title says, is there a way to style an iframes pseudo before/after? Without wrapping the iframe with another div, or else?` I tried to style it like any other element, but no success: iframe::before { content: 'foo'; position:…
yckart
  • 32,460
  • 9
  • 122
  • 129
1
2 3
11 12