使用 place-content: center 的全寬 CSS 網格項
P粉245003607
P粉245003607 2023-09-05 17:54:47
0
2
392

我使用 CSS 網格的 place-content: center 將 div 在固定容器中垂直和水平居中,如下所示:

.outer { position: fixed; inset: 0; background: black; display: grid; place-content: center; } .inner { padding: 30px; background: white; width: 100%; max-width: 500px; }
Some content here

我希望內部 div 為全寬,最大寬度為 500px,但它似乎沒有像我期望的那樣尊重 width: 100% 屬性。

有人可以解釋為什麼會發生這種情況以及如何解決它,如果可能的話保持網格實現(我知道有其他方法可以將這樣的事情居中,但出於好奇,我想嘗試讓這種方法發揮作用比什麼都重要!)

P粉245003607
P粉245003607

全部回覆 (2)
P粉478188786

您可以使用 flex 代替 grid,這樣您就可以非常輕鬆地做到這一點。

.outer { position: fixed; width: 100%; height: 100%; background: black; display: flex; justify-content: center; align-items: center; } .inner { padding: 30px; background: white; width: 100%; }
Some content here
    P粉020085599

    您可以在網格項目上使用place-self,而不是使用place-content

    .outer { position: fixed; inset: 0; background: black; display: grid; } .inner { padding: 30px; background: white; width: 100%; max-width: 500px; place-self: center; }
    Some content here
      最新下載
      更多>
      網站特效
      網站源碼
      網站素材
      前端模板
      關於我們 免責聲明 Sitemap
      PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!