CSS grid with padding overflows container
Asked Answered
R

7

27

I am trying to create a footer whose width is 100% (of the body). On the left side of the footer, I want a logo. On the right side of the footer, I want some text. So I decided to try to use CSS grid.

This is almost exactly what I'm going for:

.footer {
  background-color: #2D1975;
  width: 100%;
  height: 350px;
  display: grid;
  grid-template-columns: 30% 70%;
}

.footerGridLogo {
  background-color: red;
}

.footerGridQuestions {
  background-color: green;
}
<div class="footer">
  <div class="footerGridLogo"></div>
  <div class="footerGridQuestions"></div>
</div>

However, I want to add some padding to the left of the grid, let's say 10%, so that the logo isn't so close to the left edge. So instead of a 30-70 split, I try a 10-25-65 split. However, the grid ends up overflowing. Why is that?

Demonstration of the issue:

.footer {
  background-color: #2D1975;
  width: 100%;
  height: 350px;
  display: grid;
  padding-left: 10%;
  grid-template-columns: 25% 65%;
}

.footerGridLogo {
  background-color: red;
}

.footerGridQuestions {
  background-color: green;
}
<div class="footer">
  <div class="footerGridLogo">
  </div>
  <div class="footerGridQuestions">
  </div>
</div>

I realize that just adding another grid item of 10% instead of padding solves my problem, but I'm curious why padding doesn't work the same way.

Rogatory answered 24/5, 2018 at 3:4 Comment(0)
Y
36

First, instead of using percentage units use fr units. These units represent leftover space and are factored in after fixed lengths (such as padding) have been rendered.

So instead of this:

grid-template-columns: 30% 70%

Try this:

grid-template-columns: 3fr 7fr

More details:


Second, remove the width: 100% on the container.

.footer {
  background-color: #2D1975;
  width: 100%; <---------- remove
  height: 350px;
  display: grid;
  padding-left: 10%;
  grid-template-columns: 25% 65%;
}

When you set the .footer to display: grid it becomes (or remains) a block-level element. Such elements automatically occupy the full width of the parent.

However, if you add width: 100% to a block-level element it then adds any padding, margin and border to the calculation, resulting in an overflow. So just remove it.


Third, this whole problem can be avoided if you add the padding-left to the logo itself, and not to the grid container or grid item.

Yoakum answered 24/5, 2018 at 20:38 Comment(3)
what could width: 100% be replaced with, if the grid should fill the parent?Astrix
See Grid's "line-based placement" features. In particular, the grid-column shorthand property. For example: grid-column: 1 / -1 in an explicit grid.Yoakum
Removing the width: 100%; worked for me.Hughs
G
13

This is box-sizing.

The default value is content-box, which means that padding and border values are added to the width.

You can change this to border-box, which includes padding and border in the width.

Here is a common approach, from this article:

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

.footer {
  background-color: #2D1975;
  width: 100%;
  height: 350px;
  display: grid;
  padding-left: 10%;
  grid-template-columns: 25% 65%;
}

.footerGridLogo {
  background-color: red;
}

.footerGridQuestions {
  background-color: green;
}
<div class="footer">
  <div class="footerGridLogo">
  </div>
  <div class="footerGridQuestions">
  </div>
</div>
Ginoginsberg answered 24/5, 2018 at 3:41 Comment(4)
Be careful with this approach. It screwed up the width of my project pages and laravel mix (+ vuejs) couldn't detect it. Only removing it and an IDE restart fixed the problem.Deannedeans
How does restarting your IDE relate to changing a CSS property?Ginoginsberg
Don't ask me. If you use phpstorm with a Filewatcher, there might be a way for it to relate.Deannedeans
Just wanted to say, that instead of adding box-sizing to the html element you can add it to whatever you want and it works. Thanks @GinoginsbergDeannedeans
K
3

I fixed css-grid / padding problems adding next code to css:

*, *::before, *::after {
  box-sizing: border-box;
}
Karankaras answered 18/7, 2020 at 2:14 Comment(0)
J
1

Have you ever tried to remove this line

    width: 100%;

from your CSS code?

Just try and see the result.

Joinville answered 24/5, 2018 at 3:14 Comment(0)
R
1

When you do padding of an HTML element it displaces the content (actually creates space) with respect to its border enter image description here

When you do 10% padding-left to your footer, the content displaces by 10% from the left border. But you have 25% and 65% grid ratio which totals to 90%. The rest of 10% is background.

To resolve this issue use a ratio of 25% and 75% or any ratio which totals to 100%. Still the padding will cause the total width to overflow by 10%. So set the width of the footer to 90% that must resolve the overflow problem.

.footer {
	background-color: #2D1975;
	width: 90%;
	height: 350px;
	display: grid;
    padding-left:10%;
    	grid-template-columns: 25% 75%;
    }
    
    .footerGridLogo {
    	background-color: red;
    }
    
    .footerGridQuestions {
    	background-color: green;
    }
<div class="footer">
  <div class="footerGridLogo">
  </div>
  <div class="footerGridQuestions">
  </div>
</div>
Ricciardi answered 24/5, 2018 at 4:1 Comment(0)
M
1

None of the answers helped me with my problem. My grid would screw up the grid-width if I added padding. The only thing that helped me was to create a padding div inside the grid columns around the grid content:

<div class="grid">
  <div class="pad">
    content...
  </div>
</div>

<style lang="scss">
.grid {
    display: grid;
    .pad {
      padding: 1vw;
      content-styles...
    }
</style>
Mohammadmohammed answered 14/10, 2021 at 7:41 Comment(0)
A
0

You are padding 10% left of the div and then assigning the width to be 100% this is causing a %110 total including the padding.

Pad in the div above the footer and assign the contents of the child div to 100%

.container {
  padding: 10px;
}

.footer {
  background-color: #2D1975;
  width: 100%;
  height: 350px;
  display: grid;
  grid-template-columns: 25% 65%;
}

.footerGridLogo {
  background-color: red;
}

.footerGridQuestions {
  background-color: green;
}

<div class="container"> 
  <div class="footer">
    <div class="footerGridLogo"></div>
    <div class="footerGridQuestions"></div>
 </div>
</div>
Aisne answered 24/5, 2018 at 3:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.