Full-width CSS grid items using place-content: center
P粉245003607
P粉245003607 2023-09-05 17:54:47
0
2
493
<p>I used CSS Grid's <code>place-content: center</code> to center the div vertically and horizontally in the fixed container, like this: </p> <p> <pre class="brush:css;toolbar:false;">.outer { position: fixed; inset: 0; background: black; display: grid; place-content: center; } .inner { padding: 30px; background: white; width: 100%; max-width: 500px; }</pre> <pre class="brush:html;toolbar:false;"><div class="outer"> <div class="inner">Some content here</div> </div></pre> </p> <p>I want the inner div to be full width, with a max-width of 500px, but it doesn't seem to respect the <code>width: 100%</code> attribute as I expect. </p> <p> Can someone explain why this happens and how to fix it, if possible keeping the grid implementation (I know there are other ways to center something like this, but out of curiosity I wanted to try making this Making a difference is more important than anything else!)</p>
P粉245003607
P粉245003607

reply all(2)
P粉478188786

You can use flex instead of grid so you can do this very easily.

.outer {
  position: fixed;
  width: 100%;
  height: 100%;
  background: black;
  display: flex;
  justify-content: center;
  align-items: center;
}

.inner {
  padding: 30px;
  background: white;
  width: 100%;
}
<div class="outer">
  <div class="inner">Some content here</div>
</div>
P粉020085599

Instead of using place-content, you can use place-self on grid items.

.outer {
  position: fixed;
  inset: 0;
  background: black;
  display: grid;
}

.inner {
  padding: 30px;
  background: white;
  width: 100%;
  max-width: 500px;
  place-self: center;
}
<div class="outer">
  <div class="inner">Some content here</div>
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template