Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use table row coloring for cells in Bootstrap

Twitter Bootstrap offers classes to color table rows like so:

<tr class="success"> 

I like the color choice and the class naming. Now what I would like to do, is to re-use these classes and apply them on table cells, too. Obviously, I could define my own classes with the same colors and be done with it.

But is there a shorthand in CSS. LESS, to let the td inherit the classes?

like image 775
Mahoni Avatar asked May 26 '13 21:05

Mahoni


People also ask

How do I color a table row in bootstrap?

Using pre-defined classes, we can change the color of table cells and table rows. In order to change the color of the table row, we need to specify in <tr> tag with the required class & for changing the color of the table row then specify it inside <td> tag with the required class. Let us learn classes one by one.

How can we set the background color for the table row and cell?

Explanation: The background color of the table is set by the bgcolor="color" attribute to the <table> tag. The background color of a table row is set by using the <tr> tag. The background color of a cell in a table is set by using either in <td> or <th> tag.

How do I fill a table cell with color?

Select the cells in which you want to apply a fill effect as the fill color. On the Tables tab, under Table Styles, click the arrow next to Fill. On the Fill menu, click Fill Effects. Click the Solid tab, and then click the color that you want.

How do you color a table row?

HTML | <tr> bgcolor Attribute The HTML <tr> bgcolor Attribute is used to specify the background color of a table row. It is not supported by HTML 5. Attribute Values: color_name: It sets the background color by using the color name.


1 Answers

You can override the default css rules with this:

.table tbody tr > td.success {   background-color: #dff0d8 !important; }  .table tbody tr > td.error {   background-color: #f2dede !important; }  .table tbody tr > td.warning {   background-color: #fcf8e3 !important; }  .table tbody tr > td.info {   background-color: #d9edf7 !important; }  .table-hover tbody tr:hover > td.success {   background-color: #d0e9c6 !important; }  .table-hover tbody tr:hover > td.error {   background-color: #ebcccc !important; }  .table-hover tbody tr:hover > td.warning {   background-color: #faf2cc !important; }  .table-hover tbody tr:hover > td.info {   background-color: #c4e3f3 !important; } 

!important is needed as bootstrap actually colours the cells individually (afaik it's not possible to just apply background-color to a tr). I couldn't find any colour variables in my version of bootstrap but that's the basic idea anyway.

like image 163
Matt Avatar answered Sep 29 '22 21:09

Matt