html - 如何让我的 div 彼此对齐?

标签 html css

如何让数字“87”位于“总分”旁边?

我想要右边的数字和左边的“总分”文本,但“总分”写成两行。

我试过使用各种 div 结构、 float 和显示属性,但无法正常工作。

<div style="text-align: center; padding: 1em; margin: 1em; background-color: #eee; border-radius: 10px;"><strong>Total<br>Score</strong>
  <div><span id="count" style="font-size: 2em;">87</span></div>
</div>

<div style="text-align: center; padding: 1em; margin: 1em; background-color: #eee; border-radius: 10px;">
  Rank #1 out of 321
</div>

最佳答案

您可以使用 Flexbox 来做到这一点:

div {
  display: flex; /* displays flex-items (children) inline */
  justify-content: center; /* centers them horizontally */
  margin: 1em;
  padding: 1em;
  background: #eee;
  border-radius: 10px;
}

div > * {margin: 0 5px} /* adjust */

#count {font-size: 2em}
<div>
  <strong>Total<br>Score</strong>
  <span id="count">87</span>
</div>

<div>Rank #1 out of 321</div>

关于html - 如何让我的 div 彼此对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50269384/

相关文章:

python - 使用 Beautiful Soup 将 CSS 属性转换为单个 HTML 属性?

jquery - 如何显示/隐藏元素 onclick jquery

html - 调整ui-view元素的高度[Angular.js Angular UI Router]

html - 无法正确排列 div

HTML/CSS - 在保持其纵横比的同时缩放图像以适应特定尺寸的可能方法?

html - 使用 HTML 和 CSS 为标题着色

javascript - 单击按钮以显示模态使按钮出现模态的 "on top"

javascript - 使用 Vis.js 的带有水平和垂直线连接器的家庭图表

android - Rackspace CloudFiles HTTP 临时 URL(非 HTTPS)

html - 下拉菜单未正确对齐