Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align <hr> to the left in an HTML5-compliant way

Currently, I'm using

<hr align="left" />

on my HTML5 page, but I've read that the align property was deprecated in XHTML 4.01 and supposedly removed from HTML5. I'd like to be using CSS rather than an inline attribute like this, but when I tried

hr{align: left; max-width: 800px;}

or hr{text-align: left;} or hr{left: 0;} or hr{float: left;}, it just showed up in the center.

So what should I use instead of the inline attribute above?

like image 284
Chris Middleton Avatar asked Dec 22 '13 00:12

Chris Middleton


People also ask

How do you left-align in HTML?

To set text alignment in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property text-align for the center, left and right alignment.

Can you style hr in HTML?

Its simple to add a horizontal line in your markup, just add: <hr>. Browsers draw a line across the entire width of the container, which can be the entire body or a child element. Originally the HR element was styled using attributes.


4 Answers

One option would be to set the left margin to zero:

hr{max-width: 800px; margin-left:0;}
like image 71
matt Avatar answered Sep 30 '22 17:09

matt


You're trying to use something in a way that (as Eliezer Bernart mentions.. and apparently that comment with the link to the MDN doc disappeared) no longer "works that way". You can, as long as you don't mind it being screwy in IE, just set the margin to zero - http://jsfiddle.net/s52wb/

hr {
    max-width: 100px;
    margin: 0px;
}

A better idea though would be to mimic the HR's old way of doing things by way of CSS without the use of the HR. Check out http://jsfiddle.net/p5ax9/1/ for a demo:

p:first-child:before {
    display: none;
}
p:before {
    content: " ";
    border: 1px solid black;
    margin-top: 15px;
    margin-bottom: 15px;
    display: block;
    max-width: 100px;
}
like image 42
Stephen Avatar answered Sep 30 '22 15:09

Stephen


I don't know what browsers were used for some above answers, but I found neither text-align:left nor margin-left:0 worked in both IE (11, Standards mode, HTML5) and Firefox (29).

  • IE11: text-align:left works, margin-left:0 leaves rule centred.

  • FF: margin-left:0 works, text-align:left leaves rule centred.

  • So, either use both, or I found that margin-right:100% works for both!

like image 20
JonBrave Avatar answered Sep 30 '22 16:09

JonBrave


You can use div tag instead.

<div style="border: thin solid lightgray; width: 100px;"></div>
like image 22
Art Avatar answered Sep 30 '22 15:09

Art