156

Is it possible to use newline character in CSS content property to force a line break? Something like:

figcaption:before
{
    content: 'Figure \n' + attr(title);
}
Salman A
  • 262,204
  • 82
  • 430
  • 521
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • 2
    Does this answer help? http://stackoverflow.com/a/4609491/582278 – Dan Blows Jan 30 '12 at 11:22
  • @Blowski, yeah, but it doesn't work in my [fiddle](http://jsfiddle.net/saeedneamati/q4WC4/) – Saeed Neamati Jan 30 '12 at 11:28
  • @SaeedNeamati does in mine. What browser are you looking in? – Dan Blows Jan 30 '12 at 11:31
  • 6
    Now it works. The last [fiddle](http://jsfiddle.net/saeedneamati/q4WC4/3/) – Saeed Neamati Jan 30 '12 at 11:32
  • 1
    This question is not a duplicate of [the question indicated](http://stackoverflow.com/q/7363766/17300). That one asks how to create a break _before_ starting the generated content, which can be done using `display`, `clear` etc. _This_ question asks how to put newlines/line-breaks _**within**_ the generated content, which is what I want, and that _can't_ be accomplished the same way. – Stephen P Aug 19 '15 at 23:29

2 Answers2

178
figcaption:before
{
    content: 'Figure \a' attr(title);
    white-space: pre;
}

Note that in the content attribute value, concatenation is expressed just by whitespace, not by a “+” sign. The escape notation \a in a CSS string literal indicates a linebreak character.

Volker E.
  • 5,911
  • 11
  • 47
  • 64
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • 1
    Still not working. Updated [fiddle](http://jsfiddle.net/saeedneamati/q4WC4/2/) – Saeed Neamati Jan 30 '12 at 11:30
  • 18
    The `white-space: pre` was missing. See http://jsfiddle.net/FP5gX/ where I added `' '` as you probably want that space. – Jukka K. Korpela Jan 30 '12 at 11:49
  • @Jan should we use `::before` or `:before`? – Ormoz Sep 15 '14 at 13:27
  • @Ormoz it definitely needs to be `:before`. You were probably confused by Dave Jarvis' edit of the accepted answer, which used bad syntax and didn't work anyway. I reverted that edit. – Sygmoral Jan 16 '15 at 13:21
  • 5
    I found "white-space: pre-wrap;" to work even better. – Jeremy Haile Sep 30 '16 at 19:30
  • For longer content which may overflow the container, I suggest you replace `white-space: pre;` with `white-space: pre-line;` – Muleskinner Oct 05 '17 at 09:55
  • 2
    I want to point out that you should use the double colon notation that was introduced with CSS3: https://developer.mozilla.org/en-US/docs/Web/CSS/::before since this is a pseudo element and not a pseudo selector. – Martin Braun Feb 25 '19 at 23:16
  • But how do you specify a newline character in the attribute that is used by the content in css? I believe that was also part of the main question? \A or \n does not work then, they are just displayed literally as text. – Johncl Jan 26 '23 at 07:21
153

The content property accepts a string and:

A string cannot directly contain a newline. To include a newline in a string, use an escape representing the line feed character in ISO-10646 (U+000A), such as "\A" or "\00000a". This character represents the generic notion of "newline" in CSS.

The tricky bit is to remember that HTML collapses white-space by default.

figure {
    /* Avoid whitespace collapse to illustrate what works and what doesn't */
    white-space: pre-wrap;
}

#first figcaption:before
{
    /* \n is not a valid entity in this context */
    content: 'Figure \n Chemistry';
    display: block;  
}

#second figcaption:before
{
    content: 'Figure \A Chemistry';
    display: block;  
}
<figure id='first'>
    <figcaption>Experiments</figcaption>
</figure>

<figure id='second'>
    <figcaption>Experiments</figcaption>
</figure>

You can check Using character escapes in markup and CSS for reference about the escape syntax, which essentially is:

  • \20AC must be followed by a space if the next character is one of a-f, A-F, 0-9
  • \0020AC must be 6 digits long, no space needed (but can be included)

NOTE: use \00000a rather than just \A when escaping an arbitrary string, because if the newline is followed by a number or any character from [a-f] range, this may give an undesired result.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 39
    Note that newline characters inserted this way are treated like any other newlines in an HTML document: they'll be rendered as a regular space character by browsers by default, and only as regular newlines in preformatted content. – BoltClock Jan 30 '12 at 11:26
  • It doesn't work. [jsfiddel](http://jsfiddle.net/saeedneamati/q4WC4/). – Saeed Neamati Jan 30 '12 at 11:27
  • 9
    @SaeedNeamati - It does: http://jsfiddle.net/q4WC4/1/ – Álvaro González Jan 30 '12 at 11:29
  • 20
    Then you should also include `white-space` property, yeah? Am I right? – Saeed Neamati Jan 30 '12 at 11:31
  • 7
    @SaeedNeamati - The key is on @BoltClock's comment. Playing with `white-space` is just a simple way to make line feeds visible. – Álvaro González Jan 30 '12 at 11:44
  • 2
    This doesn't work if `content: attr(some-attr)` if e.g. `some-attr="foo \A bar"`. Any ideas about what to do in this case? – Griffin Jun 03 '21 at 12:06
  • @Griffin - You can use the HTML code for a new line in that case ` `, but you also still need `white-space: pre-wrap` or something similar on the element. – Shoelaced Aug 17 '23 at 21:09