CSS Tips: Create Horizontal Lines and Centered Text
P粉248602298
2023-08-20 15:11:18
<p>I'm trying to make a horizontal divider with some text.
For example: </p>
<p>---------------------------------- Here is my title------ -----------------------</p>
<p>Is there a way to achieve this effect in CSS? Obviously there is no need to use all the "-" dashes. </p>
After trying different solutions, I found a solution that works for different text widths, any possible background and without adding extra markup.
I tested in IE8, IE9, Firefox and Chrome. You can check it here: http://jsfiddle.net/Puigcerber/vLwDf/1/
This is roughly how I would do it: by setting
border-bottom
on the containingh2
, and then givingh2
a smallerline-height
to create lines. Then put the text inside a nestedspan
with a non-transparent background.