search

Home  >  Q&A  >  body text

Full-width CSS grid items using place-content: center

<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粉245003607P粉245003607445 days ago464

reply all(2)I'll reply

  • P粉478188786

    P粉4781887862023-09-06 14:48:14

    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>

    reply
    0
  • P粉020085599

    P粉0200855992023-09-06 12:43:41

    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>

    reply
    0
  • Cancelreply