简体   繁体   English

使用Bootstrap表格式化时如何覆盖特定的表边框CSS样式

[英]How to override a specific table border CSS style while using Bootstrap table formatting

I'm using Bootstrap with tables, and trying to make some minor overrides to the default CSS with limited success. 我正在将Bootstrap与表一起使用,并尝试对默认CSS进行一些小的覆盖,但效果有限。

In the table below, I'm able to add a dark border at the bottom of the table head (thead), and to the bottom of the table rows in the footer (tr in tfoot), but I cannot add a border to the bottom of the last table row (tr:last-child), or alternately the bottom of the table body (tbody), or I suppose the top of the table footer (tfoot). 在下表中,我可以在表头底部(thead)以及页脚中表行的底部(tr in tfoot)添加深色边框,但是我不能在边框上添加边框最后一个表格行的底部(tr:last-child),或者表格主体的底部(tbody),或者我假设表格页脚的顶部(tfoot)。

I've had limited success with this: 我在此方面取得的成功有限:

.table-sm.event-table tbody > tr:last-child {
    border-bottom: 2px solid #999;
}

However this doesn't render in all browsers, and only 'works' by making the single pixel light grey line a 2 pixel dark line, which I don't want, I just want a single pixel dark border between the last row of the body and the first row of the footer (between Row Two and Total Expense). 但是,这并不能在所有浏览器中呈现,只能通过将单个像素的浅灰色线设置为2像素的暗线来“起作用”,我不希望这样做,我只希望在最后一行之间有一个像素的暗边框。正文和页脚的第一行(在第二行和总费用之间)。

I know this has to do with the specificity of the CSS rules, and Bootstrap's rule taking precedent over my own, but even though I was able to make the other rules work, I cannot for the life of me figure out how to specify this one. 我知道这与CSS规则的特殊性有关,Bootstrap的规则优先于我自己的规则,但是即使我能够使其他规则发挥作用,我也无法一生都想出如何指定该规则。

 .event-table { width: 100%; } .table thead > tr > th { border-bottom: 1px solid #333; } .table tfoot > tr > td { border-bottom: 1px solid #333; } 
  <table class="table table-bordered table-sm event-table"> <thead> <tr> <th>Unit</th> <th>Total</th> </tr> </thead> <tfoot> <tr> <td>Total Expense $</td> <td class="text-right">$200</td> </tr> <tr> <td>Total Revenue $</td> <td class="text-right">$300</td> </tr> </tfoot> <tbody> <tr> <td>Row One</td> <td>$100</td> </tr> <tr> <td>Row Two</td> <td>$100</td> </tr> </tbody> </table> 

Specificity is the name of the game and if you deal with Bootstrap, you'll quickly learn that it get's very complicated and even nigh impossible. 特殊性是游戏的名称,如果您使用Bootstrap,您将很快了解到它变得非常复杂,甚至几乎是不可能的。 While using #ids and !important may be an immediate remedy to your situation, it will bite you in the @rse if they are used even if only moderately. 虽然使用#ids!important可能会立即解决您的情况,但即使使用得当,也会在@rse中咬住您。 Try using only a few #id if you must and avoid !important at all costs. 如果有必要,请尝试仅使用几个#id ,并不惜一切代价避免!important

A safer solution is to double up on a class: 一个更安全的解决方案是对一堂课加倍:

As a nonsense special case for (2), duplicate simple selectors to increase specificity when you have nothing more to specify. 作为(2)的废话特殊情况,当您没有其他要指定的内容时,请复制简单的选择器以增加特异性。

MDN - The !important exception MDN-重要的异常

The following demo has each table section (ie <thead> , <tbody> , and <tfoot> ) with it's last row border-bottom a different color. 以下演示具有每个表部分(即<thead><tbody><tfoot> ),其最后一行的border-bottom不同。 Note that the bootstrap.css file is loaded as well, so it does work to the best of my knowledge and evidence at hand. 请注意,bootstrap.css文件也已加载,因此,据我所知和手头所知,它确实可以正常工作。

Demo 演示

 .event-table { width: 100%; } .table thead>tr.rowA1.rowA1>th { border-bottom: 1px solid red; } .table tbody>tr.rowB2.rowB2>td { border-bottom: 1px solid lime; } .table tfoot>tr.rowC2.rowC2>td { border-bottom: 1px solid blue; } 
 <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'> <table class="table table-bordered table-sm event-table"> <thead> <tr class='rowA1'> <th>Unit</th> <th>Total</th> </tr> </thead> <tbody> <tr class='rowB1'> <td>Row One</td> <td>$100</td> </tr> <tr class='rowB2'> <td>Row Two</td> <td>$100</td> </tr> </tbody> <tfoot> <tr class='rowC1'> <td>Total Expense $</td> <td>$200</td> </tr> <tr class='rowC2'> <td>Total Revenue $</td> <td>$300</td> </tr> </tfoot> </table> 

Give your tags an id instead of a class. 给您的标签一个ID而不是一个类。 This way, when you go to style the certain element with the id in your css, it will be at a higher priority than the Bootstrap style, which would erase the need for !important in most cases 这样,当您在CSS中使用ID设置特定元素的样式时,它的优先级将高于Bootstrap样式,这将消除大多数情况下对!important的需要。

So say you add an id to your table tag in the html like so 所以说你像这样在html中的表格标签中添加一个id

<table class="table table-bordered table-sm event-table" id="main-table">

You should be able to do this with success 您应该能够成功做到这一点

#main-table {   
  width: 100%; 
} 

#main-table thead > tr > th, #main-table tfoot > tr > td {  
  border-bottom: 1px solid #333; 
}

有时您必须使用!important来覆盖Bootstrap样式,如下所示

border-bottom: 2px solid #999 !important;

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

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