Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapse borders of nested DIVs

Tags:

css

border

I have a problem styling nested DIVs (see here for an example).

I have some nested DIVs (with class="box") which are dynamically rendered, e.g:

<div class="box" id="1">
  other content
  <div class="box" id="2">
    <div class="box" id="3">
    </div>
  </div>
  other content
  <div class="box" id="4">
  </div>
</div>
other content

I'd like these DIVs to have a border at the bottom:

<style>
div.box {border-bottom: solid 1px gray;}
</style>

The problem is, when the bottom border of two nested DIVs are adjacent (e.g. box 2 and 3 or box 1 and 4), then the result is a gray line of 2 (or more pixels) height.

Is it possible to collapse the borders of nested DIVs, if they are adjacent?

I tried adding border-collapse: collapse, but that didn't help.

like image 510
M4N Avatar asked Oct 20 '25 21:10

M4N


2 Answers

border-collapse property is only applicable to table and inline-table elements.

Try adding margin-bottom property to .box elements with a negative value to overlap the borders as follows:

Example Here

div.box {
    border-bottom: solid 1px gray;
    margin-bottom: -1px;
}
like image 186
Hashem Qolami Avatar answered Oct 23 '25 11:10

Hashem Qolami


The border collapse doesn't work, I got it working with your JsFiddle but you probably have to change it because you're DIVs are dynamically created.

div.box > div.box {
    border-bottom: solid 1px gray;
}

div.box > div.box > div.box:last-child {
    border-bottom: none;
}
like image 42
Tom Spee Avatar answered Oct 23 '25 10:10

Tom Spee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!