html - flex 上的 div 文本对齐

标签 html css flexbox

如何对齐 div,使两个标签具有相同的宽度并右对齐,并且两个内容都从同一位置开始。有些人建议使用 float,但我不喜欢 float 内容。有没有一种灵活的方式来做到这一点。

enter image description here

<!DOCTYPE HTMML>
<html>
<body>
    <div>
        <div style="display: flex; flex-direction:row;">
            <div style="align: right; background: blue">
                Long label:
            </div>
            <div style="text-align: left; background: green">
                This is the content
            </div>
        </div>  

        <div style="display: flex; flex-direction:row;">
            <div style="align: right; background: blue">
                label:
            </div>
            <div style="text-align: left; background: green">
                This is the content
            </div>
        </div>  
    </div>
</body>
</html>

最佳答案

使用 flexbox...NO...除非您使用固定宽度值(无论它们是什么),否则您不能...非同胞之间没有宽度/高度均衡。

您需要为其中一个元素指定固定宽度值,然后使用 flex:1 让另一个元素占据剩余空间。

.blue {
  background: lightblue;
  /* width: 150px; */
  flex: 0 0 150px
}
.green {
  background: #bada55;
  flex: 1;
}
.parent {
  display: flex;
}
<div class="parent">
  <div class="blue">
    Long label:
  </div>
  <div class="green">
    This is the content
  </div>
</div>

<div class="parent">
  <div class="blue">
    label:
  </div>
  <div class="green">
    This is the content
  </div>
</div>

关于html - flex 上的 div 文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35937253/

相关文章:

javascript - 从 textarea 编辑 Javascript

html - 如何写一个quomma

html - Css高度问题

html - 如何使我的 flex 列在绝对定位的容器中具有相同的高度?

CSS flex 盒 : Align last nth items towards the end

html - Flexbox 布局在 IE 11 中被破坏。前缀不起作用?

javascript - 如何重定向到 mypage.com#?test=1 使用 html 表单 + javascript?

html - 链接不重定向

css - 操纵第 3 方 *.less css 而不破解它

html - 使 Fieldset 边框匹配 TextArea