Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Targeting specific class inside table with ID using CSS - Changing ROW background

I have a table that looks like this:

<table class="table table-striped table-hover table-responsive" id="commenttable">
    <thead>
        <th>Status</th>
        <th>Subject</th>
        <th>Message</th>
        <th>Tech</th>
        <th>Emailed?</th>
        <th>Date/Time</th>
    </thead>
    <tbody>
        <?php
            foreach($commresult as $row)
            {
                if($row['commentPrivate'] == 'yes'){
                    echo "<tr class='private'>";
                }
                else{
                    echo "<tr>";
                }
                echo "<td>" . ucwords($row['commentType']) . "</td>";
                echo "<td>" . ucwords($row['commentSubject']) . "</a></td>";
                echo "<td>" . $row['commentMessage'] . "</td>";
                echo "<td>" .  ucwords($row['commentBy']) . "</td>";
                echo "<td>" .  ucwords($row['commentEmailComm']) . "</td>";
                echo "<td>" . $row['commentDate'] . "</td>";
                echo "</tr>";
            }
        ?>
    </tbody>

What I'm trying to do is if $row['commentPrivate'] == 'yes' I want to change the background color of just that row to red.

I've tried just using <tr bgcolor="#ff7f7f"> which didn't work. So now I'm just trying to add the class 'private' to the row and target it using CSS, which I think I'm failing at miserably.

Here's the css:

.private {
background-color: #ff7f7f;
}

I've also tried:

#commenttable tbody .private {
background-color: #ff7f7f;
}

Any help is appreciated.

like image 553
D Mort Avatar asked Apr 29 '26 18:04

D Mort


2 Answers

.table-striped class adds zebra-stripes to a table. So to override the same, you can put !important in your CSS like this:

.private {
  background-color: #ff7f7f !important;
 }

Here is a Demo

.table .table-striped is more specific than .private. Hence the rules for .table-striped prevails.

enter image description here

So, you can use !important to override the same. The !important value appended a CSS property value is an automatic win. It overrides even inline styles from the markup. The only way an !important value can be overridden is with another !important rule declared later in the CSS and with equal or great specificity value otherwise.

like image 163
Senjuti Mahapatra Avatar answered May 02 '26 07:05

Senjuti Mahapatra


#commenttable .private {
  background-color: #ff7f7f !important;
}
like image 30
Mamunur Rashid Avatar answered May 02 '26 09:05

Mamunur Rashid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!