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've been looking around for some solutions for this simple decoration and I've found quite a few ones, some weird, some even with JS to calculate the height of the font and bla,bla,bla, then I've read the one on this post and read a comment from thirtydot speaking about
fieldset
andlegend
and I thought that was it.I'm overriding those 2 elements styles, I guess you could copy the W3C standards for them and include it on your
.middle-line-text
class (or whatever you want to call it) but this is what I did:Here's the fiddle: http://jsfiddle.net/legnaleama/3t7wjpa2/
I've played with the border styles and it also works in Android ;) (Tested on kitkat 4.XX)
EDIT:
Following Bekerov Artur's idea which is a nice option too, I've changed the .png base64 image to create the stroke with an .SVG so you can render in any resolution and also change the colour of the element without any other software involved :)
Ok, this one is more complicated but it works in everything but IE<8
The :before and :after elements are positioned absolutely so we can pull one to the left and one to the right. Also, the width (40% in this case) is very dependent of the width of the text inside.. have to think about a solution for that. At least the
top: 1.2em
makes sure the lines stay more or less in the center of the text even if you have different font size.It does seem to work well though: http://jsfiddle.net/tUGrf/3/
this is roughly how i'd do it: the line is created by setting a
border-bottom
on the containingh2
then giving theh2
a smallerline-height
. The text is then put in a nestedspan
with a non-transparent background.HTML:
CSS:
I tested in Chrome only, but there's no reason it shouldn't work in other browsers.
JSFiddle: http://jsfiddle.net/7jGHS/
If anyone is wondering how to set the heading such that it appears with a fixed distance to the left side (and not centered as presented above), I figured that out by modifying @Puigcerber's code.
Here the JSFiddle.
From so many alternatives I would prefer this answer as it multi cross the nowadays browsers.
I use it more than one of my homepage. As it is advised to only have a single
<h1>
especially on the main page. So it is necessary to changeh1
toh2
or higher.for later(nowdays) browser ,
display:flex
anddpseudo-elements
makes it easy to draw .border-style
,box-shadow
and evenbackground
helps too for the makeup.