html - 垂直对齐 Div

标签 html css vertical-alignment

我试图在我的代码中垂直对齐一个 div 但没有成功。此 div 包含子 div。第一个

我希望它看起来像这样:

enter image description here

但目前还没有对齐。这是我的 HTML 代码:

body {
  display: block;
  margin-left: auto;
  margin-right: auto;
  background: #f1f1f1;
}
.content {
  float: left;
  margin: 20px auto;
  text-align: center;
  width: 300px;
}
.content h1 {
  text-transform: uppercase;
  font-weight: 700;
  margin: 0 0 40px 0;
  font-size: 25px;
  line-height: 30px;
}
.circle {
  width: 100px;
  height: 100px;
  line-height: 100px;
  border-radius: 50%;
  /* the magic */
  -moz-border-radius: 50%;
  -webkit-border-radius: 50%;
  text-align: center;
  color: white;
  font-size: 16px;
  text-transform: uppercase;
  font-weight: 700;
  margin: 0 auto 20px;
}
.blue {
  background-color: #052D72;
}
.green {
  background-color: #16a085;
}
.red {
  background-color: #e74c3c;
}
<div class="content">
  <div class="circle blue"></div>
</div>
<div class="content">
  <div class="circle blue">Blue</div>
  <div class="circle blue"></div>
  <div class="circle blue"></div>
  <div class="circle blue"></div>
</div>
<div class="content">
  <div class="circle blue"></div>
  <div class="circle blue"></div>
</div>

所以,在 .content 中我尝试添加这个:

vertical-align:baseline;

但我看不出有什么不同。

最佳答案

#content 添加 display:inline-block 并移除 float

.content {
    display: inline-block;
    margin: 20px auto;
    text-align: center;
    vertical-align: middle;
    width: 200px;
}

https://jsfiddle.net/k0fx384a/1/

编辑类别:https://jsfiddle.net/k0fx384a/2/

关于html - 垂直对齐 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37968256/

相关文章:

javascript - 如何更改 Chart.JS 中标签的字体(系列)?

CSS:将除最后一个元素之外的所有列表元素居中

html - 如何使用 CSS 对图像进行去饱和和饱和处理?

css - 垂直居中CSS

css - 水平导航菜单中的垂直对齐图像

html - div中的垂直居中元素

image - 你更喜欢哪个?用于图形和图表的 SVG、HTML5 或 regen'd-PNG?

html - 位置 : absolute;

javascript - 如何使单选按钮显示/隐藏输入表单

html - CSS3 关键帧过渡替代点击问题