Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternating Row Colors in Bootstrap 3 - No Table

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.

like image 257
Trick Kramer Avatar asked Aug 04 '14 14:08

Trick Kramer


People also ask

How do I change the background color of a row in bootstrap?

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.

How do you alternate colors in a table?

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.


1 Answers

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.

like image 75
aniskhan001 Avatar answered Sep 17 '22 07:09

aniskhan001