Languages

CSS - how to add HTML non break space using content style property?

0 points
Asked by:
Palpys
764

How can I add HTML entities using CSS content property?

I wanted to add non-breaking space using   but instead it prints ' ' between the words.

My code:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>
    
    span:before {
        content: '&nbsp;';
    }

  </style>
</head>
<body>
  Hello<span>World!</span>
</body>
</html>
1 answer
0 points
Answered by:
Palpys
764

If you want to add HTML entities using content property, you have to use the escaped unicode of the entity.

In your case instead of '&nbsp;' use '\0000a0'.

Practical example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    span:before {
        content: '\0000a0';
    }

  </style>
</head>
<body>
  Hello<span>World!</span>
</body>
</html>
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join