Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical border between floating DIVs using CSS

Tags:

html

css

border

I have the following HTML structure

<div id='parent'>
    <div id='child-1'>Some text goes here</div>
    <div id='child-2'>Different text goes here</div>
    <div class='clear'></div>
</div>

I also have the following CSS

#parent {
    width: 800px;
    position: relative;
}

#child-1 {
    width: 500px;
    float: left;
}

#child-2 {
    width: 200px;
    float: left;
}

.clear {
    clear: both;
}

Now, the contents of the child DIVs (child-1 and child-2) could be anything, so eventually child-1 might have longer height than child-2, or child-2 might have a longer height than child-1.

What I want to do, is have a vertical line between child-1 and child-2, and this line has the length of the DIV that is of longer height. I tried border on both DIVs, (right border for child-1 and left border for child-2), but this is not a good idea, because the line will appear thick where the two DIVs touch each other and then thin for the extended part.

How can I do that? I also like to use CSS only if possible, no jQuery nor JavaScript. If you think extra DIVs are needed then this is OK though.

Thanks.

like image 205
Greeso Avatar asked Aug 04 '13 03:08

Greeso


People also ask

How do you put a vertical line between two divs in CSS?

You can use <hr> , as it is semantically correct, and then use CSS to convert it to a vertical line.

How do I add a line between two divs?

To add a horizontal line between two divs, just put <hr> between the divs. You can use CSS to style this in a variety of ways such as adjusting the color and thickness of the line as well as the top and bottom margins.

Can we give border to div?

You can use: border-style: solid; border-width: thin; border-color: #FFFFFF; You can change these as you see fit, though.


1 Answers

I tried border on both divs, (right border on child-1 and left border on div-2, but this is not a good idea, because the line will appear thick where the two divs touch each other and then thin for the extended part.

That's a good way to go, actually. The final step, though, is to give the right div a negative left margin of 1px (assuming the border is 1px wide), so that the two borders overlap.

#child-1 {
    width: 500px;
    float: left;
    border-right: 1px solid gray;
}

#child-2 {
    width: 200px;
    float: left;
    border-left: 1px solid gray;
    margin-left: -1px;
}

Another option is to use display: table on the parent and then display: table-cell on the columns, and then have a single border line between them.

like image 78
ralph.m Avatar answered Nov 13 '22 10:11

ralph.m