2

I would like to have a divider on my page that looks like this:

My "Mock Up"

What is the best way to do that?

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
BrianLender
  • 531
  • 1
  • 8
  • 16

5 Answers5

3

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

HTML

<div class="rule">
    <div class="line"><div></div></div>
    <div class="words">words are cool</div>
    <div class="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: 0 5px;
}

.line {
    width: 50%;
    vertical-align: middle;
}
mpen
  • 272,448
  • 266
  • 850
  • 1,236
2

html

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

css

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

h3 span {
    display:inline-block;
    padding:0 10px;
    background:#fff;
}
h3:before {
    content:"";
    display:block;
    position:absolute;
    z-index:-1;
    left:0;
    right:0;
    top:50%;
    height:1px;
    background:#ccc;
}
Mary Tarasenko
  • 708
  • 6
  • 11
1

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

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

<div class="lines" data-text="Some Text Goes Here"></div>

.lines {
    position: relative;
    font-size: 20px;
    font-family: sans-serif;
    margin: 0 auto;
    border-top: 1px solid silver;
    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;
}
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • 1
    Like this solution a lot. Might use it in a couple of years when HTML5 and CSS3 are fully integrated. – S.Visser Aug 29 '13 at 23:39
0

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;
}
#myDiv span
{
    padding-left: 16px;
    padding-right: 16px;
    background: white;
    font-weight: bold;
    font-size: 1.5em;
}

HTML:

<div id='myDiv'><span>OUR LATEST WORK</span></div>
enhzflep
  • 12,927
  • 2
  • 32
  • 51
0

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: 0px 1em;
}

HTML:

<div class="hline"><span>Our latest work</span></div>
lwiseman
  • 750
  • 7
  • 29