157

I need to draw a horizontal line after some block, and I have three ways to do it:

1) Define a class h_line and add css features to it, like

#css
.hline { width:100%; height:1px; background: #fff }

#html
<div class="block_1">Lorem</div> <div class="h_line"></div>

2) Use hr tag

#css
hr { width:100%; height:1px; background: #fff }

#html
<div class="block_1">Lorem</div> <hr />

3) use it like a after pseudoclass

#css
.hline:after { width:100%; height:1px; background: #fff; content:"" }

#html
<div class="block_1 h_line">Lorem</div>

Which way is the most practical?

4
  • 2
    I would think the <hr> is the most semantic. I mean, isn't that what it's meant for? Commented Feb 11, 2013 at 21:24
  • 4
    why not use border-bottom? Commented Feb 11, 2013 at 21:24
  • 4
    In HTML5 the HTML <hr> element represents a thematic break between paragraph-level elements (for example, a change of scene in a story, or a shift of topic with a section). Commented Feb 11, 2013 at 21:44
  • 2
    Only use <hr> if the line semantically denotes a thematic break. Do not use an <hr> if it's purely decorative; instead use a non-semantic element like a <div> and style it with CSS. Commented Feb 27, 2023 at 16:39

11 Answers 11

188

hr {
    display: block;
    height: 1px;
    border: 0;
    border-top: 1px solid #ccc;
    margin: 1em 0;
    padding: 0;
}
<div>Hello</div>
<hr/>
<div>World</div>

Here is how html5boilerplate does it:

hr {
    display: block;
    height: 1px;
    border: 0;
    border-top: 1px solid #ccc;
    margin: 1em 0;
    padding: 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Note: Use margin: 1em auto if you want it to always be in the center of the page.
@JacquesMarais, not sure that's necessary as it is a block element with no defined width so it would span the width of the entire container anyway.
69

I'd go for semantic markup, use an <hr/>.

Unless it's just a border what you want, then you can use a combination of padding, border and margin, to get the desired bound.

3 Comments

<hr> is valid HTML5. It used represent a horizontal rule, but is now defined in semantic terms as a thematic break between content. Most browsers will still display it as a horizontal line unless told otherwise. Source: developer.mozilla.org/en-US/docs/Web/HTML/Element/hr
Title says horizontal line and <hr/> is that so I up this. Title maybe should have read styled horizontal line.
@RyanBayne ... but but but ... hr = horizontal rule, not line :)
22

.line {
  width: 53px;
  height: 0;
  border: 1px solid #C4C4C4;
  margin: 3px;
  display:inline-block;
}
<html>
<body>
<div class="line"></div>
<div style="display:inline-block;">OR</div>
<div class="line"></div>
</body>
</html>

1 Comment

You have not answered the question "Which is the most practical?"
14

In HTML5, the <hr> tag defines a thematic break. In HTML 4.01, the <hr> tag represents a horizontal rule.

http://www.w3schools.com/tags/tag_hr.asp

So after definition, I would prefer <hr>

Comments

13

If you really want a thematic break, by all means use the <hr> tag.


If you just want a design line, you could use something like the css class

.hline-bottom {
    padding-bottom: 10px;
    border-bottom: 2px solid #000; /* whichever color you prefer */
}

and use it like

<div class="block_1 hline-bottom">Cheese</div>

Comments

7

I wanted a long dash like line, so I used this.

.dash{
        border: 1px solid red;
        width: 120px;
        height: 0px;

}
<div class="dash"></div>

Comments

0

My simple solution is to style hr with css to have zero top & bottom margins, zero border, 1 pixel height and contrasting background color. This can be done by setting the style directly or by defining a class, for example, like:

.thin_hr {
margin-top:0;
margin-bottom:0;
border:0;
height:1px;
background-color:black;
}

Comments

0

hr {
    display: block;
    height: 1px;
    border: 0;
    border-top: 1px solid #ccc;
    margin: 1em 0;
    padding: 0;
}
<div>Hello</div>
<hr/>
<div>World</div>
emphasized text

1 Comment

Please add a comment explaining your answer.
0

This is relatively simple example and worked for me.

hr {
    width: 70%;
    margin-left: auto;
    margin-right: auto;
}

Resource: https://www.w3docs.com/snippets/css/how-to-style-a-horizontal-line.html

Comments

0

A centered line, with a text in the middle, using margin-top and margin-bottom:

.lineL {
  width: calc(32vw - 24px);
  margin-right:2vw;
  margin-bottom:5px;
  height: 0;
  border: 1px solid #a5a5a5;
  display:inline-block;
}
.lineR {
  width: calc(32vw - 24px);
  margin-left:2vw;
  margin-bottom:5px;
  height: 0;
  border: 1px solid #a5a5a5;
  display:inline-block;
}
.line {
  margin-bottom:40px;
  margin-top:20px;
  margin-left:auto;
  margin-right:auto;
  width:calc(70vw - 24px);
}
<html>
<body>
  <button>Login</button>
  <div class="line">
    <div class="lineL"></div>
    <div style="display:inline-block;width:24px;">OR</div>
    <div class="lineR"></div>
  </div>
  <button>Logout</button>
</body>
</html>

Comments

-1

it is depends on requirement , but many developers suggestions is to make your code as simple as possible . so, go with simple "hr" tag and CSS code for that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.