1124

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: ' ';
}
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 8
    In a different sense, adding content using CSS violates the separation of concerns, CSS is meant for style definitions alone. It is preferable to avoid from a accessibility point of view, as disabling CSS screws up the whole mark up. However, it is nice to add images using this technique. – questzen Oct 10 '08 at 08:03
  • 106
    It depends. In this case it's for presentation purposes. It would "violate" SoC to put " >" in the html – mathieu Oct 10 '08 at 09:11
  • 3
    Just a question, do you think that'd be better off as an ordered list? I mean, it is a list with an order isn't it? – alex Mar 20 '09 at 05:36
  • 2
    True, but... there's still order there. Three is after Two is after One. Using an OL would require more markup and a lot more styling, too. – nickf Mar 20 '09 at 05:50
  • 4
    @questzen - I think it's perfectly acceptably (and good form) to use CSS `:after` to set, for instance, the asc/desc sort indicator on a sorted column. – Josh M. Apr 29 '14 at 01:18
  • 16
    I noticed people have gone crazy about the SoC principle. Heavens, what kind of content is a ">" sign for you?! Have mercy! it's just an icon in this context, a widget, a bullet, you name it. To me content is something I would like to be searchable. – user776686 May 09 '14 at 10:06
  • Helpful link to convert your Icons or characters into the CSS value: https://www.evotech.net/articles/testjsentities.html – user2310852 Oct 16 '17 at 13:59
  • 1
    I edited the question and a couple answers to simplify to the question of html entities since that was the actual question here and removed the reference to the strange use case that was making all the answers confusing (adding what looked liked CSS syntax that was just a greater than sign due to its proximity to other unusual CSS escapes) – Charlie Apr 23 '18 at 05:18
  • I came to this question because I wanted to add a checkmark to list items marked as completed. – Alonso del Arte Mar 30 '23 at 03:07

10 Answers10

1173

You have to use the escaped unicode :

Like

.breadcrumbs a:before {
  content: '\0000a0';
}
Rob
  • 14,746
  • 28
  • 47
  • 65
mathieu
  • 30,974
  • 4
  • 64
  • 90
  • 69
    The leading zeroes are superfluous, see [CSS 2.1: 4.3.7 Strings](http://www.w3.org/TR/CSS2/syndata.html#strings). `'>\a0'` suffices. – PointedEars Dec 21 '11 at 19:35
  • 22
    @dlamblin Further, to avoid characters being interpreted as part of the escape sequence *reliably*, add standard white-space: `'>\a0 bc'` is displayed `> bc`. – PointedEars Dec 21 '11 at 19:41
  • 23
    My tool http://amp-what.com/#q=%3E provides a "CSS" mode. Choose "css" at the bottom of the page. Per CSS reference above, these need to be space delimited when they are ambiguous. – ndp Feb 21 '13 at 19:47
  • 1
    @mathieu Can you use 'content' to append image instead? Just wondering – weia design Jul 11 '13 at 14:27
  • 2
    @Adam, no, if you want to append an image, use "background-image" and display:inline-block; and width:123px; height:123px; (use exact width/height) – Nathan J.B. Oct 10 '13 at 04:42
  • online converter: https://r12a.github.io/apps/conversion/ (use CSS escapes result) – Evgenii Jan 21 '16 at 10:56
  • For anyone about to use two a0's for a wider space, try `\2002` for an n-space or `\2003` for an m-space. Similar in width to the n-dash and mdash. – cdaddr Mar 14 '18 at 00:09
  • how can we use custom html like this ? for example, I want to use a svg image as content in multiple places, can that be done like this ? – user2595861 Oct 16 '20 at 07:30
  • I realise this is very after-the-fact but I found this to be helpful: https://css-tricks.com/snippets/html/glyphs/ –  Sep 17 '21 at 19:54
198

CSS is not HTML.   is a named character reference in HTML; equivalent to the decimal numeric character reference  . 160 is the decimal code point of the NO-BREAK SPACE character in Unicode (or UCS-2; see the HTML 4.01 Specification). The hexadecimal representation of that code point is U+00A0 (160 = 10 × 161 + 0 × 160). You will find that in the Unicode Code Charts and Character Database.

In CSS you need to use a Unicode escape sequence for such characters, which is based on the hexadecimal value of the code point of a character. So you need to write

.breadcrumbs a:before {
  content: '\a0';
}

This works as long as the escape sequence comes last in a string value. If characters follow, there are two ways to avoid misinterpretation:

a) (mentioned by others) Use exactly six hexadecimal digits for the escape sequence:

.breadcrumbs a:before {
  content: '\0000a0foo';
}

b) Add one white-space (e. g., space) character after the escape sequence:

.breadcrumbs a:before {
  content: '\a0 foo';
}

(Since f is a hexadecimal digit, \a0f would otherwise mean GURMUKHI LETTER EE here, or ਏ if you have a suitable font.)

The delimiting white-space will be ignored, and this will be displayed  foo, where the displayed space here would be a NO-BREAK SPACE character.

The white-space approach ('\a0 foo') has the following advantages over the six-digit approach ('\0000a0foo'):

  • it is easier to type, because leading zeroes are not necessary, and digits do not need to be counted;
  • it is easier to read, because there is white-space between escape sequence and following text, and digits do not need to be counted;
  • it requires less space, because leading zeroes are not necessary;
  • it is upwards-compatible, because Unicode supporting code points beyond U+10FFFF in the future would require a modification of the CSS Specification.

Thus, to display a space after an escaped character, use two spaces in the stylesheet –

.breadcrumbs a:before {
  content: '\a0  foo';
}

– or make it explicit:

.breadcrumbs a:before {
  content: '\a0\20 foo';
}

See CSS 2.1, section "4.1.3 Characters and case" for details.

Dolan
  • 313
  • 1
  • 4
  • 14
PointedEars
  • 14,752
  • 4
  • 34
  • 33
  • +1 for the "to display a space after an escaped character" trick, but have to mention that adding an nbsp and then a space kind of defeats the purpose of the nbsp ;) – TWiStErRob Dec 28 '17 at 12:44
  • @TWiStErRob No, a combination of a non-breaking space character followed by a breaking space character (or vice-versa) will display a gap about the width of *two* space characters, while two or more *verbatim* space characters will only display a gap of the width of *one* space character as the parser of the layout engine collapses consecutive breaking whitespace (including newline) into one space character unless the [`white-space` CSS property](https://www.w3.org/TR/2011/REC-CSS2-20110607/text.html#white-space-prop) declares otherwise. – PointedEars Jan 23 '18 at 05:48
  • yep, I know about collapsing, but the point of nbsp is that it's non-breaking, word chars are also non-breaking by default, so adding a breaking space next to a non-breaking one makes no sense, the end result will be breaking. I'm talking for `white-space: normal`. – TWiStErRob Jan 23 '18 at 10:07
  • @TWiStErRob Please read my answer more carefully. It is an *example* for “display[ing] a space after an escaped character”. The original code had a `NO-BREAK SPACE` character, so I used the escape sequence for it. You can choose any other escape sequence; if required, you can declare `white-space: nowrap`, too. – PointedEars Jan 25 '18 at 03:01
84

Update: PointedEars mentions that the correct stand in for   in all css situations would be
'\a0 ' implying that the space is a terminator to the hex string and is absorbed by the escaped sequence. He further pointed out this authoritative description which sounds like a good solution to the problem I described and fixed below.

What you need to do is use the escaped unicode. Despite what you've been told \00a0 is not a perfect stand-in for   within CSS; so try:

content:'>\a0 ';          /* or */
content:'>\0000a0';       /* because you'll find: */
content:'No\a0 Break';    /* and */
content:'No\0000a0Break'; /* becomes No Break as opposed to below */

Specifically using \0000a0 as  . If you try, as suggested by mathieu and millikin:

content:'No\00a0Break'   /* becomes No਋reak */

It takes the B into the hex escaped characters. The same occurs with 0-9a-fA-F.

dlamblin
  • 43,965
  • 20
  • 101
  • 140
  • 8
    You just need to put a space after the escape sequence, as specified: `'\a0 Break'`. `'\0000a0Break'` is *not* reliable. – PointedEars Dec 21 '11 at 19:39
  • @PointedEars Interesting, so then this space is a terminator and does not get included into the string? – dlamblin Jan 03 '12 at 05:53
  • 2
    See [CSS 2.1, section "4.1.3 Characters and case"](http://www.w3.org/TR/CSS2/syndata.html#escaped-characters). Which also shows that your approach SHOULD be reliable per CSS 2.1. But I find the whitespace approach cleaner (upwards-compatible) and having a smaller footprint. – PointedEars Jan 03 '12 at 13:29
54

In CSS you need to use a Unicode escape sequence in place of HTML Entities. This is based on the hexadecimal value of a character.

I found that the easiest way to convert symbol to their hexadecimal equivalent is, such as from ▾ (▾) to \25BE is to use the Microsoft calculator =)

Yes. Enable programmers mode, turn on the decimal system, enter 9662, then switch to hex and you'll get 25BE. Then just add a backslash \ to the beginning.

Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
netgoblin
  • 659
  • 5
  • 11
36

Use the hex code for a non-breaking space. Something like this:

.breadcrumbs a:before {
  content: '>\00a0';
}
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
John Millikin
  • 197,344
  • 39
  • 212
  • 226
18

There is a way to paste an nbsp - open CharMap and copy character 160. However, in this case I'd probably space it out with padding, like this:

.breadcrumbs a:before { content: '>'; padding-right: .5em; }

You might need to set the breadcrumbs display:inline-block or something, though.

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Dare
  • 181
  • 1
  • 2
  • 4
    Instead of pasting the nbsp, i'd stick to using an entity, so that it's clear to future maintainers that it's different to a space. – nickf Dec 06 '09 at 03:58
  • 2
    on the other hand, you dont do breadcrumbs as they are supposed to be, i.e: the > symbol should be inline with the breadcrumb, and not just a visual style. at least if you want google to see your breadcrumbs and list them under your listing in the search engine. – Rafael Herscovici Jan 25 '12 at 16:32
18

For Example :

http://character-code.com/arrows-html-codes.php

Example: If you want select your character , I selected "&#8620" "&#x21ac" (We use HEX values)

.breadcrumbs a:before {
  content: '\0021ac';
}

Result: ↬

Thats it :)

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Ferhat KOÇER
  • 3,890
  • 1
  • 26
  • 26
  • i used content:'\10095'; . But the output is not showing. instead of the icon, it's showing rectangle. – kapil Dec 04 '17 at 06:28
1

I know this is an pretty old post, but if spacing is all your after, why not simply:

.breadcrumbs a::before {
  content: '>';
  margin-left: 8px;
  margin-right: 8px;
}

I have used this method before. It wraps perfectly fine to other lines with ">" by its side in my testing.

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
brian-welch
  • 441
  • 1
  • 7
  • 19
0

Here are two ways:

  • In HTML:

    <div class="ics">&#9969;</div>

This will result into ⛱

  • In Css:

    .ics::before {content: "\9969;"}

with HTML code <div class="ics"></div>

This also results in ⛱

Tarandeep Singh
  • 1,322
  • 16
  • 16
0

Just to make the answers here a little more complete. If you are using something like content: attr(aria-label);, then rather than using unicode escapes, you DO need to use HTML entities instead. For example, using &#10; to generate a line break.

Julian Knight
  • 4,716
  • 2
  • 29
  • 42