Full-width CSS grid items using place-content: center
P粉245003607
2023-09-05 17:54:47
<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>
You can use flex instead of grid so you can do this very easily.
Instead of using
place-content
, you can useplace-self
on grid items.