Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Horizontal Rule with Css and have margins

Tags:

html

css

div.horizontalRule {
    clear:both;
    width:100%;
    background-color:#d1d1d1;
    height:1px;
    margin-top:5px;
    margin-bottom:5px;
}

This is what I am doing now but the Margins seem to have no effect! I am not sure why but the text above and below this "horizontal rule" touch the horizontal rule with no margins. Is there a way to implement margins in this scenario?

http://jsfiddle.net/fwqSH/

like image 947
joncodo Avatar asked Aug 17 '11 17:08

joncodo


People also ask

How do you code a horizontal line in CSS?

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.

Can you style a horizontal rule?

A horizontal rule is used to provide a visual break and divide content. Like other HTML elements, horizontal rules can be styled using CSS (and SVG). This means that they don't have to look like boring, plain horizontal lines.

How do you make a horizontal line with words in the middle in CSS?

We are using the css flex-box, to create a horizontal line with a text is placed in the middle. In the above code, we have used the flex-box in . sperator class, so that it creates a flex-container and aligns all three elements horizontally. The align-items: center property aligns the elements vertically center.

Can you add margin to a border CSS?

No, that's not possible. Padding, margin and border are all parts of elements, you can't give a border padding or a margin a border.


2 Answers

Problem is your not closing the div:

You cannot close a div as you did there must be a closing tag as so:

<div></div>

and not

<div />

corrected jsfiddle:

http://jsfiddle.net/fwqSH/1/

EDIT

Final solution was to add a min-height of 1px because an empty div sometimes do weird things.

Final CSS:

div.horizontalRule {
    min-height: 1px;
    clear:both; width:100%;
    border-bottom:1px solid #d1d1d1;
    height:1px; padding-top:5px;
    margin-top:5px;
    margin-bottom:5px;
}
like image 172
Nachshon Schwartz Avatar answered Oct 24 '22 11:10

Nachshon Schwartz


The reason the text below it butts right up against the line is because you didn't properly close the div. The browser sees <div /> and thinks that the paragraph after that is part of the div. So change your HTML to something like this:

<div class="horizontalRule" runat="server"></div>
like image 26
sdleihssirhc Avatar answered Oct 24 '22 13:10

sdleihssirhc