Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unwanted horizontal overflow when using padding top

Here is the code:

body {
  margin: 0;
  padding: 0;
}

div {
  -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
  -moz-box-sizing: border-box; /* Firefox, other Gecko */
  box-sizing: border-box;
}

.body {
  width: 100vw;
  height: 100vh;
  background-color: black;
  padding-top: 20vh;
}

.content {
  width: 50vw;
  height: 1000px;
  background-color: yellow;
}
<body>
  <div class="body">
    <div class="content">
    </div>
  </div>
</body>

The problem is fairly simple. When I add padding-top to the .body div, there is a horizontal overflow. I would like to know why does the y-axis padding affect the x-axis width.

I know I can get rid of it using overflow-x: hidden, but it’s a dirty solution.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just use a percentage value:

.body {
  width: 100%; /* Percentage-based (full width of <body>) */
}

More About VW and VH

Viewport units represent a percentage of the current browser viewport (current browser size). While similar to % units, there is a difference. Viewport units are calculated as a percentage of the browser’s current viewport size. Percentage units on the other hand are calculated as a percentage of the parent element, which may be different than the viewport size.

Reference: What’s The Difference Between PX, EM, REM, %, VW, and VH?

body {
  margin: 0;
  padding: 0;
}

div {
  -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
  -moz-box-sizing: border-box; /* Firefox, other Gecko */
  box-sizing: border-box;
}

.body {
  width: 100%; /* Percentage-based (full width of <body>) */
  height: 100vh;
  background-color: black;
  padding-top: 20vh;
}

.content {
  width: 50vw;
  height: 1000px;
  background-color: yellow;
}
<body>
  <div class="body">
    <div class="content">
    </div>
  </div>
</body>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading