Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom css being overridden by bootstrap css

Tags:

I am using Bootstrap 3 and I have a table showing some data. in this table I have applied some javascript for conditional formatting, in the event that a condition is met, I set the element's class to "red"

.red { background-color:red; color:white; } 

the elements HTML is as follows:

<td class="red">0</td> 

I now have a conflict on odd rows the text color applies but the background color is overridden by the following css from bootstrap.

.table-striped > tbody > tr:nth-child(odd) > td, .table-striped > tbody > tr:nth-child(odd) > th {   background-color: #f9f9f9; } 

how can I resolve this conflict and assure that the red class takes presedence?

like image 829
mmilan Avatar asked Nov 04 '13 13:11

mmilan


People also ask

Does custom CSS override bootstrap?

You can override the default styles of Bootstrap elements using two possible methods. The first way — using CSS overrides— applies to sites using BootstrapCDN or the pre-compiled versions of Bootstrap. The second — using Sass variables — applies to sites using the source code version of Bootstrap.

Can I use custom CSS with bootstrap?

Use Bootstrap's CSS custom properties for fast and forward-looking design and development. Bootstrap includes around two dozen CSS custom properties (variables) in its compiled CSS, with dozens more on the way for improved customization on a per-component basis.

Why my CSS is not working in bootstrap?

These are the possible reasons: You have a wrong or incorrect link to the bootstrap file. browser caching and history is messing with your html. Other CSS files are overriding the bootstrap file.


2 Answers

Specificity

Your issue is most likely regarding specificity. Chris Coyier has a great article on CSS specificity. I would also suggest you check out this handy specificity calculator.

Using that calculator, we can see that .table-striped > tbody > tr:nth-child(odd) > td has a specificity of 23. As such, to override that, any new rule needs to have a specificity of something equal to or greater than 23. .red is at 10, so that isn't going to cut it.

In this case, it should be as simple as matching the existing specificity, and then adding your class to it. .table-striped > tbody > tr:nth-child(odd) > td.red gives us a specificity of 33. As 33 is greater than 23, your rule should now work.

See a working example here: http://bootply.com/91756

!important

In general, you should never use !important unless you never want that rule to be overridden. !important is basically the nuclear option. I am moderately confident in saying that if you understand specificity, you should never need to !important to make a custom rule work properly in a framework like Bootstrap.

Update

After a bit of thought, the rule I provide here is probably a bit too specific. What happens if you want to higlight a cell on a table that isn't stripped? To make your rule a bit more global while still having enough specificity to work in stripped tables, I would go with .table > tbody > tr > td.red. This has the same specificity as the Bootstrap stripping, but will also work on tables that are not zebra stripped. Updated example is here: http://bootply.com/91760

like image 71
Sean Ryan Avatar answered Dec 12 '22 08:12

Sean Ryan


Firstly, read Sean Ryan's answer - it is very good and informative, but I had to tweak his answer enough before implementing in my code that I wanted have a distinct answer that might help the next person.

I had almost the same question as the OP but also needed to be able to highlight the row, too. Below is how I enhanced(?) Sean Ryan's answer and turned it into my final implementation, which allows you to add a class to most any random element, including to a "tr" or a "td"

See this on bootply.com

CSS

    /* enable 'highlight' to be applied to most anything, including <tr> & <td>, including bootstrap's aggressive 'table-stripe' see: http://stackoverflow.com/questions/19768794/custom-css-being-overridden-by-bootstrap-css  FYI: In the stack overflow question, the class is 'red' instead of 'highlight' FYI: This is forked from http://www.bootply.com/91756  I used three different colors here for illustration, but we'd likely want them to all be the same color. */  .highlight {     background-color: lightyellow; }   /* supersede bootstrap at the row level by being annoyingly specific  */ .table-striped > tbody > tr:nth-child(odd).highlight > td {     background-color: pink; }  /* supersede bootstrap at the cell level by being annoyingly specific */ .table-striped > tbody > tr:nth-child(odd) > td.highlight {     background-color:lightgreen; } 

HTML

<table class="table table-striped">     <thead>         <tr>             <th>Col 1</th>             <th>Col 2</th>             <th>Col 3</th>         </tr>     </thead>     <tbody>         <tr>             <td>1hi</td>             <td>hi</td>             <td>hi</td>         </tr>         <tr class="highlight">             <td>2hi</td>             <td>This row highlights fine since it was an even row to begin with, and therefore not highlighted by bootstrap</td>             <td>hi</td>         </tr>         <tr class="highlight">             <td>3hi</td>           <td>This whole row should be highlighted.</td>             <td>hi</td>         </tr>         <tr>             <td>4hi</td>             <td>hi</td>             <td>hi</td>         </tr><tr>             <td>5hi</td>             <td class="highlight">Just a specific cell to be highlighted</td>             <td>hi</td>         </tr>     </tbody> </table> 
like image 32
JJ Rohrer Avatar answered Dec 12 '22 08:12

JJ Rohrer