html - 将 Div 水平和垂直居中

标签 html css alignment

我有一个像这样的 HTML:

<div class='number'><div class='n'><span>1</span></div></div>

现在,我想将它居中,如下图所示。我该怎么做?

Jsfiddle:link

enter image description here

最佳答案

这是一个 FIDDLE

.number {
  background: green;
  position: relative;
  width: 200px;
  height: 200px
}
.n {
  background: red;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -60px;
  margin-left: -60px;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-radius: 100%;
}

关于html - 将 Div 水平和垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23002054/

相关文章:

css - float div 和右对齐

html - 没有绝对定位的垂直对齐

html - 使用属性选择器向网站链接添加图标

javascript - 如何将css文件添加到pdf文档中?

jquery - CSS 扩展菜单在 IE 11 中悬停时折叠

html - 使用CSS添加按钮按下效果

CSS:h1、h2、h3 多个选择器 + 单独的 h2 ...顺序很重要吗?

Javascript:检查最小字符串长度

asp.net - 将控件标记保存到新的 html 页面并从现有的附加 css

html - IE8 html图像对齐