n4m31ess_c0d3r
n4m31ess_c0d3r

Reputation: 3148

Overflow background beyond window's width

I have to design something like below:

Scroll horizontally in the snippet below to see the behavior.

.overflow {
  overflow-x: auto;
}

.parent {
  background: #ccc;
  border: 4px solid blue;
  display: flex;
  margin: 10px 0;
}

.child {
  display: inline-flex;
  height: 50px;
  background: white;
  margin: 10px;
  flex: 1 0 300px;
  border: 2px solid red;
}
<div class="overflow">
  <div class="parent">
    <div class="child">
      1
    </div>
    <div class="child">
      2
    </div>
    <div class="child">
      3
    </div>
  </div>
  <div class="parent">
    <div class="child">
      1
    </div>
    <div class="child">
      2
    </div>
    <div class="child">
      3
    </div>
  </div>
</div>

The issue now is that the grey background on parent is not overflowing behind the children beyond the window's width.

How to achieve this (make .parent div background overflow for all its children) and possibly with retaining the flex-box?

Upvotes: 0

Views: 137

Answers (2)

Temani Afif
Temani Afif

Reputation: 273080

Use inline-flex for parent and replace flex-basis by width

.overflow {
  overflow-x: auto;
}

.parent {
  background: #ccc;
  border: 4px solid blue;
  display: inline-flex;
  min-width:100%; /*To keep the block behavior*/
  box-sizing:border-box;
  margin: 10px 0;
}

.child {
  display: inline-flex;
  height: 50px;
  background: white;
  margin: 10px;
  width:300px;
  flex-shrink:0;
  flex-grow:1;
  border: 2px solid red;
}
<div class="overflow">
  <div class="parent">
    <div class="child">
      1
    </div>
    <div class="child">
      2
    </div>
    <div class="child">
      3
    </div>
  </div>
  <div class="parent">
    <div class="child">
      1
    </div>
    <div class="child">
      2
    </div>
    <div class="child">
      3
    </div>
  </div>
</div>

Upvotes: 2

onkar vhatkar
onkar vhatkar

Reputation: 56

Remove overflow div

Change your parent class css to

.parent {
  overflow-x: auto;
  background: #ccc;
  border: 4px solid blue;
  display: flex;
  margin: 10px 0;
}

Upvotes: 0

Related Questions