Greeso Greeso - 8 days ago 6
CSS Question

Vertical border between floating DIVs using CSS

I 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>


Now, I 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 larger height than child-2, or child-2 might have a larger 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 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.

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.

Answer

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.