Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal line in the middle of text [duplicate]

Tags:

html

css

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/

like image 854
user1251698 Avatar asked Apr 15 '13 07:04

user1251698


People also ask

How do I put a horizontal line between text in HTML?

While working on web pages, you may want to add a horizontal line to indicate a thematic change between different sections. To create a horizontal line in HTML, you will need to use the HTML hr tag. HR is a block-level element that moves all the elements after it to another line.

Which tag is used to insert horizontal lines to separate blocks of text?

<hr> tag in HTML language is used to insert a horizontal line or rule on the web page.


2 Answers

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

like image 100
Rohit Azad Malik Avatar answered Oct 19 '22 01:10

Rohit Azad Malik


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.

like image 2
Daanvn Avatar answered Oct 18 '22 23:10

Daanvn