Using just CSS is it possible to have a child div border "override" or "remove" a portion of its parent's border? Essentially I'd like to have a border but not on the outside of specific rows, and I'd like to do so without rearranging the DOM structure.
Note that the innerNoBorder
div
does not have a background color.
Example of what I have (which does not work)--
Html:
<div class="outerBorder">
<div class="inner">hello</div>
<div class="innerNoBorder">world</div> <!--have this remove border / override outerBorder -->
<div class="inner">!</div>
</div>
CSS:
.outerBorder {
border: 2px solid black;
}
.innerNoBorder {
border-left:none;
border-right:none;
}
JSFiddle demo.
We can specify the no border property using CSS border: none, border-width : 0, border : 0 properties.
If you want the child divs to fit the parent size, you should put a margin at least of the size of the child borders on the child divs ( child. margin >= child. bordersize ).
The solution is to simply not declare width: 100% . The default is width: auto , which for block-level elements (such as div ), will take the "full space" available anyway (different to how width: 100% does it). Just in case it's not already clear from my answer: just don't set a width on the child div .
The border property isn't inherit from the parent, so your try won't work... but you already figured that out.
You can overlap the parent div though by using a negative margin on the child. Do note that you'll need to give the child a background-color to make it functional.
.innerNoBorder {
background: yellow;
margin-left: -2px;
margin-right: -2px;
}
Updated Fiddle
Same method, but than with a white border:
.innerNoBorder {
border: 2px solid white;
border-width: 0 2px;
margin-left: -2px;
margin-right: -2px;
}
Updated Fiddle
No, it is not possible.
Can you not just set the .outBorder to have no left border?
.outerBorder {
border-left-width: 0;
}
Or you could position the .innerNoBorder div over the left side of its parent, something like this:
.innerNoBorder {
margin-left: -2px;
border-left: 2px solid white;
}
You can use box-shadow
:
.innerNoBorder {
box-shadow: 0 0 0 2px white;
}
FIDDLE: https://jsfiddle.net/lmgonzalves/bc6uLmhx/5/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With