html - div 内的居中元素(圆形)

标签 html css

如何使元素在圆内居中。这是现在的例子 https://codepen.io/anon/pen/JNmEVB .我需要做什么才能在 div 中居中。我正在尝试 margin: 0 auto;但没什么 :(。你们有什么想法如何做到这一点。我需要在圆圈内居中计数器

HTML

<body>
     <div id="del-countdown"> 
      <div id="clock"></div>
      <div id="units">
        <span>Hours</span>
        <span>Minutes</span>
        <span>Seconds</span>
      </div>
    </div>

  </body>

CSS

* { margin:0; padding:0; box-sizing:border-box; }
body {
  font-family: Ubuntu, sans-serif;
}

h1 {
  color: #fff;
  text-align: center;
  font-size: 74px;
  letter-spacing: 10px;
  margin-bottom: 70px;
}

#del-countdown {
  width: 600px;
  height: 600px;
  margin: 15% auto;
  background-color: rgba(255, 0, 0, 0.3);
  border-radius: 50%;
  border-style: solid;
  border-color: #0000ff;
}


#clock span {
  float: left;
  text-align: center;
  font-size: 84px;
  margin: 0 2.5%;
  color: #fff;
  padding: 20px;
  width: 20%;
  border-radius: 20px;
  box-sizing: border-box;
}
#clock span:nth-child(1) {
  background: #fa5559;
}
#clock span:nth-child(2) {
  background: #26c2b9;
}
#clock span:nth-child(3) {
  background: #f6bc58;
}

#clock:after {
  content: '';
  display: block;
  clear: both;
}

#units span {
  float: left;
  width: 25%;
  text-align: center;
  margin-top: 30px;
  color: #ddd;
  text-transform: uppercase;
  font-size: 13px;
  letter-spacing: 2px;
  text-shadow: 1px 1px 1px rgba(10, 10, 10, 0.7);
}


span.turn {
  animation: turn 0.7s ease forwards;
}

@keyframes turn {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(0deg);
  }
}

JS

"use strict";

function updateTimer(deadline) {
  var time = deadline - new Date();
  return {
    hours: Math.floor(time / (1000 * 60 * 60) % 24),
    minutes: Math.floor(time / 1000 / 60 % 60),
    seconds: Math.floor(time / 1000 % 60),
    total: time
  };
}

function animateClock(span) {
  span.className = "turn";
  setTimeout(function () {
    span.className = "";
  }, 700);
}

function startTimer(id, deadline) {
  var timerInterval = setInterval(function () {
    var clock = document.getElementById(id);
    var timer = updateTimer(deadline);

    clock.innerHTML = "<span>" + timer.hours + "</span><span>" + timer.minutes + "</span><span>" + timer.seconds + "</span>";

    var spans = clock.getElementsByTagName("span");
    animateClock(spans[2]);

    if (timer.seconds == 59){
        animateClock(spans[1]);
    }

    if (timer.minutes == 59 && timer.seconds == 59){
        animateClock(spans[0]);
    }

    if (timer.total < 1) {
      clearInterval(timerInterval);
      clock.innerHTML = "<span>0</span><span>0</span><span>0</span>";
    }

  }, 1000);
}

window.onload = function () {
  var deadline = new Date("Jan 1, 2018 12:00:00");
  startTimer("clock", deadline);
};

最佳答案

你可以使用 flexbox。

"use strict";

function updateTimer(deadline) {
  var time = deadline - new Date();
  return {
    hours: Math.floor(time / (1000 * 60 * 60) % 24),
    minutes: Math.floor(time / 1000 / 60 % 60),
    seconds: Math.floor(time / 1000 % 60),
    total: time
  };
}

function animateClock(span) {
  span.className = "turn";
  setTimeout(function () {
    span.className = "";
  }, 700);
}

function startTimer(id, deadline) {
  var timerInterval = setInterval(function () {
    var clock = document.getElementById(id);
    var timer = updateTimer(deadline);

    clock.innerHTML = "<span>" + timer.hours + "</span><span>" + timer.minutes + "</span><span>" + timer.seconds + "</span>";

    var spans = clock.getElementsByTagName("span");
    animateClock(spans[2]);
	
    if (timer.seconds == 59){
		animateClock(spans[1]);
	}
	
    if (timer.minutes == 59 && timer.seconds == 59){
		animateClock(spans[0]);
	}
	
    if (timer.total < 1) {
      clearInterval(timerInterval);
      clock.innerHTML = "<span>0</span><span>0</span><span>0</span>";
    }
	
  }, 1000);
}

window.onload = function () {
  var deadline = new Date("Jan 1, 2018 12:00:00");
  startTimer("clock", deadline);
};
* { margin:0; padding:0; box-sizing:border-box; }
body {
  font-family: Ubuntu, sans-serif;
}

h1 {
  color: #fff;
  text-align: center;
  font-size: 74px;
  letter-spacing: 10px;
  margin-bottom: 70px;
}

#del-countdown {
  width: 600px;
  height: 600px;
  margin: 15% auto;
  background-color: rgba(255, 0, 0, 0.3);
  border-radius: 50%;
  border-style: solid;
  border-color: #0000ff;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

#clock,#units {
  width: 100%;
  display: flex;
  justify-content: center;
}

#clock span {
  text-align: center;
  font-size: 84px;
  margin: 0 2.5%;
  color: #fff;
  padding: 20px;
  width: 20%;
  border-radius: 20px;
  box-sizing: border-box;
}
#clock span:nth-child(1) {
  background: #fa5559;
}
#clock span:nth-child(2) {
  background: #26c2b9;
}
#clock span:nth-child(3) {
  background: #f6bc58;
}

#clock:after {
  content: '';
  display: block;
  clear: both;
}

#units span {
  width: 25%;
  text-align: center;
  margin-top: 30px;
  color: #ddd;
  text-transform: uppercase;
  font-size: 13px;
  letter-spacing: 2px;
  text-shadow: 1px 1px 1px rgba(10, 10, 10, 0.7);
}


span.turn {
  animation: turn 0.7s ease forwards;
}

@keyframes turn {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(0deg);
  }
}
 <body>
	 <div id="del-countdown"> 
      <div id="clock"></div>
      <div id="units">
        <span>Hours</span>
        <span>Minutes</span>
        <span>Seconds</span>
      </div>
    </div>
	
  </body>

关于html - div 内的居中元素(圆形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44058504/

相关文章:

jquery - 当#div.load 出错时向div 添加一个类?(Jquery)

css - 列表样式图像 : url(svg#id) in Mozilla Firefox

javascript - 滚动浏览图片

javascript - 如何从外部调用 highcharts 按钮

javascript - 使用 Javascript 更改没有 id 的 DOM 元素的 css

javascript - Material-ui Drawer 不适用于单独的 css 文件

html - 内联 block 与边距 : 0 auto

javascript - 动态添加然后隐藏新行后维护表格备用行背景

css - 我可以将带有 '' <div class ='home-icon' 'html 的图片更改为链接中的图片吗?

javascript - 如何检测IE11并将其重定向到其他网站