What is the best way to generate alternating row colors for dynamically loaded data sets?

I need to customize the background color of each location in a list that is dynamically loaded from our SQL Server DB and structured using Bootstrap Rows. How can I achieve alternating colors for each location?

Attempting to apply an "nth-child" style to the rows didn't work, as each row generated treats it as the first row regardless of how many are created. Even creating a table resulted in formatting and styling issues.

https://i.sstatic.net/gskTU.png

The code responsible for generating the list shown above is provided below:

 @foreach (var hospital in Model)
    {
        <button class="top50accordion">@Html.DisplayFor(modelItem => hospital.StatesAbre[hospital.State])</button>
        <div class="panel">
            @foreach (var subhospital in hospital.NameCityCenterId)
            {
                string[] hosp = @subhospital.Split('+');
                <div class="row">
                    <div class="col-8 col-sm-8 col-md-8 col-lg-6" style="margin: 5px 0;"><a href="~/ResearchCenter?id=@hosp[2]&alias=@hosp[0]" style="color:#0A5D66;font-weight:bold;" target="_blank">@hosp[0]</a></div>
                    <div class="text-right col-4 col-sm-4 col-md-4 col-lg-5" style="margin: 5px 0;">@hosp[1]</div>
                </div>
                <div class="col-lg-1"></div>

            }

        </div>
    }

Answer №1

Ensure that the nth child selector is correctly implemented using .panel .row as the selector.

.panel .row:nth-child(even){
 background-color: lightblue;
}
<div class="panel">
  <div class="row">a</div>
  <div class="row">b</div>
  <div class="row">c</div>
  <div class="row">d</div>
</div>
<hr>
<div class="panel">
  <div class="row">a</div>
  <div class="row">b</div>
  <div class="row">c</div>
  <div class="row">d</div>
</div>

Answer №2

Attempt to tackle this challenge...

.section .grid:nth-of-type(odd){
 background-color: #f1f1f1;
}

.section .grid:hover{
 background-color: #e0e0e0;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be the reason for my post container not functioning properly?

I am facing an issue with my HTML and CSS code. The container is not displaying as expected, specifically the green bar that should appear below the image. I suspect that the container's auto attribute is causing it to overlook the content inside. Can ...

What methods can be used to adjust the dimensions of a webpage's textbox?

I've been working on a Chrome extension and I have encountered a few challenges that I need help with. One issue I'm facing is changing the size of the text box in the Facebook chat box at the right bottom corner of the page. To accomplish this ...

Choosing an option in react-select causes the page to unexpectedly shift

Encountering a problem with a mobile modal developed using react-select. The selectors are within a div with fixed height and overflow-y: scroll. Upon selecting an option for the 'Choose observer' select, the entire modal briefly jumps down in th ...

The height of the sidebar must be set to 100% in order to fully cover the

Take a look at the examples below. // Content removed .aside { position: relative; float: left; width: 195px; top: 0px; bottom: 0px; background-color: #ebddca; height: 100%; } Currently, I'm still searching for the right ...

Mobile browsers consistently display a horizontal scroll bar

My website is displaying a horizontal scrollbar on Android devices in browsers like Chrome, Firefox, and Opera Mini. It does not show correctly on the Android Default browser, appearing broken. In contrast, when comparing my site to the full desktop versi ...

Using jQuery to access the ID of a div and create a custom close button

I am trying to implement a close button for my popup DIVs. Each one has a unique ID and I want to hide them by setting the CSS 'display' property to 'none' when closed. However, the following example is not functioning as expected. I a ...

In this guide, we will explore the process of designing unique styles for ng

What is the proper way to customize CSS for a specific element? &.ng-select-focused { &:not(.ng-select-opened) > .ng-select-container { border-color: $ng-select-highlight; box-shadow: $ng-select-box-shadow; } } The offi ...

Maintain the layout of HTML page while reducing the window size

My webpage is experiencing an issue. When I resize the window, the placement of all elements becomes misaligned and the layout gets distorted. ...

When using Material UI GridTile, the actionIcon will not be shown if there is no title present

In my React application, I am utilizing Material-UI GridList to showcase some images. I am interested in adding an IconButton on top of the image in my GridTile component. Currently, I can achieve this by providing the button to the actionIcon prop. Howeve ...

Guide on hiding the sidebar in mobile view and enabling toggling on click for both mobile and other devices with the use of vue.js and bootstrap4

I need assistance with transitioning my code from pure Bootstrap4 and JavaScript to Vue.js. When I tried implementing it in mobile view, the sidebar is not showing. Below is the code snippet that I am trying to change for Vue.js, but I keep encountering an ...

What is the best way to ensure an image is responsive in CSS and aligns perfectly in height with its neighboring text?

There is an image within the <img> tag and some text inside the <p> tag, both enclosed in a <div>. Although the image is responsive and resizes proportionally when the browser window changes size, there is an issue where as the text wrap ...

Prevent content overlap in Tumblr's sidebars by using CSS positioning techniques

I'm facing an issue with my blog on Tumblr where the sidebar keeps overlapping the main content. I have modified a premade template and am trying to position the sidebar in the top right corner using absolute or fixed positioning: #sidebar{ position: ...

The off canvas menu remains visible even while adjusting the window size

Can someone please help me? I am currently working on an off-canvas menu (you can find it here). The issue I am facing is that when the mobile menu is open and a user resizes the window, the content still retains its transform property and overlay. How can ...

Why is the column width on this Tumblr page constantly flickering?

Seeking assistance with a minor issue on my Tumblr page - the content column seems to be flickering on mouseover, shifting between full width and its original programmed width. Any insight on the CSS causing this? Much appreciated! Check it out here (Dis ...

Integrating the 'Select All' feature into a Bootstrap Multiselect form or list

I am working on a .NET web application that has multiple views for selecting from database tables. Using Bootstrap 4.3, I have successfully generated the list but now need to include a 'Select All' option to streamline the selection process. Be ...

Adding a three-dimensional perspective to an HTML5 canvas without utilizing CSS3 or WebGL

Here is a canvas I am working with: http://jsbin.com/soserubafe Here is the JavaScript code associated with it: var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var w = canvas.width; var h = canvas.height; var cw=canvas. ...

"Despite using 'vertical-align: middle' on table cells, the alignment to the middle is not achieved when using AlphaImageLoader in IE6

I am currently working on aligning text within table cells that have a PNG Transparent background. To achieve this in IE6, I am using the filter:progid:DXImageTransform.Microsoft.AlphaImageLoader() method. However, I am facing an issue where the text is no ...

Help with CSS and React - how to position text in front of an image

Currently, I am working on positioning text on top of an image and centering it within the parent div. You can see the desired design in the screenshot below: https://i.sstatic.net/NNidz.png After experimenting with code and using z-index to layer the te ...

Using Angular 8 to Toggle Switches with Data Binding

Creating a toggle switch similar to this image is my current task https://i.sstatic.net/8oTmi.png While attempting to bind the data to the switch using [checked], like shown below, <div class="mb-3"> <div class="d-flex mb-2"> ...

Display issue with flex slider thumbnail view

I am experiencing an issue with my flex slider. Everything works fine when I have 4 images, but as soon as there are more than 5 images, the thumbs view is displayed incorrectly. Does anyone know a solution to fix this problem? Here is a Screenshot for r ...