如何在CSS中让子元素水平均匀排列

4

期望效果

我在图片中展示了我想要实现的效果。

容器 div 的宽度可能会有所变化。子元素之间以及子元素与左右两侧之间应该有统一的间距。

我注意到边距会合并。我不确定是否有一种方法可以说“最小边距”,或者是否有一种方法可以说“分散开来”。

幸运的是,总会有 6 个子元素。(或者 n,但是它将被固定)

在这种情况下,我正在使用 bootstrap。我将调查行/列功能。但我不想依赖它。我更愿意找到一种适用于非 bootstrap 项目的方法,因为那将是我学习的更可移植的技术。

有没有一种方法可以在不引入布局结构的情况下实现这一点?换句话说,是否有一种仅基于规则的解决方案?

.phase .float-right {
  float: right;
}

.phase .image-carousel {
  display: table-cell;
  vertical-align:middle;
  height: 175px;
  width: 900px;
  border: 1px solid red;
  background-color: orange;
}

.phase .image-carousel .circle {
  position: relative;
  float: left;
  top: calc(0.5vh + 20px);
  height: 40px;
  width: 50px;
  border-radius: 50%;
  background-color: white;
  padding-top: 10px;
}

.arrow-left {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 15px 26.0px 15px 0;
  border-color: transparent #000000 transparent transparent;
  margin-left: 10px;
}

.arrow-right {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 15px 0 15px 26.0px;
  border-color: transparent transparent transparent #000000;
  margin-left: 15px;
}

.phase .image-carousel .thumbnail {
  float: left;
  top: calc(0.5vh - 100px);
  width: 100px;
  height: 100px;
  border: 2px solid #ddd;
  background-color: black;
}
<div class="phase angular-animate" ng-class="color($index)" ng-repeat="phase in phases">
  <div class="float-right">
    <div class="image-carousel">
      <div class="circle">
        <div class="arrow-left"></div>
      </div>
      <div class="thumbnail">
        <img src="http://dummyimage.com/100/green.png" />
      </div>
      
          <span class="thumbnail">
            <img src="http://dummyimage.com/100/green.png" />
          </span>
          <span class="thumbnail">
            <img src="http://dummyimage.com/100/green.png" />
          </span>
          <span class="thumbnail">
            <img src="http://dummyimage.com/100/green.png" />
          </span>
      <div class="circle">
        <div class="arrow-right"></div>
      </div>

    </div>
  </div>
</div>


你可以加入一个margin-right属性,如果你想让容器内的所有内容居中,你可以在容器的样式中加入text-align:center; - codetalker
2
请添加相关代码。 - Alex Char
@AlexChar,已完成,谢谢! - toddmo
3个回答

7

孩子们之间以及左右两侧应该有统一的间距。

您可以使用CSS flexbox 来组织您的布局。

以下是一个快速示例:

.wrapper {
  display: flex;
  border: 1px solid #666;
  padding: 10px;
}
.wrapper.around {
  justify-content: space-around;
}
.wrapper.between {
  justify-content: space-between;
}
.wrapper img {
  border: 1px solid #000;
}
<h1>justify-content: space-around</h1>

<div class="wrapper around">

  <img class="item" src="http://dummyimage.com/100">

  <img class="item" src="http://dummyimage.com/100">

  <img class="item" src="http://dummyimage.com/100">

  <img class="item" src="http://dummyimage.com/100">

</div>

<h1>justify-content: space-between</h1>

<div class="wrapper between">

  <img class="item" src="http://dummyimage.com/100">

  <img class="item" src="http://dummyimage.com/100">

  <img class="item" src="http://dummyimage.com/100">

  <img class="item" src="http://dummyimage.com/100">

</div>


好的回答,但我们必须支持IE9 :( 我会编辑我的问题。这让我很难过,因为这是最佳答案。 - toddmo
@toddmo 你说得对,这个解决方案的问题在于它只受现代浏览器支持!但至少你学到了新东西!我建议多了解一下它! - Romulo
我已经点赞并阅读了你的文章!迫不及待地等待微软加入这个计划!这些新功能真是太棒了 :) - toddmo

1
这样的东西会是一个好的开始吗?

.container {
  width: 80%;
  margin: 0 auto;
  background-color: #eee;
  text-align: center;
}

.item {
  display: inline-block;
  width: 15%;
  height: 60px;
  margin: 10px 2%;
  background-color: #bbb;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

这个可能会更好一些...

html, body {
    margin: 0;
    padding: 0;
}
.container {
    width: 80%;
    margin: 0 auto;
    text-align: right;
    white-space: nowrap;
    min-width: 500px;
    background-color: #eee;
    overflow: hidden;
}
.container:before {
    content: "";
    float: left;
    width: 4.5454%;
    height: 60px;
    margin-left: -0px;
}
.wrapper {
    float: left;
    width: 18.1818%;
    min-width: 80px;
    height: 60px;
    text-align: center;
    padding: 2px 0;
}
.item {
    margin-left: auto;
    margin-right: auto;
    width: 60px;
    height: 60px;
    background-color: #bbb;
}
<div class="container">
    <div class="wrapper"><div class="item"></div></div>
    <div class="wrapper"><div class="item"></div></div>
    <div class="wrapper"><div class="item"></div></div>
    <div class="wrapper"><div class="item"></div></div>
    <div class="wrapper"><div class="item"></div></div>
</div>


当屏幕变小/变大时,您还可以添加媒体查询来控制栅格和项目大小(受IE9支持)。 - Asons

0
只需在子元素上声明 margin-left。调整 margin-left 直到间距相等即可。

.container{
width:100%;
height:100px;
background-color:black;
position:relative;
}
.item{
position:relative;
display:inline-block;
background-color:white;
width:15%;
height:80px;
margin:10px 0;
margin-left:1.44%;
}
<div class="container">
  <div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div>
</div>


谢谢。当用户调整视口大小时,容器的宽度发生变化会发生什么? - toddmo
它使用百分比宽度,因此框将保持在同一行,并且边距将维持比例。高度为80px,因此框本身不成比例,但如果您使用图像而不是div,则会解决这个问题。否则,请使用脚本使高度与宽度相同。 - bazzlebrush

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接