Skip to content Skip to sidebar Skip to footer

Create A Horizontal Rule With Text In The Middle?

I would like to have a divider on my page that looks like this: What is the best way to do that?

Solution 1:

html

<h3><span>My latest work</span></h3>

css

h3 {
position:relative;
text-align:center;}

h3span {
    display:inline-block;
    padding:010px;
    background:#fff;
}
h3:before {
    content:"";
    display:block;
    position:absolute;
    z-index:-1;
    left:0;
    right:0;
    top:50%;
    height:1px;
    background:#ccc;
}

Solution 2:

We can do this without images or masking lines like so:

HTML

<divclass="rule"><divclass="line"><div></div></div><divclass="words">words are cool</div><divclass="line"><div></div></div></div>

CSS

.rule {
    display: table;
}

.rule>div {
    display: table-cell;
    white-space:nowrap;
}

.line>div {
    border-bottom: 1px solid silver;
    height: 1px;
}

.words {
    padding: 05px;
}

.line {
    width: 50%;
    vertical-align: middle;
}

Solution 3:

Demo: http://jsfiddle.net/5tqE5/1/

This uses attr() which is not supported in older browsers. It could be replaced with an extra element.

<divclass="lines"data-text="SomeTextGoesHere"></div>.lines {
    position:relative;font-size:20px;font-family:sans-serif;margin:0auto;border-top:1pxsolidsilver;margin-top:20px;
}

.lines:before{content:attr(data-text);background-color:#fff;position:absolute;text-align:center;left:50%;width:220px;margin-left:-110px;padding:10px;top:-20px;}

Solution 4:

Dunno about the 'best' - you've given no terms by which to assess that. Smallest, fastest, most compatible, etc, etc.

Anyhoo, I just took a 1-pixel wide slice of your image and saved it. I then use it as the background-image of the div.

CSS:

#myDiv
{
    background: url(horizline1x41px.png) repeat;
    text-align: center;
    line-height: 41px;
}
#myDivspan
{
    padding-left: 16px;
    padding-right: 16px;
    background: white;
    font-weight: bold;
    font-size: 1.5em;
}

HTML:

<divid='myDiv'><span>OUR LATEST WORK</span></div>

Solution 5:

Demo: http://jsfiddle.net/8zve4/

I don't like the extra markup, but this should work.

CSS:

.hline {
  border: 1px solid #EEE;
  color: #666;
  font-family: helvetica;
  font-weight: bold;
  font-variant: small-caps;
  letter-spacing: .1em;
  line-height: 0px;
  text-align: center;
  text-transform: uppercase;
}

.hline > span {
  background-color: #FFF;
    padding: 0px1em;
}

HTML:

<divclass="hline"><span>Our latest work</span></div>

Post a Comment for "Create A Horizontal Rule With Text In The Middle?"