【问题标题】:What is the best way to align content left and right in a header while using flexbox?使用 flexbox 时在标题中左右对齐内容的最佳方法是什么?
【发布时间】:2017-04-19 10:18:38
【问题描述】:

我正在努力寻找合适的关键字来解决这个问题。

这是我的有效代码:

HTML

 <div class="windowTopBar">
   <span style="display: inline-block;"><b>System Properties</b></span>
   <span style="float: right;">[?][X]</span>
 </div>

CSS

 .windowTopBar {
   background: linear-gradient(90deg, #000080, #1084D0);
   color: white;
   height: 2em;
   min-height: 2em;
   align-items: center;
   margin: 0;
 }

这样可以将“系统属性”浮动到左侧,将“两个 img 按钮”浮动到右侧。

如果我将跨度样式放入类中,它将停止工作。为什么?

如果我输入“display: flex;”进入父div,它也停止工作了吗?为什么?我真的很想使用 display flex。实际上,我已经搞砸了很长时间。

到目前为止,我最喜欢的解决方案是在父级上执行 display: flex 和 justify-content: flex-end (同时使用两个子 div 而不是跨度),但我不知道如何浮动“系统属性”在左侧。

我正在努力使其具有响应性,因此如果有人能帮助我理解最佳实践是什么,并明确帮助我理解为什么 span 样式属性在 html 中有效,但在 css 类中无效,我将不胜感激。

要特别清楚,如果我这样做:

HTML

 <span class="test1">blah1</span>
 <span class="test1">blah2</span>

CSS

 .test1 {
   display: inline-block;
 }
 .test2 {
   float: right;
 }

它不起作用。请帮忙! :) 如果我要求太多,请原谅我。这是我的第一个问题,但我已经潜伏多年了。

【问题讨论】:

标签: css header alignment flexbox


【解决方案1】:

我会使用justify-content: space-between:

.windowTopBar {
  display: flex;
  justify-content: space-between;
}
 <div class="windowTopBar">
   <span><b>System Properties</b></span>
   <span>[?][X]</span>
 </div>

【讨论】:

  • 谢谢,其实我自己也发现了同样的东西,呵呵。
  • 由于所有使用疯狂表格方法和浮动的旧问题,我在 Google 上遇到了困难。 flex space-between 是一个宝石。我会将关键字添加到我的帖子中,希望有一天有人能找到它。
猜你喜欢
  • 2010-10-18
  • 1970-01-01
  • 2011-05-26
  • 1970-01-01
  • 2016-09-05
  • 2015-08-13
  • 2015-05-09
  • 1970-01-01
  • 2017-06-16
相关资源
最近更新 更多