简体   繁体   English

使用Flex Box网格系统的列之间没有空间

[英]No space between columns using the Flex Box Grid system

Using the Flex Box Grid system . 使用Flex Box网格系统 What is the correct way of removing the spacing between two 50% width columns? 删除两个50%宽度的列之间的间距的正确方法是什么?

Is there a standard class available for this? 是否有为此提供的标准课程? I can't find one on the official website. 我在官方网站上找不到。

<div class="container">
    <div class="row">
        <div class="col-md-6">
            col 1
        </div>
        <div class="col-md-6">
            col 2
        </div>
    </div>
</div>

I have not used this grid system, but I can see from inspecting the elements in my browser's dev tools that the space between elements is created with the padding-right and padding-left properties. 我没有使用这个网格系统,但是通过检查浏览器的开发工具中的元素,我可以看到元素之间的空间是使用padding-right和padding-left属性创建的。 You could try adding another class name to your nested elements, and write rules for that class which override the padding-right and padding-left. 您可以尝试在嵌套元素中添加另一个类名称,然后为该类编写规则以覆盖padding-right和padding-left。

The css would look something like this. CSS看起来像这样。 Your selector should have a higher specificity than the selectors applied by Flex Box Grid System. 您的选择器应比Flex Box网格系统应用的选择器具有更高的特异性。 If you need to learn about specificity, here is a good guide: https://stuffandnonsense.co.uk/archives/css_specificity_wars.html 如果您需要了解特异性,请参考以下指南: https : //stuffandnonsense.co.uk/archives/css_specificity_wars.html

.container .row .noPadding {
    padding-right: 0;
    padding-left: 0;
}

The html would look like this: html看起来像这样:

<div class="container">
    <div class="row">
        <div class="noPadding col-md-6">
            col 1
        </div>
        <div class="noPadding col-md-6">
            col 2
        </div>
    </div>
</div>

Alternatively, you could refrain from applying classes from the Flex Box Grid System on the two elements that you don't want spaced, and just use the css spec's flex property. 或者,您可以避免将Flex Box Grid System中的类应用于您不想隔开的两个元素,而仅使用css spec的flex属性。 Here is a concise guide to css flex: https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 这是CSS Flex的简要指南: https : //css-tricks.com/snippets/css/a-guide-to-flexbox/

One way is to use the child-no-padding class in the row div: 一种方法是在div row使用child-no-padding类:

.child-no-padding>div {
  padding: 0 !important;
}

By this way you prevent from repetitive no-padding class name in the child elements. 通过这种方式,可以防止子元素中重复的no-padding类名。

Sample: 样品:

 .child-no-padding>div { padding: 0 !important; } .col-2{ background: #00fff5; border: 1px solid #6cccca; text-align:center; font-size:.8em; color:white; } 
 <div class="container"> <div class="row child-no-padding"> <div class="col-2"> col 1 </div> <div class="col-2"> col 2 </div> <div class="col-2"> col 3 </div> <div class="col-2"> col 4 </div> <div class="col-2"> col 5 </div> <div class="col-2"> col 6 </div> </div> </div> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> 

Two options: 两种选择:

1) If you need to remove the row-column spacing (gutters) globally 1)如果需要全局删除行-列间距(装订线)

You can directly change the :root variables in flexboxgrid.css or override them in your custom css file. 您可以直接在flexboxgrid.css中更改:root变量,也可以在自定义css文件中覆盖它们。

:root {
    --gutter-compensation: 0; // remove negative margins for row
    --half-gutter-width: 0; // remove padding from columns
}

2) You can use a utility class, if you need to remove spacing only in a specific section 2)如果只需要删除特定部分中的间距,则可以使用实用程序类

Borrowing the concept from Bootstrap , you can just add a class which removes negative margin from .row and padding from .col-* , 借用Bootstrap的概念,您只需添加一个类即可删除.row中的负边距和.col-* padding,

.no-gutters {
  margin-right: 0;
  margin-left: 0;

  > [class*="col-"] {
    padding-right: 0;
    padding-left: 0;
  }
}

Then add .no-gutters class with .row . 然后将.no-gutters .row类与.row添加.row

<div class="container">
    <div class="row no-gutters">
        <div class="col-md-6">
            col 1
        </div>
        <div class="col-md-6">
            col 2
        </div>
    </div>
</div>

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

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