Tr Bgcolor Is Obsolete: Here's How To Set Table Row Background ...
Có thể bạn quan tâm
Code Example
<table> <tr bgcolor="#ddd"> <td>First column</td> <td>Second column</td> <td>Third column</td> </tr> <tr bgcolor="#eee"> <td>First column</td> <td>Second column</td> <td>Third column</td> </tr> <tr bgcolor="#ddd"> <td>First column</td> <td>Second column</td> <td>Third column</td> </tr> </table>| First column | Second column | Third column |
| First column | Second column | Third column |
| First column | Second column | Third column |
Setting the Background Color of Table Rows
The bgcolor attribute is now deprecated, but it was once the correct way to control the background color of table rows. Color names (such as “blue”), hex numbers, and rgb color codes could all be used with the bgcolor attribute. While browser support for this attribute is still pretty good, you really shouldn’t use it anymore. Instead, use the CSS background-color property control the color of table rows. Using CSS instead of HTML attributes has the added benefit of being applied to multiple elements simultaneously if you wish. For example, if we wanted to create a table where the color of each row alternated between hex colors #ddd and #eee, we can do that with just two CSS rules. However, if we used HTML attributes, we would have to add the attribute to every single table row.
<style> .example-table tr:nth-child(2n+1) { background-color: #ddd; } .example-table tr:nth-child(2n+0) { background-color: #eee; } </style> <table class="example-table"> <tr><td>First column</td><td>Second column</td><td>Third column</td></tr> <tr><td>First column</td><td>Second column</td><td>Third column</td></tr> <tr><td>First column</td><td>Second column</td><td>Third column</td></tr> <tr><td>First column</td><td>Second column</td><td>Third column</td></tr> <tr><td>First column</td><td>Second column</td><td>Third column</td></tr> <tr><td>First column</td><td>Second column</td><td>Third column</td></tr> </table>What that bit of code does is use the nth-child selector apply the background color #ddd to every odd numbered <tr> and the #eee background color to every even numbered row.
.example-table tr:nth-child(2n+1){background-color: #ddd;}.example-table tr:nth-child(2n+0){background-color: #eee;}| First column | Second column | Third column |
| First column | Second column | Third column |
| First column | Second column | Third column |
| First column | Second column | Third column |
| First column | Second column | Third column |
| First column | Second column | Third column |
Post navigation
What Does Td Colspan Have To Do With Tables In HTML?AppletsSearch HTML.com
Search for:Most Popular
- <var> HTML Tag300 views
- What Is Doxing? (And Why Is It So Scary?): An Infographic83 views
- New Audio HTML Element: Master It Out Now With Our Code Example61 views
- The Web Worker's Stress Busting Toolbox: 50 Tips to Improve Your Life41 views
- <meter> HTML Tag40 views
Từ khóa » Html Col Background Color
-
HTML Col Tag - W3Schools
-
HTML Colgroup Tag - W3Schools
-
Can I Color Table Columns Using CSS Without Coloring Individual Cells?
-
: The Table Column Element - HTML - MDN Web Docs - Mozilla -
Table Colors & Background : MGA - Web Development Tutorials
-
How To Change The Background Color Of An HTML Table - ThoughtCo
-
How To Set Background Color In The First Column Of A Table (HTML ...
-
HTML:
Tag - TechOnTheNet -
Set Background For Column Group In HTML And CSS
-
HTML Table Background Color
-
Setting Table Colors - Documentation For Action Request System 21.3
-
Colors - Bootstrap
-
How To Change The Background Colour Of A Table-column In Html And ...
-
HTML Table Basics