简体   繁体   English

使用网格布局-HTML将框均匀地水平放置

[英]Positioning Boxes evenly horizontally using Grid Layout - HTML

Basically I have this code: 基本上我有以下代码:

 .flex-container6 { margin-left: 20%; margin-right: 20%; margin-top: 125px; font-family: Archivo Narrow Regular; font-size: 33px; color: #fff; text-align: center; background: #EEE; } 
 <div class="flex-container6"> <div class="wrapper"> <div id="firstt">1</div> <div id="firstt2">2</div> <div id="firstt3">3</div> </div> </div> 

I gave the biggest box margins which are correct, now I'm trying to make these 3 separate boxes in the big box with 20% margins be aligned evenly horizontally. 我给出了正确的最大框边距,现在,我试图使具有20%边距的大框中的这3个单独的框水平对齐。 This means: Box 1 will be against left side of 20% margin and box 3 at the right side with 2nd box in the middle. 这意味着:框1将在左边距20%的左边,框3在右边,第二个框在中间。

Thank you for your time! 感谢您的时间!

In your class .wrapper , you can use the flexbox system, that's quit simple. 在您的.wrapper类中,可以使用flexbox系统,这很简单。 Take a look and let me know if that's what you needed. 看一看,让我知道这是否是您需要的。

 .flex-container6 { margin-left: 20%; margin-right: 20%; margin-top: 125px; font-family: Archivo Narrow Regular; font-size: 33px; text-align: center; } .wrapper { display: flex; flex-wrap: wrap; justify-content: space-between; } 
 <div class="flex-container6"> <div class="wrapper"> <div id="firstt">1</div> <div id="firstt2">2</div> <div id="firstt3">3</div> </div> </div> 

You should have the css for wrapper class like this, 您应该像这样的包装类使用CSS,

.wrapper {
  display: flex;
  flex-direction: row;
  margin-left: 20%;
  justify-content: space-between;
}

Adding margin-left in the wrapper class will push the first child to left (20% in this case). 在包装器类中添加margin-left会将第一个孩子推到左边(在这种情况下为20%)。 Then adding justify-content: space-between will spread the children in available width. 然后添加justify-content: space-between将以可用宽度扩展子级。 This will make sure that there is same gap between 1-2 and 2-3. 这样可以确保1-2和2-3之间有相同的间隔。

You can use CSS grid: 您可以使用CSS网格:

.wrapper {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

You can then use grid-gap to control the gap between the grid items. 然后,您可以使用grid-gap来控制网格项之间的间隙。

See https://css-tricks.com/snippets/css/complete-guide-grid/ for more information on CSS grid. 有关CSS网格的更多信息,请参见https://css-tricks.com/snippets/css/complete-guide-grid/

It's very simple, You need to add just below .wrapper css. 这很简单,您需要在.wrapper css下方添加。 It's the most easiest and efficient way to achieve this using display: grid; 这是使用display: grid;做到这一点的最简单,最有效的方法display: grid;

grid-template-columns: repeat(3, 1fr);

above property will make the grid area of 3 fractions and give equal width of 1 out of 3 fraction to each child grid elements. 上述属性将使网格面积为3个分数,并且每个子栅格元素的3个分数中的1个宽度相等。 Also use grid-column-gap: 10%; 也使用grid-column-gap: 10%; to give 10% margin from both sides. 给双方10%的保证金。 Total 20%. 总计20%。

 .flex-container6 { margin-left: 20%; margin-right: 20%; margin-top: 125px; font-family: Archivo Narrow Regular; font-size: 33px; color: #000; text-align: center; } .wrapper { display: grid; grid-template-columns: repeat(3,1fr); grid-column-gap: 10%; } .wrapper>div { background: #EEE; } 
 <div class="flex-container6"> <div class="wrapper"> <div id="firstt">1</div> <div id="firstt2">2</div> <div id="firstt3">3</div> </div> </div> 

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

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