简体   繁体   English

如何在 bootstrap 5 表格单元格中垂直对齐文本?

[英]how to vertically align a text in a bootstrap 5 table cell?

I created a table and I want one of the cells to have a vertical alignment.我创建了一个表格,我希望其中一个单元格具有垂直 alignment。 The bootstrap 5 documentation states : bootstrap 5 文档指出

Change the alignment of elements with the vertical-alignment utilities.使用垂直对齐实用程序更改元素的 alignment。 Please note that vertical-align only affects inline, inline-block, inline-table, and table cell elements请注意,vertical-align 仅影响 inline、inline-block、inline-table和表格单元格元素

I just made a small comparison table, to demonstrate my disarray:我只是做了一个小比较表,以展示我的混乱:

 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="table table-dark"> <tbody id="table"> <tr class="row"> <td class="col-sm-2 align-middle">not aligned</td> <td class="col-sm-1"> <button> <div class="row"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Flag_of_Papua_New_Guinea.svg/320px-Flag_of_Papua_New_Guinea.svg.png" width="64px" height="48px"> </div> </button> </td> <td class="col-sm-9" colspan="9"></td> </tr> </tbody> </table> <table class="table table-dark"> <tbody> <tr> <td class="col-sm-2 align-middle">aligned</td> <td class="col-sm-1"> <button> <div class="row"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Flag_of_Papua_New_Guinea.svg/320px-Flag_of_Papua_New_Guinea.svg.png" width="64px" height="48px"> </div> </button> </td> <td class="col-sm-9" colspan="9"></td> </tr> </tbody> </table>

So why does the first table not have a vertically correctly aligned text like the second one?那么为什么第一个表格没有像第二个表格那样垂直正确对齐的文本呢?

I also tried to add a flex box: <div class="d-flex align-items-center">...</div> .我还尝试添加一个弹性框: <div class="d-flex align-items-center">...</div> Same result.结果相同。 I need that class="row" in my tr because of horizontal alignment of table elements.由于表格元素的水平 alignment ,我在我的tr中需要那个class="row" How can I still get vertical aligned items in my first cell?我怎样才能在我的第一个单元格中获得垂直对齐的项目?

I assume you mean both horizontal and vertical centering, right?我假设您的意思是水平居中和垂直居中,对吗?

As already pointed out in the comments, mixing tables with Bootstrap's row class causes issues with aligning items.正如评论中已经指出的那样,将表与 Bootstrap 的row class 混合会导致对齐项目的问题。

But, if you want to keep the current layout as it is, you could use display: contents on the element with class row .但是,如果您想保持当前布局不变,您可以在元素上使用display: contents和 class row This makes the children position relative to the parent of the element.这使得子元素 position 相对于元素的父元素。 Read more in the docs here .此处的文档中阅读更多信息。

Your modified snippet:您修改后的代码段:

 tr.row { display: contents }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="table table-dark"> <tbody id="table"> <tr class="row"> <td class="col-sm-2 align-middle">now aligned</td> <td class="col-sm-1"> <button> <div class="row"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Flag_of_Papua_New_Guinea.svg/320px-Flag_of_Papua_New_Guinea.svg.png" width="64px" height="48px"> </div> </button> </td> <td class="col-sm-9" colspan="9"></td> </tr> </tbody> </table> <table class="table table-dark"> <tbody> <tr> <td class="col-sm-2 align-middle">aligned</td> <td class="col-sm-1"> <button> <div class="row"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Flag_of_Papua_New_Guinea.svg/320px-Flag_of_Papua_New_Guinea.svg.png" width="64px" height="48px"> </div> </button> </td> <td class="col-sm-9" colspan="9"></td> </tr> </tbody> </table>

I don't let you wait for the correct answer but till that time you must look upon the following [1]: https://getbootstrap.com/docs/5.0/utilities/vertical-align/ which states the Vertical Alignment of item and item container in Bootstrap 5. You can use the same property to set the alignment for the table cell and item in that table cell.我不会让您等待正确的答案,但在那之前您必须查看以下内容 [1]: https://getbootstrap.com/docs/5.0/utilities/vertical-align/说明项目的垂直 Alignment和 Bootstrap 5 中的项目容器。您可以使用相同的属性为表格单元格和该表格单元格中的项目设置 alignment。

Cheers and Enjoy Development干杯,享受发展

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

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