IggY
IggY

Reputation: 3125

Remove excess width on parent when child elements wrap

I have a div container with display: inline-block; with a background color.

Inside this div, I add more rectangle divs side by side with float:left.

When there is not enough space for all the divs on one line, the next one is added under the others, leaving an empty space on the right side of the page. The empty space left over has the color of the background of my container but I want it to be white.

enter image description here

The image on the left is what it currently looks like. The image on the right is what it should look like; the parents background should shrink wrap around its children:

enter image description here

Here is a jsfiddle of the below:

.bigDiv {
  background-color: #aaa;
  display: inline-block;
}
.item {
  height: 50px;
  width: 150px;
  background-color: #000;
  float: left;
  margin: 10px;
}
<div class="bigDiv">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

Upvotes: 1

Views: 1268

Answers (2)

misterManSam
misterManSam

Reputation: 24692

Creating "pseudo-backgrounds"

I have just come up with a somewhat... creative solution:

  • The parent is not given a background, only padding and overflow: hidden

  • Each child div is given a ::before pseudo element that will create a "pseudo-background" (z-index: -1 moves it behind the divs background)

  • The pseudo-background is positioned with top, left and right with negative values that drag them into the divs padding.

  • The height: 1000% can be any absurd number, as long as it is tall enough to cover every row of divs. The excess is cleanly cut off with overflow: hidden on the parent div

This is what each div looks like, the black border is the edge of the parent which is where the yellow "pseudo-background" is cut off:

Screenshot 1

it becomes this:

Screenshot 2

Note: This is a purely visual thought experiment, which may be useful in some cases. The div is still taking up the same width, it just has no background color.

Example

Resize it, see how the "pseudo-background" works at all widths.

.wrapper {
  overflow: hidden;
  padding: 10px 0 0 10px;
  width: 70%;
}
.wrapper > div {
  height: 40px;
  background: #F00;
  margin: 0 10px 10px 0;
  width: 200px;
  float: left;
  position: relative;
}
.wrapper > div::before {
  content: '';
  position: absolute;
  background: rgba(255, 255, 0, 1);
  left: -10px;
  right: -10px;
  top: -10px;
  z-index: -1;
  height: 1000%;
}
<div class="wrapper">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>

</div>

Upvotes: 2

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

This is a perfect use-case of <table> or better, FlexBox:

Preview:

Snippet:

.flex-container {
  margin: 10px;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-direction: row;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-justify-content: flex-start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  -webkit-align-content: stretch;
  -ms-flex-line-pack: stretch;
  align-content: stretch;
  border: 1px solid #999;
}

.flex-item {
  -webkit-order: 1;
  -ms-flex-order: 1;
  order: 1;
  -webkit-flex: 1 1 auto;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  -webkit-align-self: auto;
  -ms-flex-item-align: auto;
  align-self: auto;
  border: 1px solid #99f;
  margin: 5px;
  min-width: 30%;
}
<div class="flex-container">
  <div class="flex-item">Hello</div>
  <div class="flex-item">Hello</div>
  <div class="flex-item">Hello</div>
  <div class="flex-item">Hello</div>
</div>

<div class="flex-container">
  <div class="flex-item">Hello</div>
  <div class="flex-item">Hello</div>
  <div class="flex-item">Hello</div>
</div>

<div class="flex-container">
  <div class="flex-item">Hello</div>
  <div class="flex-item">Hello</div>
</div>

Same CSS, infinite entries.

Upvotes: 1

Related Questions