简体   繁体   English

我可以用纯 CSS 居中自动填充网格吗?

[英]Can I center an auto-fill grid with pure CSS?

I'm trying to fill my page horizontally with as many blocks as possible and center the result.我试图用尽可能多的块水平填充我的页面并将结果居中。

I want the grid to be able to resize when the window becomes smaller:我希望网格能够在 window 变小时调整大小:

wide window
xxx

small window
xx
x

Is this possible to achieve without javascript?如果没有 javascript,这有可能实现吗?

 .box { box-sizing: border-box; width: 200px; height: 150px; background-color: white; border: solid 6px red; }.box:nth-child(2) { background-color: blue }.grid { display: grid; grid-template-columns: repeat(auto-fill, 200px); }
 <div class="center"> <div class="grid"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div>

To explain the desired centering visually:直观地解释所需的居中:

Can the yellow highlighted area be evenly distributed on either side?黄色高亮区域能否均匀分布在两侧? 在此处输入图像描述


The desired box alignment:所需的盒子 alignment: 在此处输入图像描述

Try this with flexbox.用 flexbox 试试这个。

 .box { box-sizing: border-box; width: 200px; height: 150px; background-color: white; border: solid 6px red; } .box:nth-child(2) { background-color: blue } .grid { display: flex; justify-content: center; align-items: center; } @media screen and (max-width: 767px) { .grid { flex-wrap: wrap; justify-content: center; } .box { width: 50% } }
 <div class="center"> <div class="grid"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div>

This only works in chrome for some reason.出于某种原因,这只适用于 chrome。


Auto-fit and auto-fill needs a known width to calculate from, we can do that with max-width:100% instead of using width which will stretch it and prevent us from centering, and avoid fixed widths.自动适应和自动填充需要一个已知的宽度来计算,我们可以用max-width:100%来做到这一点,而不是使用width会拉伸它并阻止我们居中,并避免固定宽度。

 .box { box-sizing: border-box; width: 200px; height: 150px; background-color: white; border: solid 6px red; } .box:nth-child(2) { background-color: blue } .grid { display: grid; grid-template-columns: repeat(auto-fill, 200px); max-width: 100%; } .center { display: flex; flex-direction: column; align-items: center; }
 <div class="center"> <div class="grid"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div>


There is a quirk with this, if you start with a wide viewport then resize it to a smaller one, the grid won't be centerd because of auto-fill这有一个怪癖,如果您从宽viewport开始,然后将其调整为较小的viewport ,由于auto-fill ,网格将不会居中

Say you have 3 elements but the container can fit 4 with auto-fill it will create a forth column but we have no forth element so it will look unevenly spaced.假设您有 3 个元素,但容器可以通过auto-fill容纳 4 个元素,它将创建第四列,但我们没有第四个元素,因此它看起来间隔不均匀。

I suggest using auto-fit , which instead of creating the forth column it will split the space evenly on each side.我建议使用auto-fit ,而不是创建第四列,它会在每一侧均匀地分割空间。

 .box { box-sizing: border-box; width: 200px; height: 150px; background-color: white; border: solid 6px red; } .box:nth-child(2) { background-color: blue } .grid { display: grid; grid-template-columns: repeat(auto-fit, 200px); max-width: 100%; } .center { display: flex; flex-direction: column; align-items: center; }
 <div class="center"> <div class="grid"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div>


Now if you resize the window enough you'll see sometimes it's not evenly spaced that's because of the relative unit on the max-width, it need to recalculate the width because it's based on the parent and parent's width which is based on the content.现在,如果您足够地调整窗口大小,您有时会看到它的间距不均匀,这是因为最大宽度的相对单位,它需要重新计算宽度,因为它基于父级和基于内容的父级宽度。

we can trigger that recalculation using an animation.我们可以使用动画触发重新计算。

chrome specific solution铬特定解决方案

 .center { display: flex; flex-direction: column; align-items: center; } .box { box-sizing: border-box; width: 200px; height: 150px; background-color: white; border: solid 6px red; } .box:nth-child(2) { background-color: blue } .grid { display: grid; grid-template-columns: repeat(auto-fit, 200px); max-width: 100%; animation: recalc 5s linear infinite; } @keyframes recalc { to { max-width: 99.9%; } }
 <div class="center"> <div class="grid"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div>


To support at least FF and maybe some other browsers, use viewport units.要至少支持 FF 或其他一些浏览器,请使用视口单位。

 .box { box-sizing: border-box; width: 200px; height: 150px; background-color: white; border: solid 6px red; } .box:nth-child(2) { background-color: blue } .grid { display: grid; grid-template-columns: repeat(auto-fit, 200px); max-width: 100vw; margin:0 auto; } .center { display: flex; }
 <div class="center"> <div class="grid"> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div>

I wouldn't use grid for this.我不会为此使用grid Use flex :使用flex

 *{ box-sizing:border-box; padding:0; margin:0; font-size:0; } html,body{ width:100%; height:100%; background:#ccc; } .box{ width:200px; height:150px; background-color:white; border:6px solid red; } .box:nth-child(2n+2) { background-color:blue; } .center,.grid{ display:flex; align-items:center; justify-content:center; height:100%; } .grid{ min-width:600px; flex-wrap:wrap; align-items:center; }
 <div class='center'> <div class='grid'> <div class='box'></div> <div class='box'></div> <div class='box'></div> <div class='box'></div> <div class='box'></div> </div> </div>

Yes, you can centre a left-aligned grid (and it works in Firefox).是的,您可以将左对齐的网格居中(这在 Firefox 中有效)。

To do this, you just need justify-content: center;为此,您只需要justify-content: center; on the grid container, which would give you this:在网格容器上,这会给你这个:

在此处输入图像描述

A good example of this (and why it works) is on the MDN docs page for justify-content一个很好的例子(以及为什么它有效)在MDN 文档页面上的 justify-content

Your example would look like this:您的示例如下所示:

 .box { box-sizing: border-box; width: 200px; height: 150px; background-color: white; border: solid 6px red; }.box:nth-child(2) { background-color: blue }.grid { display: grid; grid-template-columns: repeat(auto-fill, 200px); justify-content: center; /* <-- This is all you need to add */ }
 <div class="center"> <div class="grid"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div>

It's worth noting though, that when you only have one row of boxes, they may not be centred, since the repeat function will create empty columns to fill the row, effectively left aligning the actual boxes:但值得注意的是,当你只有一排框时,它们可能不会居中,因为重复 function 将创建空列来填充行,有效地左对齐实际框:

在此处输入图像描述

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

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