Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inline CSS for background-color not showing

I have a bootstrap table-striped table. In this case, odd rows typically have gray background. Bootstrap CSS version is v5.3.2.

But if I add style="background-color: red" directly to the <td> elements, cells on odd rows still show a gray background instead of a red one. Why would the background still appear as gray instead of red? I would have thought inline styles have the highest specificity.

For context, the reason for I wanted to assign inline style is to highlight specific cells that has important information, but the result is unexpected, hence this question and the simple example given.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s the same on both Chrome and Edge browsers.

Below is a very simple example, showing that the cells in odd rows are still gray instead of red, despite having inline style setting the background to red.
Cells on odd row have gray background instead of red despite having inline style assigned a red background
Short sample showing the problem
The Chrome DevTools shows that the cell should have a red background but it is displayed with a gray one

>Solution :

I believe that the .table-striped class applies background color on the <tr> element, and not the <td> element.
I tried to replicate your issue, but was in vain. But I did see that when I tried to set the same for <tr> it did not override the color.

I tried changing some things in the devtools, and found that setting --bs-table-bg to none like this:

<table class="table table-striped " style="--bs-table-bg:none;">

i was able to fix it. This might cause other issues, so make sure to check if other row backgrounds are unaffected in your case.

Check your markup for issues as well since I wasn’t able to get your exact issue replicated.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading