css - 在同一行的标题内垂直 div

标签 css vertical-alignment html-heading heading

在我不断尝试之后,我做了一些调整,允许我将 div 与 h3 放在同一行,但它没有居中。我只是想让 div 在同一行,在 h3 旁边,并具有垂直居中的 h3 文本(或 div,只要文本出现就没关系。

我猜 position:absolute; 是一个问题,但是如果我删除它,数字周围的圆圈将不再是一个圆圈(我需要保持它是一个圆圈) 现在,圆圈将位于同一行,但如果标题文本在页面上有足够的空间(一行),它会垂直顶部对齐 enter image description here

或底部,如果标题文本占两行(由于太长) enter image description here

并在圆圈和文本之间水平添加 10px。

我使用这个html:

 <h3> <div class="numberCircle">
<div class="content">24</div> </div>Smallest Personal Computer - Micro Mote </h3>

还有这个CSS:

 .numberCircle {
    border-radius: 50%;
    -webkit-box-shadow: 3px 3px 4px rgba(0,0,0,0.2);
    -moz-box-shadow: 3px 3px 4px rgba(0,0,0,0.2);
    width: 50px;
    padding: 8px;
    font-size: 32px;
    line-height: 1em;
    border: 2px solid #666;
    position: relative;

}
.numberCircle:before{
  content:"";
  display:block;
  margin-top:100%;
}
.numberCircle .content {
    position: absolute;
    left: 0;
    top: 50%;
    width: 100%;
    text-align: center;
    transform: translateY(-50%);
}

对于 h3:

 h3 {
  line-height: 1.17391em;
  color: #242d36;
  font-family: "PT Serif", Georgia, Times, serif;
  font-size: 23px;
  margin: 27px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  vertical-align:middle; 
}

最佳答案

您可以轻松地实现它,更改一点 HTML 并将 Flexbox 移动到包装器。

 .numberCircle {
    border-radius: 50%;
    -webkit-box-shadow: 3px 3px 4px rgba(0,0,0,0.2);
    -moz-box-shadow: 3px 3px 4px rgba(0,0,0,0.2);
    width: 50px;
    padding: 8px;
    font-size: 32px;
    line-height: 1em;
    border: 2px solid #666;
    position: relative;

}
.numberCircle:before{
  content:"";
  display:block;
  margin-top:100%;
}
.numberCircle .content {
    position: absolute;
    left: 0;
    top: 50%;
    width: 100%;
    text-align: center;
    transform: translateY(-50%);
}
h3 {
  line-height: 1.17391em;
  color: #242d36;
  font-family: "PT Serif", Georgia, Times, serif;
  font-size: 23px;
  margin: 27px 0;
  vertical-align:middle; 
}
.wrapper{
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="wrapper">
  <div class="numberCircle">
    <div class="content">24</div>
  </div>
  <h3>Smallest Personal Computer - Micro Mote </h3>
</div>

关于css - 在同一行的标题内垂直 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40634848/

相关文章:

css - 如何将 h1 居中放置在正文中

html - 在标题 HTML 上使用不同的字体粗细

html - 如何使 div margin-right 内的所有内容

html - 垂直对齐 div 中的 h2 和 p 标签

html - 我想把这个有问题的图片放到我的网站上,我把 CSS 代码和 HTML 都放进去了,但它仍然不起作用

HTML 停靠到底部

html - 根据父 div 调整 div 高度,然后垂直对齐 div 内容

javascript - 将静态标题保持在所有其他元素之上

css - :hover selector being detected even when behind another element

html - 如何使用 Bootstrap 将复选框与文本输入表单控件紧密对齐?