【问题标题】:Flex Container CSS : How to center div element?Flex Container CSS:如何使 div 元素居中?
【发布时间】:2017-07-24 18:21:42
【问题描述】:

我有以下 HTML/CSS 代码,但在将内容置于 flex 容器内居中时遇到了问题。结果是:

在我看来,我想在 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>

【问题讨论】:

    标签: html css flexbox containers


    【解决方案1】:

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

    .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>

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2013-08-29
      • 2020-12-16
      • 2014-09-14
      • 1970-01-01
      • 2013-07-28
      相关资源
      最近更新 更多