简体   繁体   English

父级中对齐的内联块元素

[英]justified inline-block element in parent

I have a container with a width of 1200px and 4 inline-block children of 250px width each. 我有一个宽度为1200px的容器,每个容器有4个250px宽度的内联块子元素。

Is there anyway I can justify them inside the parent? 无论如何,我可以在父母内部为他们辩护吗? For example I would like the first element to have no margin on the left and the last element to have no margin on the right, and the margin between the elements to be equal. 例如,我希望第一个元素的左边没有空白,而最后一个元素的右边没有空白,并且元素之间的空白相等。

Use display flex on the container 在容器上使用display flex

.container { 
   display:flex; /* make the container a flex container */
   justify-content: space-between /* justify the flex items on the main axis */
}

See the spec for more detials 有关更多细节,请参见规格

Of course, using flexbox: 当然,使用flexbox:

HTML 的HTML

<div class="parent">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>

CSS 的CSS

.parent{
    display:flex;
    flex-direction: row;
    justify-content: space-between;
}

Learn more about flexbox 了解有关Flexbox的更多信息

You can also try: 您也可以尝试:

HTML the same as above HTML与上面相同

CSS 的CSS

.parent{
   display:table
}

.parent > .child{
   display: table-cell;
   float: none;
}

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

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