如何使用css将div移动到其他div内的中心?

时间:2018-04-18 06:41:46

标签: html css

如何移动

<div class="rate1"></div>
<div class="info"></div>
<div class="rate2"></div>

到div class="bottom"的中心?

.bottom {
  float: left;
  width: 100%;
  border: 1px solid #ccc;
}

.rate1 {
  width: 70px;
  height: 70px;
  border-radius: 70px;
  border: 5px solid #f0f0f0;
  float: left;
  box-shadow: 1px 1px 1px 0px #e9e9e9;
  background: url(http://web.arjentienkamp.com/codepen/tinder/delete.png);
  margin-left: 4px;
  background-size: 25px;
  background-position: center;
  background-repeat: no-repeat;
}

.info {
  width: 40px;
  height: 40px;
  float: left;
}

.rate2 {
  width: 70px;
  height: 70px;
  border-radius: 70px;
  border: 5px solid #f0f0f0;
  float: left;
  box-shadow: 1px 1px 1px 0px #e9e9e9;
  background: url(http://web.arjentienkamp.com/codepen/tinder/heart.png);
  margin-left: 4px;
  background-size: 25px;
  background-position: center;
  background-repeat: no-repeat;
}
<div class="bottom">
  <div class="rate1"></div>
  <div class="info"></div>
  <div class="rate2"></div>
</div>

2 个答案:

答案 0 :(得分:4)

使用 flexbox

&#13;
&#13;
.bottom{
    width: 100%;
    border: 1px solid #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
}
.rate1{
    width: 70px;
    height: 70px;
    border-radius: 70px;
    border: 5px solid #f0f0f0;
    box-shadow: 1px 1px 1px 0px #e9e9e9;
    background: url(http://web.arjentienkamp.com/codepen/tinder/delete.png);
    margin-left: 4px;
    background-size: 25px;
    background-position: center;
    background-repeat: no-repeat;
}
.info{
    width: 40px;
    height: 40px;
}
.rate2{
    width: 70px;
    height: 70px;
    border-radius: 70px;
    border: 5px solid #f0f0f0;
    box-shadow: 1px 1px 1px 0px #e9e9e9;
    background: url(http://web.arjentienkamp.com/codepen/tinder/heart.png);
    margin-left: 4px;
    background-size: 25px;
    background-position: center;
    background-repeat: no-repeat;
}
&#13;
<div class="bottom">
    <div class="rate1"></div>
    <div class="info"></div>
    <div class="rate2"></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用flexbox轻松完成此操作:

.bottom {
  display : flex;
  justify-content : center;
}

您还应该考虑删除内部div中的float : left;

您还可以查看css-tricks的centering guide以了解其他用例。