简体   繁体   English

对齐元素与网格和 flex-box,CSS & Html

[英]Aligning elements with grid and flex-box, CSS & Html

I'm trying to find a way to push the last row of the gird to the left so it doesn't stay centered but aligns with the grid.我试图找到一种方法将网格的最后一行推到左边,这样它就不会保持居中而是与网格对齐。 Thanks for any help in advanced:)感谢您提供高级帮助:)

image example bellow:图片示例如下:

在此处输入图像描述

Here is my code:这是我的代码:

 #grid { display: grid; grid-gap: 77px; grid-template-columns: 1fr 1fr 1fr 1fr; padding-top: 114px; list-style-type: none; clear: both; display: flex; flex-wrap: wrap; justify-content: center; } #grid>div { border: solid 2px black; height: 208px; width: 370px; justify-content: flex-start; }
 <section id="grid"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </section>

You are justifying to the center.你在为中心辩护。 You need to change that for flex-start .您需要为flex-start更改它。

#grid {
  justify-content: flex-start;
}

You can learn more about flexbox here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/您可以在此处了解有关 flexbox 的更多信息: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

You override the grid property with a display: flex .您可以使用display: flex覆盖grid属性。

Without it, every block go the the left but the whole grid isn't centered anymore, to adjust that, you can add justify-items: center没有它,左边的每个块 go 但整个网格不再居中,要调整它,您可以添加justify-items: center

Demo (I changed the block sizes to have a better preview)演示(我更改了块大小以获得更好的预览)

 #grid { display: grid; grid-gap: 77px; grid-template-columns: 1fr 1fr 1fr 1fr; padding-top: 114px; list-style-type: none; clear: both; /* display: flex; <-- HERE */ flex-wrap: wrap; justify-content: center; /* new rule for centering */ justify-items: center; /* preview */ grid-gap: 1em; padding: 1em; } #grid>div { border: solid 2px black; height: 208px; width: 370px; justify-content: flex-start; /* preview */ height: 40px; width: 100px; } /* preview */ body { margin: 0 }
 <section id="grid"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </section>

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

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