Languages
[Edit]
EN

CSS - add centered text in the middle of horizontal line

3 points
Created by:
nickkk0
647

In this article, we would like to show you how to add centered text in the middle of horizontal line using CSS.

Preview:

Centered text in the middle of horizontal line created using CSS.
Centered text in the middle of horizontal line created using CSS.

Practical example

Edit

In this example, we use ::before and ::after pseudo elements to style title element to have horizontal line passing through the text. The key in this solution is to set height property to 0.05em (em means font size of the parent) and set offset top to 0.55em. With this we achieve the line effect right in the middle of the font size.

 

References

Edit
  1. ::before - CSS: Cascading Style Sheets | MDN
  2. ::after - CSS: Cascading Style Sheets | MDN
  3. CSS values and units - Learn web development | MDN

Alternative titles

  1. CSS - add centered horizontal line behind text
1
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