skip to Main Content

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:
enter image description here

And this is expected behaviour:
enter image description here

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


  1. 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:

    .bg-green, .bg-red {
       min-width: max-content;
    }
    
    Login or Signup to reply.
  2. 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 and max-width are sized within their containing elements.

    So I tried it on jsfiddle and fixed it.


    Below is the code for the same :

    .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%;
    }
    
    .bg-green, .bg-red {
      min-width: max-content;
      padding: 10px;
      box-sizing: border-box; /* Added box-sizing */
    }
    <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>

    Zoomed at 100% Output :

    enter image description here

    Zoomed at 300% Output :

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search