htmlcsstwitter-bootstrapbootstrap-5

In Bootstrap 5.3, is it possible to change the background color to white for the last two rows of a striped table?


I'm using Bootstrap to display a striped table with the table-striped class. However, I want the last two rows to have a white background despite the row number being odd or even.

I tried these approaches, but they didn't work:

  1. Add bg-white class to the two rows
  2. Create a new class in a separate CSS stylesheet such as:
.white-row tr {
    background color: white;
}
  1. Use an id selector:
#white-row {
    background color: white;  
}
  1. Add !important inside the new class

Here is my code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, width=device-width">

        <!-- http://getbootstrap.com/docs/5.3/ -->
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

        <title> Example code </title>
    </head>

    <body>
        <main class="container py-5 text-center">
            <div class="container">
                <div class="row justify-content-center">
                    <table class="table table-striped w-75 p-3 justify-content-center">
                        <thead>
                            <tr>
                                <th class="text-start">Symbol</th>
                                <th class="text-end">Shares</th>
                                <th class="text-end">Price</th>
                                <th class="text-end">TOTAL</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td class="text-start"> NFLX </td>
                                <td class="text-end"> 1 </td>
                                <td class="text-end"> $486.88 </td>
                                <td class="text-end"> $486.88 </td>
                            </tr>
                            <tr>
                                <td  class="border-0"></td>
                                <td  class="border-0"></td>
                                <th  class="border-0 text-end"> Cash </th>
                                <td  class="border-0 text-end"> $9,513.12 </td>
                            </tr>
                            <tr>
                                <td class="border-0"></td>
                                <td class="border-0"></td>
                                <th class="border-0 text-end"> TOTAL </th>
                                <td class="border-0 text-end"> $10,000.00 </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </main>
    </body>
</html>

This is the desired look for the table: desired look


Solution

  • The reason why it is not working is, that Bootstrap does not use background-color to create the effect but a box-shadow.
    You simply have to remove the box-shadow or make it transparent:

    tbody tr:nth-last-child(-n+2) {
      td, th {
      box-shadow: none;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, width=device-width">
      <!-- http://getbootstrap.com/docs/5.3/ -->
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
      <title> Example code </title>
    </head>
    
    <body>
      <main class="container py-5 text-center">
        <div class="container">
          <div class="row justify-content-center">
            <table class="table table-striped w-75 p-3 justify-content-center">
              <thead>
                <tr>
                  <th class="text-start">Symbol</th>
                  <th class="text-end">Shares</th>
                  <th class="text-end">Price</th>
                  <th class="text-end">TOTAL</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td class="text-start"> NFLX </td>
                  <td class="text-end"> 1 </td>
                  <td class="text-end"> $486.88 </td>
                  <td class="text-end"> $486.88 </td>
                </tr>
                <tr>
                  <td class="border-0"></td>
                  <td class="border-0"></td>
                  <th class="border-0 text-end"> Cash </th>
                  <td class="border-0 text-end"> $9,513.12 </td>
                </tr>
                <tr>
                  <td class="border-0"></td>
                  <td class="border-0"></td>
                  <th class="border-0 text-end"> TOTAL </th>
                  <td class="border-0 text-end"> $10,000.00 </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </main>
    </body>
    
    </html>