10

I am currently working on some html for a calendar (in html5), and I am trying to add in a coloured horizontal line. I have been using the horizontal rule tag <hr> but I'm struggling to get most of the attributes to work at all.

This is what I want:

<hr color="purple" align="left" width="120%" size="6">

I read that some attributes don't work in html5... and the only one that is working is the width tag. None of the others are doing anything!

How can I get this to work? Is there a better way to approach this?

1
  • Use CSS. Not HTML attributes.
    – Turnip
    Commented Feb 14, 2017 at 13:47

4 Answers 4

17

Use CSS rules

hr.someClass {
  background-color: purple;
  width: 120%;
  height: 12px;
  border-top: 1px solid black
}
<hr class="someClass">

2
  • Simple question, but I need to get rid of the gaps on the left and right... as in zero margin on each side. any ideas?
    – Ruth Young
    Commented Feb 14, 2017 at 14:00
  • 1
    @RuthYoung the space is caused (in the example snippet here) by padding on the body - adding body { padding-left: 0; } to your css fixes the example here, and may fix it for you
    – CalvT
    Commented Feb 14, 2017 at 14:03
12

You could add a div with a border-top, as below:

<div style="border-top: 6px solid purple"></div>

With external styles (which is always better):

.horizontal-rule {
  border-top: 6px solid purple;
}
<div class="horizontal-rule"></div>

8
  • Why can OP not use a <hr>? How/why is a <div> more suitable?
    – Turnip
    Commented Feb 14, 2017 at 13:49
  • @Turnip is OP's question not literally asking alternative to horizontal rule tag?
    – nbokmans
    Commented Feb 14, 2017 at 13:51
  • OP wanted a 6px purple rule, why not just show the complete CSS for that?
    – msanford
    Commented Feb 14, 2017 at 13:52
  • No. It is "why aren't the attributes applied"
    – Turnip
    Commented Feb 14, 2017 at 13:52
  • 1
    @Turnip Well the question states that the OP is having trouble with <hr>, so I've presented an alternative
    – CalvT
    Commented Feb 14, 2017 at 13:52
4

In HTML 4.01, the <hr> tag represents a horizontal rule. In HTML5, the <hr> tag defines a thematic break. Also attributes like align, size and width are not supported in HTML5.

The proper alternative to this is defining a div as:

HTML

<div class="hr"></div>

CSS

.hr {
    border-top: 6px solid purple;
    width: 120%;
}
0
2

You can use

<div style="background-color: purple; text-align: left; width: 120%; height: 6px;"></div>

here you can give the "background-color" for the "hr" line and height as the border height. I hope this will work :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.