I am looking for a way to do alternating row colors in a responsive layout in Bootstrap 3. I cannot figure out how to do it without a LOT of extensive, confusing CSS and was hoping that someone had a better solution.
Here is the simple premise: 12 divs that display as 4 rows of 3 on large screens, 6 rows of 2 on small screens, and 12 rows of 1 on mobile. The rows will need to have alternating background colors regardless of screen size.
The HTML for Bootstrap 3 is as follows:
<div class="container"> <div class="row"> <div class="col-md-4 col-sm-6 col-xs-12">Emp-01</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-02</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-03</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-04</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-05</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-06</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-07</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-08</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-09</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-10</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-11</div> <div class="col-md-4 col-sm-6 col-xs-12">Emp-12</div> </div> </div>
Any thoughts/hints/help would be greatly appreciated.
Using pre-defined classes, we can change the color of table cells and table rows. In order to change the color of the table row, we need to specify in <tr> tag with the required class & for changing the color of the table row then specify it inside <td> tag with the required class.
Select the range of cells that you want to format. Click Home > Format as Table. Pick a table style that has alternate row shading. To change the shading from rows to columns, select the table, click Design, and then uncheck the Banded Rows box and check the Banded Columns box.
Since you are using bootstrap and you want alternating row colors for every screen sizes you need to write separate style rules for all the screen sizes.
/* For small screen */ .row :nth-child(even){ background-color: #dcdcdc; } .row :nth-child(odd){ background-color: #aaaaaa; } /* For medium screen */ @media (min-width: 768px) { .row :nth-child(4n), .row :nth-child(4n-1) { background: #dcdcdc; } .row :nth-child(4n-2), .row :nth-child(4n-3) { background: #aaaaaa; } } /* For large screen */ @media (min-width: 992px) { .row :nth-child(6n), .row :nth-child(6n-1), .row :nth-child(6n-2) { background: #dcdcdc; } .row :nth-child(6n-3), .row :nth-child(6n-4), .row :nth-child(6n-5) { background: #aaaaaa; } }
Working FIDDLE
I have also included the bootstrap CSS here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With