简体   繁体   English

垂直和水平居中网格容器

[英]Center a grid container vertically and horizontally

Surprisingly, I can't find anything about this online.令人惊讶的是,我在网上找不到任何关于此的信息。 Maybe I've missed something.也许我错过了一些东西。 How do you horizontally and vertically center your entire main wrapper with CSS Grid, and obviously, maintain responsiveness?您如何使用 CSS Grid 将整个主要包装器水平和垂直居中,并且显然保持响应能力?

 .wrapper { display: grid; grid-template-columns: minmax(100vw, 1fr); grid-template-rows: minmax(100vh, 1fr); align-items: center; justify-content: center; }
 <div class="wrapper"> <div class="centerthis">loremloremlorem</div> </div>

Using the CSS above, the div is centered vertically, but not horizontally.使用上面的 CSS, div垂直居中,但不是水平居中。

The following CSS centers the div horizontally, but not vertically:以下 CSS 将 div 水平居中,而不是垂直居中:

 .maingrid { display: grid; grid-template-columns: 1fr; grid-template-rows: 1fr; align-items: center; justify-content: center; } .centerthis { align-self: center; justify-self: center; }
 <div class="maingrid"> <div class="centerthis">loremloremlorem</div> </div>

Instead of justify-content: center use justify-items: center .而不是justify-content: center使用justify-items: center

 .wrapper { display: grid; grid-template-columns: 1fr; grid-template-rows: 100vh; align-items: center; justify-items: center; /* adjusted */ }
 <div class="wrapper"> <div class="centerthis">loremloremlorem</div> </div>

In your second example, you say:在你的第二个例子中,你说:

The following CSS centers the div horizontally but not vertically.以下 CSS 将 div 水平居中而不是垂直居中。

That's because the container has no extra height.那是因为容器没有额外的高度。 The row is the height of the content.行是内容的高度。 Instead of grid-template-rows: 1fr try something like grid-template-rows: 100vh .而不是grid-template-rows: 1fr尝试类似grid-template-rows: 100vh

 .maingrid { display: grid; grid-template-columns: 1fr; grid-template-rows: 100vh; align-items: center; justify-content: center; } .centerthis { align-self: center; justify-self: center; } body { margin: 0; }
 <div class="maingrid"> <div class="centerthis">loremloremlorem</div> </div>

More here:更多在这里:

Example for 4 items in a 2x2 grid. 2x2 网格中 4 个项目的示例。

If you want to make your items smaller within the outer container;如果您想让您的物品在外容器内变小; change width: 100% to whatever you like and add margin-top: and margin-left: in your CSS (for example: width: 70% , margin-top: 15% , margin-left: 15% . Margins being half of your left-over space, to keep things centered)width: 100%更改为您喜欢的任何值,并在您的 CSS 中添加margin-top:margin-left: (例如: width: 70% , margin-top: 15% , margin-left: 15% 。边距为一半您剩余的空间,以保持事物居中)

 #wrapper { position: absolute; display: grid; width: 100%; grid-template-columns: 1fr 1fr; }
 <div id="wrapper"> <div></div> <div></div> <div></div> <div></div> </div>

Instead of using而不是使用

  align-items: center;
  justify-items: center;

you can use您可以使用

  place-items: center;

 .wrapper { display: grid; grid-template-columns: 1fr; grid-template-rows: 100vh; place-items: center; border: 2px solid; }
 <div class="wrapper"> <div class="centerthis">loremloremlorem</div> </div>

it's better if you do如果你这样做会更好

 .wrapper { display: flex; flex-direction: row; // Not really necassary though justify-content: center; // Or you could use "space-around" align-items: center; }

I suggest you use flex-box, it is a new layout mode in CSS3.我建议你使用 flex-box,它是 CSS3 中一种新的布局模式。

In this case we just give .maingrid display: flex, a height and center the content.在这种情况下,我们只提供 .maingrid display: flex、高度和内容居中。

Then let .centerthis align itself to the middle of his father element like below.然后让 .centerthis 与他的父亲元素的中间对齐,如下所示。

Hope it help,希望有帮助,

 .maingrid { display: flex; height: 100px; justify-content: center; } .centerthis { align-self: center; }
 <div class="maingrid"> <div class="centerthis">loremloremlorem</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM