I have a problem where containers that have min/max width set have a cut off background color and border after horizontal scrolling.
Please ignore a lot of containers, it’s just a simplified version of a React component tree that I’m dealing with:
.page-container {
width: 100%;
height: 100vh;
}
.bg-wrapper {
background-color: wheat;
min-width: 100%;
height: 100%;
}
.content-wrapper {
display: flex;
overflow: auto;
}
.container {
width: 100%;
height: 100%;
}
.bg-red {
background-color: red;
border: 1px solid yellow;
}
.bg-green {
background-color: green;
border: 1px solid yellow;
}
.box {
width: 100%;
margin: 0 auto;
min-width: 500px;
max-width: 1000px;
}
header {
display: flex;
justify-content: space-between;
width: 100%;
}
<div class="page-container">
<div class="bg-wrapper">
<div class="content-wrapper">
<div class="container">
<div class="container">
<div class="container">
<div class="bg-red">
<div class="box">
<header>
<h1>Test</h1>
<h3>Test</h3>
</header>
</div>
</div>
<div class="bg-green">
<div class="box">
<h1>Content</h1>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Here’s how it looks after scrolling horizontally:
And this is expected behaviour:
The only fix I have in mind is adding display: inline-block
to .content
but that introduces other issues so I wouldn’t want that
2
Answers
The easiest way to find out what happens here is to right click your page in your browser and then choose inspect. This is a essential tool for every web developer, because it provides the sizes of the different components and makes debugging much easier.
For example here is the Firefox Documentation for the inspector: Firefox inspector
EDIT:
I tested it now for myself its not working if you set the width to 100% but its working perfectly fine if you set the width to max-content like this:
As per the above comment I got your point and you were correct I was able to reproduce the problem now. It is caused by the way nested elements with
min-width
andmax-width
are sized within their containing elements.So I tried it on jsfiddle and fixed it.
Below is the code for the same :
Zoomed at 100% Output :
Zoomed at 300% Output :