【问题标题】:Can't get justify-content: space-between to work in Bootstrap framework [duplicate]无法在 Bootstrap 框架中使用 justify-content: space-between [重复]
【发布时间】:2017-04-25 01:33:45
【问题描述】:

我怎样才能让下面的 sn-p 看起来像这样:

使项目均匀分布,第一项在左侧没有空白空间,最后一项在右侧没有空白空间。类似于 MS Word 的功能:

充分理由

段落中的所有行都被扩展,因此它们对两者都很整齐 左边距和右边距。 为了扩大行,必须在单词和单词之间添加空格 字符,填写该行。

在下面的代码片段中,第一项左侧有很多紫色空间,我想删除它。

ul {
  list-style: none;
  background-color: rebeccapurple;
  color: white;
  margin: 55px 10px;
  display: flex; 
  // align-items: stretch;
  justify-content: space-between;
}
li {
  padding: 0;
  background-color: #6495ed;
}
li:nth-of-type(2n) {
  background: lightslategrey;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/dist/css/bootstrap.css" rel="stylesheet"/>
<ul class="nav navbar-nav">
	<li>Item</li>
	<li>Item</li>
	<li>Item</li>
	<li>Item</li>
	<li>Item</li>
	<li>Item</li>
</ul>

更新

@kukkuz 下面的答案解决了代码片段,但我还必须添加以下内容才能使其与 twitter Bootstrap 一起使用。 Bootstrap 正在添加 :before:after 元素,这些元素让我搞砸了。

删除它们对我有用,但我建议使用更具体的选择器 ul 否则你可能会用 Bootstrap 搞砸。

仅供参考 - 我使用的是来自这个 CDN 的 Bootstrap,v3.3.7: https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/dist/css/bootstrap.css

ul:before { 
    content: none;
}
ul:after {
    content: none;
}

【问题讨论】:

标签: html twitter-bootstrap css flexbox


【解决方案1】:

重置ul 元素的用户代理/浏览器padding - 请参见下面的演示:

ul {
  list-style: none;
  background-color: rebeccapurple;
  color: white;
  margin: 55px 10px;
  padding: 0;
  display: flex;
  /*align-items: stretch;*/
  justify-content: space-between;
}
li {
  padding: 0;
  background-color: #6495ed;
}
li:nth-of-type(2n) {
  background: lightslategrey;
}
<ul>
  <li>Item</li>
  <li>Item</li>
  <li>Item</li>
  <li>Item</li>
  <li>Item</li>
  <li>Item</li>
</ul>

【讨论】:

  • 打败我吧 :)
  • @kukkuz,这是有道理的,但它对我不起作用,我意识到这是因为我使用的是 Bootstrap 并且有 :before:after 元素让我搞砸了。我不得不添加ul:before { content: none; } ul:after { content: none; } 来修复它,你能把它添加到你的答案中,以防它帮助别人吗?
  • 你确定它来自引导程序吗...我尝试添加引导程序 css 并找不到 after/before 样式..
  • @kukkuz,是的,这可能是因为我在ul 上也有课程nav navbar-nav,抱歉应该在我的问题中更具体,只是根本没有怀疑 Bootstrap
  • 哦,现在我明白为什么那里有伪了 :)
猜你喜欢
  • 2018-03-25
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 2016-04-01
  • 2022-12-04
  • 2021-01-17
相关资源
最近更新 更多