简体   繁体   English

如何在Bootstrap网格中添加列表项之间的差距?

[英]How to add gap between list items in Bootstrap grid?

I'm trying to make a two tiered horizontal list in bootstrap. 我正在尝试在bootstrap中创建一个两层水平列表。 It's like a secondary nav that will sit below the page header (so it won't be wrapped in nav tags). 它就像是位于页眉下方的辅助导航(因此它不会包含在导航标签中)。

I'm trying to do it using a list, but am having trouble with the spacing. 我正在尝试使用列表,但我的间距有问题。 Since it needs to be 4 items wide (4 on top, 4 on bottom) I'm making each list-item into a col-md-3 (because 12/4 = 3). 因为它需要4个项目宽(顶部4个,底部4个)我将每个列表项目变成col-md-3(因为12/4 = 3)。

So far, I can get them to line-up horizontally but I can't figure out a way to get any separation between them without pushing the 4th item down to the next level. 到目前为止,我可以让他们水平排队,但我无法找到一种方法来获得它们之间的任何分离,而无需将第4项推到下一级。 I've been using margin to get some space between them, but like I said, that only breaks it. 我一直在使用保证金来获得它们之间的一些空间,但就像我说的那样,只会破坏它。

Here's the code... 这是代码......

 .secondary-nav-container { ul.list-group:after { clear: both; display: block; content: ""; } .list-group-item { float: left; border-left: 0 none; border-right: 0 none; border-bottom: 0 none; margin: 0px; padding: 10px 0px; } a { //nothing here yet } } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <div class="secondary-nav-container container"> <div class="row"> <ul class="list-group"> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> </ul> </div> </div> 

The goal is to have two rows with 4 list-items in each that takes up the full width of the row, and stacks on top of each other. 目标是有两行,每行有4个列表项,占用行的整个宽度,并堆叠在一起。 With spacing between each list-item so it doesn't look like there is just one giant horizontal line across the top of each row — there needs to be space between each list-items horizontal line at the top. 每个列表项之间的间距使得看起来不像每行顶部只有一条巨大的水平线 - 顶部的每个列表项水平线之间需要有空格。

It's best to not mix grid elements and style elements. 最好不要混合网格元素和样式元素。 Since your list requires it, you'll need to move some styles to the anchors rather than the list items: 由于您的列表需要它,您需要将一些样式移动到锚点而不是列表项:

 ul.list-group:after { clear: both; display: block; content: ""; } .list-group-item { float: left; border: 0 !important; margin: 0px; padding: 10px 0px; } a { display: block; padding: 5px; border: 1px solid pink; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <div class="secondary-nav-container container"> <div class="row"> <ul class="list-group"> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> <li class="col-xs-3 list-group-item"><a href="#">One</a></li> </ul> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM