html - 左对齐和居中对齐 div 中的元素?

标签 html css

如何布局一个元素居中而另一个元素左对齐的 div 内容。

我已经尝试过 flexbox - 如果不添加隐藏的第三个元素并证明内容是不可能的。

我也尝试过在父级上对齐文本,但随后所有内容都以一种方式对齐(居中或左对齐)。

HTML:

.left{

}

.center{

}
<div class="container">
  <div class="left">
    I'm left aligned
  </div>
  <div class="center">
    I'm center aligned
  </div>
</div>

编辑

我正在对齐 div 本身,并相对于父元素居中。

最佳答案

你可以对隐藏元素使用 flex 和 :after 元素:

.container {
  display: flex;
  border: 1px solid #000;
}

.container:after {
  content: " ";
  display: block;
  flex: 1;
}

.left,
.center {
  border: 1px solid #0F0;
  flex: 1;
  text-align: center;
}
<div class="container">
  <div class="left">
    I'm left aligned
  </div>
  <div class="center">
    I'm center aligned
  </div>
</div>

关于html - 左对齐和居中对齐 div 中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46525254/

相关文章:

html - 使 CSS DIV block 响应

html - CSS 在使用文本溢出时阻止 DIV 换行 : ellipsis

javascript - 重复的 ID 值会搞乱 jQuery 选择器吗?

javascript - 页脚贴在底部

c# - 如何水平放置元素?

html - 在 html 中对选项卡进行编码

CSS Dropdown 移除文字换行

html - 滚动条淡入淡出推送文本问题

html - 使菜单响应 - Flex

html - LinkedIn 网站分享始终显示 1 分钟阅读