9

I want to display a horizontal line with words in the middle so that it looks like following:

enter image description here

I'm trying this and doesn't work:

HTML:

<h2><span>Test</span></h2>

CSS:

h2{
    font-size: 100px;
    border-top: solid 1px black;
    width: 100%;
    height: 50px;
    margin-top: 25px;
    top: 50%;
    z-index: 1;    
}

span{
     background: #fff;
      padding: 0 20px;
      margin-top:-25px;
       display: inline-block;
       z-index: 5;
}

JSFiddle: http://jsfiddle.net/2ds9a/

user1251698
  • 2,103
  • 12
  • 35
  • 51

3 Answers3

20

you can used to css :after

as like this

HTML

<h2><span  class="line-center">Test</span></h2>

Css

.line-center{
    margin:0;padding:0 10px;
    background:#fff;
    display:inline-block;
}
h2{

    text-align:center;
    position:relative;
    z-index:2;

}
h2:after{
    content:"";
    position:absolute;
    top:50%;
    left:0;
    right:0;
    border-top:solid 1px red;
    z-index:-1;
}

LIve Demo

Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97
  • 1
    I prefer this approach, its semantic and cleaner than the answer found here.. http://stackoverflow.com/questions/2812770/add-centered-text-to-the-middle-of-a-hr-like-line you should post your anser on this page as this question will probably get closed. – Blowsie Apr 15 '13 at 07:31
  • Its also worth mentioning that this wont work in old IE – Blowsie Apr 15 '13 at 07:34
  • IE8 + support :after and now check to all css selector http://caniuse.com/ – Rohit Azad Malik Apr 15 '13 at 07:35
  • hi @RohitAzad please visit this link : http://magento.stackexchange.com/questions/62827/to-display-text-between-horizontal-line-in-magento/62831#62831 he gave an answer as a)

    Text

    b)
    Content
    please help to get above code lines in single html line code....
    –  Apr 06 '15 at 12:48
2

As shown in this answer (suggested by Quentin) the following code should work fine for you:

<div style="height: 2px; background-color: black; text-align: center">
  <span style="background-color: white; position: relative; top: -0.5em;">
    Section Title
  </span>
</div>

For more info take a look at this question.

Community
  • 1
  • 1
Daanvn
  • 1,254
  • 6
  • 27
  • 42
1

There are differents ways to do that, this is my solution:

http://jsfiddle.net/zzLnt/

   <h2><span class="line"></span><span class="text">Test<span></h2>
   <style> 
    h2{
        font-size: 100px;
        z-index: 1;
        position: relative;
        text-align: center;
    }

    .line{
        background: #000;
        border-top: solid 1px black;
        position: absolute;
        height: 1px;
        display: block;
        top: 56px;
        width: 100%;
    }

    .text{
        background-color: #FFFFFF;
        z-index: 20;
        position: relative;
        text-align: center;
        padding: 0 34px;
    }
</style>
grigno
  • 3,128
  • 4
  • 35
  • 47