I'm trying to make a horizontal rule with some text in the middle. For example:
----------------------------------- my title here -----------------------------
Is there a way to do that in CSS? Without all the "-" dashes obviously.
I'm trying to make a horizontal rule with some text in the middle. For example:
----------------------------------- my title here -----------------------------
Is there a way to do that in CSS? Without all the "-" dashes obviously.
I have tried most of the ways suggested but ends with some problems like full width, or Not suitable for dynamic content. Finally i modified a code, and works perfectly. This example code will draw those lines before and after, and it is flexible in content change. Center aligned too.
HTML
CSS
Result: https://jsfiddle.net/fasilkk/vowqfnb9/
Not to beat a dead horse, but I was searching for a solution, ended up here, and was myself not satisfied with the options, not least for some reason I wasn't able to get the provided solutions here to work well for me. (Likely due to errors on my part...) But I've been playing with flexbox and here's something I did get to work for myself.
Some of the settings are hard-wired, but only for purposes of demonstration. I'd think this solution ought to work in just about any modern browser. Just remove/adjust the fixed settings for the .flex-parent class, adjust colors/text/stuff and (I hope) you'll be as happy as I am with this approach.
HTML:
I also saved my solution here: https://jsfiddle.net/Wellspring/wupj1y1a/1/
This gives you a static line width, but works great. The line width is controlled by adding or taking '\00a0' (a unicode space).
Yet another method:
Here is Flex based solution.
HTML:
CSS:
JSFiddle: https://jsfiddle.net/yoshiokatsuneo/3h1fmj29/
Solution for IE8 and newer...
Issues worth noting:
Using
background-color
to mask a border might not be the best solution. If you have a complex (or unknown) background color (or image), masking will ultimately fail. Also, if you resize the text, you'll notice that white background color (or whatever you set) will start covering up the text on the line above (or below).You also don't want to "guesstimate" how wide the the sections are either, because it makes the styles very inflexible and almost impossible to implement on a responsive site where the width of the content is changing.
Solution:
(View JSFiddle)
Instead of "masking" a border with a
background-color
, use yourdisplay
property.HTML
CSS
Resize your text by placing your
font-size
property on the.group
element.Limitations:
top
property on.line
element needs to be half ofline-height
. So, if you have aline-height
of1.5em
, then thetop
should be-.75em
. This is a limitation because it's not automated, and if you are applying these styles on elements with different line-heights, then you might need to reapply yourline-height
style.For me, these limitations outweigh the "issues" I noted at the beginning of my answer for most implementations.