Flex容器CSS:如何将div元素居中?

时间:2017-07-24 18:21:43

标签: html css css3 flexbox containers

我有以下HTML / CSS代码,我有一个问题是将内容集中在Flex容器中。结果是:

ko result

在我看来,我想将环放在flex容器中心,并在环形动画内的温度之后将文本(温度)和CSS动画(箭头)居中。

怎么做?

body {
  background-color: #0d74ff;
}

.container {
  padding: 20px;
}

.flexwrap {
  display: flex;
  flex-wrap: wrap;
}

.cell {
  position: relative;
  height: 200px;
  width: 200px;
  border: 1px solid rgba(0, 0, 0, 0.25);
}

.loader-ring {
  width: 150px;
  height: 150px;
}

.loader-ring-light {
  width: 150px;
  height: 150px;
  border-radius: 150px;
  box-shadow: 0 4px 0 #ffffff inset;
  animation: rotate-360 6s linear infinite;
}

.loader-text {
  font-weight: bold;
  font-size: 2em;
}

.scroll-down {
  border: 2px solid #fff;
  border-radius: 100px;
  width: 30px;
  height: 30px;
}

.scroll-down i {
  display: block;
  border-radius: 100px;
  transition: all 0.4s ease;
  width: 100%;
  height: 100%;
  background-size: 0 auto;
  animation: pulse 1.5s 0s infinite normal ease forwards;
  background-image: url("https://jamesmuspratt.com/codepen/img/arrow-down.svg");
  background-repeat: no-repeat;
}

@keyframes rotate-360 {
  from {
    transform: rotate(0);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  0% {
    opacity: 0;
    background-position: center top;
    background-size: 0 auto;
  }
  10% {
    opacity: 0;
  }
  50% {
    opacity: 1;
    background-size: 75% auto;
  }
  90% {
    opacity: 0;
  }
  100% {
    opacity: 0;
    background-position: center bottom;
    background-size: 0 auto;
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">

<div class="container">
  <div class="flexwrap">
    <div class="cell">
      <div class='loader-ring'>
        <div class='loader-ring-light'></div>
        <div class='loader-ring-track'>
          <div class='loader-text'>26.6&deg;</div>
          <div class="scroll-down"><i></i></div>
        </div>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

这里你不需要这么多块。您需要绝对定位,因为元素需要相互堆叠。另外,对于定位绝对定位的元素,您需要这种样式

.loader-ring,
.loader-ring-track {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

演示:

body {
  background-color: #0d74ff;
}

.container {
  padding: 20px;
}

.cell {
  position: relative;
  height: 200px;
  width: 200px;
  border: 1px solid rgba(0, 0, 0, 0.25);
}

 /* center absolutely positioned items */
 /* both vertically and horizontally */
.loader-ring,
.loader-ring-track {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.loader-ring-track {
  display: flex; /* new */
}

.loader-ring {
  width: 150px;
  height: 150px;
}

.loader-ring-light {
  width: 150px;
  height: 150px;
  border-radius: 150px;
  box-shadow: 0 4px 0 #ffffff inset;
  animation: rotate-360 6s linear infinite;
}

.loader-text {
  font-weight: bold;
  font-size: 2em;
}

.scroll-down {
  border: 2px solid #fff;
  border-radius: 100px;
  width: 30px;
  height: 30px;
}

.scroll-down i {
  display: block;
  border-radius: 100px;
  transition: all 0.4s ease;
  width: 100%;
  height: 100%;
  background-size: 0 auto;
  animation: pulse 1.5s 0s infinite normal ease forwards;
  background-image: url("https://jamesmuspratt.com/codepen/img/arrow-down.svg");
  background-repeat: no-repeat;
}

@keyframes rotate-360 {
  from {
    transform: rotate(0);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  0% {
    opacity: 0;
    background-position: center top;
    background-size: 0 auto;
  }
  10% {
    opacity: 0;
  }
  50% {
    opacity: 1;
    background-size: 75% auto;
  }
  90% {
    opacity: 0;
  }
  100% {
    opacity: 0;
    background-position: center bottom;
    background-size: 0 auto;
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">

<div class="cell">
  <div class='loader-ring'>
    <div class='loader-ring-light'></div>
    <div class='loader-ring-track'>
      <div class='loader-text'>26.6&deg;</div>
      <div class="scroll-down"><i></i></div>
    </div>
  </div>
</div>