Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a child Div remove portion of parent div's border?

Tags:

css

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.

like image 601
Taylor C. White Avatar asked Jun 01 '15 13:06

Taylor C. White


People also ask

How do I remove part of a border in CSS?

We can specify the no border property using CSS border: none, border-width : 0, border : 0 properties.

How can a child div fit parent div?

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 ).

How do I make a div take the width of a parent?

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 .


3 Answers

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

Alternativ

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

like image 145
LinkinTED Avatar answered Sep 18 '22 00:09

LinkinTED


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;
}
like image 21
Mark Steggles Avatar answered Sep 21 '22 00:09

Mark Steggles


You can use box-shadow:

.innerNoBorder {
    box-shadow: 0 0 0 2px white;
}

FIDDLE: https://jsfiddle.net/lmgonzalves/bc6uLmhx/5/

like image 43
lmgonzalves Avatar answered Sep 22 '22 00:09

lmgonzalves