Languages
[Edit]
EN

CSS - insert text to element using styles only

0 points
Created by:
Welsh
902

In this article, we would like to show you how to insert text to element using styles only

Quick solution:

.class-name::before  {  /* pseudo-element that is the first child of the selected element(s) */
    content: 'content before';
}

.class-name::after  {  /* pseudo-element that is the last child of the selected element(s) */
    content: 'content after';
}

Note:

The ::before and ::after elements are inline by default.

 

Practical example

In order to insert text to the element using styles only, we use ::before and ::after pseudo elements. They allow us to add content property to the selected element (::before - as the first child, ::after - as the last child of the selected element).

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>
    
    .class-name::before  {  /* pseudo-element that is the first child of the selected element(s) */
        content: 'content before...';
    }

    .class-name::after  {  /* pseudo-element that is the last child of the selected element(s) */
        content: '...content after';
    }

  </style>
</head>
<body>
  <div class="class-name">some text</div>
</body>
</html>

 

References

  1. ::before - CSS: Cascading Style Sheets | MDN
  2. ::after - CSS: Cascading Style Sheets | MDN
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