简体   繁体   English

Bootstrap 5,列未对齐

[英]Bootstrap 5, columns not aligned

I'm using Bootstrap 5 with multiple rows and columns.我正在使用具有多行和多列的Bootstrap 5 I would want my columns to be aligned with each rows.我希望我的列与每一行对齐。 This is working but some borders are not aligned with the columns这是可行的,但某些边框未与列对齐

 .VI-border { border-top: 1px solid grey; border-left: 1px solid grey; border-right: 1px solid grey; }.VI-border-top { border-top: 1px solid grey; }.VI-border-bottom { border-bottom: 1px solid grey; }.VI-border-left { border-left: 1px solid grey; }.VI-border-right { border-right: 1px solid grey; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css"> <div class="row VI-border text-center"> <div class="col-sm-1 VI-border-right VI-border-bottom" style="word-break: break-word;"> VI 10 </div> <div class="col-sm-2 VI-border-right VI-border-bottom">Ammaloramento</div> <div class="col-sm-1 VI-border-right VI-border-bottom">Gravità</div> <div class="col-sm-2 VI-border-right VI-border-bottom">Estensione</div> <div class="col-sm-3 VI-border-right VI-border-bottom">Dettaglio</div> <div class="col-sm-2 offset-sm-1 VI-border-left VI-border-bottom">Segnalazioni</div> </div> <div class="row text-center"> <div class="offset-sm-1 col-sm-2 VI-border-right VI-border-bottom VI-border-left"> Ossidazione </div> <div class="col-sm-1 VI-border-right VI-border-bottom"> Lieve </div> <div class="col-sm-2 VI-border-right VI-border-bottom"> 75 % </div> <div class="col-sm-3 VI-border-right VI-border-bottom"> In corrispondenza delle giunzioni, Forte in corrispondenza della giunzione n. 3 da imbocco Sud </div> <div class="offset-sm-1 col-sm-2 VI-border-right VI-border-bottom VI-border-left" style="height: 22px;"> Presenti: 0 </div> </div>

You can see from the snippet (open full page or expand the snippet) how the borders of the second row are not aligned with borders of first row.您可以从片段(打开整页或展开片段)中看到第二行的边框如何与第一行的边框不对齐。 The problem could be the borders themselves but I don't know how to resolve it while keeping the border.问题可能是边界本身,但我不知道如何在保持边界的同时解决它。

Any advice?有什么建议吗?

Use bootstrap 5 tables使用引导程序 5 表

 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css"> <table class="table table-bordered"> <thead> <tr> <th scope="col">Ammaloramento</th> <th scope="col">Gravità</th> <th scope="col">Estensione</th> <th scope="col">Dettaglio</th> <th scope="col">Segnalazioni</th> </tr> </thead> <tbody> <tr> <td>Ossidazione</td> <td>Lieve</td> <td>75 %</td> <td> In corrispondenza delle giunzioni, Forte in corrispondenza della giunzione n. 3 da imbocco Sud</td> <td> Presenti: 0</td> </tr> </tbody> </table>

If a table works for you that's great.如果一张table适合你,那就太好了。 Bootstrap styles them really well, However if you would like to avoid using a table then the layout you trying to achieve is totally possible. Bootstrap styles 它们非常好,但是如果您想避免使用table ,那么您尝试实现的布局是完全可能的。 So rather than telling you: don't do that , I will show you: how you can do that :所以与其告诉你:不要那样,我会告诉你:怎么那样做

The Problem(s):问题:

Your borders misalign because ie where one .row 's .col-* has a left border, the adjacent .row instead uses a right border on the previous .col-* , etc. (causing 1px offsets).您的边框未对齐,因为即一个.row.col-*具有左边框,相邻的.row反而在前一个.col-*上使用右边框等(导致1px偏移)。

<- left border here will not align <- 这里的左边框不会对齐
with right border here ->这里有右边框->

The Fix:修复:

You just need to be consistent and apply the same borders as the corresponding .col-* in each .row .您只需要保持一致并应用与每个.row中相应的.col-*相同的边框。 You can also easily style the borders with CSS variables and then use the border utility classes to apply the borders.您还可以使用CSS 变量轻松设置边框样式,然后使用边框实用程序类来应用边框。 In my example below, I also used .text-break on your first .col-* to replace the inline style, and I added .text-truncate to several .col-* s to prevent the text from overflowing the borders.在下面的示例中,我还在您的第一个.col-*上使用.text-break来替换内联样式,并将.text-truncate添加到几个.col-*以防止文本溢出边框。

 .VI-border { --bs-border-width: 1px; --bs-border-style: solid; --bs-border-color: grey; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css"> <div class="container-fluid"> <,-- rows should always be in a container. --> <div class="row VI-border border-top text-center"> <div class="col-sm-1 border-start border-bottom text-break">VI 10</div> <div class="col-sm-2 border-start border-bottom text-truncate">Ammaloramento</div> <div class="col-sm-1 border-start border-bottom text-truncate">Gravità</div> <div class="col-sm-2 border-start border-bottom text-truncate">Estensione</div> <div class="col-sm-3 border-start border-bottom border-end text-truncate">Dettaglio</div> <div class="col-sm-2 offset-sm-1 border-start border-bottom border-end text-truncate">Segnalazioni</div> </div> <div class="row VI-border text-center"> <div class="offset-sm-1 col-sm-2 border-start border-bottom text-truncate">Ossidazione</div> <div class="col-sm-1 border-start border-bottom text-truncate">Lieve</div> <div class="col-sm-2 border-start border-bottom">75 %</div> <div class="col-sm-3 border-start border-bottom border-end">In corrispondenza delle giunzioni: Forte in corrispondenza della giunzione n; 3 da imbocco Sud</div> <div class="col-sm-2 offset-sm-1 border-start border-bottom border-end" style="height: 22px ">Presenti 0</div> </div> </div>

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

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