Switch up background colors for different Div Labels

I have a unique structure resembling a table using div elements.

My goal is to alternate the background color of only the divLabels.

Each "table row" consists of a divLabel and a divData component.

Here is my attempt so far:

http://jsfiddle.net/o5dv4qkc/

However, I am facing an issue where the labels do not alternate in color as expected. Instead, they display the same color for odd rows only:

.About_RowLabel:nth-of-type(odd) {
    background-color: #DEF3CA;
}

If anyone could provide insight into why this does not work with my div elements, it would be greatly appreciated.

Answer №1

Ensure you are targeting the correct elements: move up a level to their parent elements and begin your selection from there, for example using

.someParentClass:nth-of-type(even) .child{}
: see demo

.About_Row {
  height: 2em;
  width: 500px;
}
.About_RowLabel {
  width: 98px;
  color: black;
  float: left;
  background-color: #71cd7b;
}
.About_RowData {
  width: 200px;
  float: left;
  margin-left: 20px;
}
.About_Row:nth-of-type(odd) .About_RowLabel {
  background-color: #DEF3CA;
}
.serv_resize {
  border: 1px solid #E2E2E2;
  color: #444;
  width: 425px;
  float: left;
  margin: 10px 10px 10px 0;
  padding: 5px;
  height: 239px;
}
<div id="you" class="serv_resize">
  <div class="About_Row">
    <div class="About_RowLabel">
      BirthDate:
    </div>
    <div class="About_RowData">
      <asp:Label ID="lblDoB" runat="server" Text="DoB"></asp:Label>
    </div>
  </div>
  <div class="About_Row">
    <div class="About_RowLabel">
      As of:
    </div>
    <div class="About_RowData">
      <asp:Label ID="lblAsOf" runat="server" Text="Dt"></asp:Label>
    </div>
  </div>
  <div class="About_Row">
    <div class="About_RowLabel">
      Phone:
    </div>
    <div class="About_RowData">
      <asp:Label ID="lblPhone" runat="server" Text="Ph"></asp:Label>
    </div>
  </div>
</div>

Answer №2

The pseudo selectors for nth- all function with sibling elements, meaning they target elements that have the same immediate parent.

Since all your elements with class .About_RowLabel are the first in their parent element (the .About_Row element), they are all considered odd.

Focus on styling alternating parent elements with the following code:

.About_Row:nth-of-type(odd) .About_RowLabel {
    background-color: #DEF3CA;
}

View the demonstration at http://jsfiddle.net/o5dv4qkc/4/

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

Is it possible to conceal JavaScript comments on a page before it is displayed?

Curiosity has led me to ponder a question that may seem trivial. Consider this scenario: in my HTML or ASPX page, I include a comment for some JavaScript code. When the page loads, will the comments be rendered along with the rest of the page's conten ...

Creating a dynamic sidebar menu with clickable submenus through jQuery and CSS for a user-friendly experience

<div id="sidebar" class="sidebar"> <div class="slimScrollDiv" style="position: relative; overflow: hidden; width: auto; height: 100%;"> <div data-scrollbar="true" data-height="100%" data-init="true" style="overflow: hidden; width: a ...

Use jQuery to achieve smooth scrolling when clicking on a link that points to a specific #div, maintaining the URL

I have a single page design with smooth scrolling implemented using jQuery with the following code: function ScrollMe(id){ var offset = $("#"+id).offset().top-50; $('html,body').animate({scrollTop: offset},'s ...

Crushing jQuery's Sortable/Droppable

Having a little issue here. I want to be able to toggle the sortable plugin's behavior by clicking a button - basically switching between sort mode and view mode. I've attempted to achieve this with the following code: function enterSortMode(){ ...

PHP consistently selects the final radio button element

Having an issue with submitting data from radio buttons to an ajax request for database transfer. -html <form class="siteInfo" action="ajax/site.php?nr=<?php echo $_GET['nr']; ?>" method="POST"> <input type="radio" name="transpor ...

Can this be achieved using CSS alone, without needing JavaScript?

In this scenario, there is a div with the class of some-class that can have either one or two child div elements. When there is only one child div, I want it to have a width of 100%. However, if there are two child div elements present, I want them to have ...

Exploring the file attributes within nw.js

I'm in the process of developing a native application using nw.js. I have included the following code snippet: <input id="fileDialog" type="file" accept=".pdf,.epub" multiple/><a id="add" href="#">Add</a> Below is my JavaScript cod ...

A workaround for background-size in Internet Explorer 7

Currently, I am working on a project at this link: Everything seems to be functioning well in Firefox and Chrome, however, I have encountered an issue with the background-size property not being supported in IE7. I heavily rely on this property throughout ...

Increase the div id using jQuery

I've got this code snippet here and, oh boy, am I a newbie. How can I increase the number in the div using a jQuery script? if($res >= 1){ $i=1; while($row = mysqli_fetch_array($qry)){ echo "<div clas ...

Updating URL links with PHP and DOM manipulation

I am attempting to transform all the links within an HTML variable into random ones using PHP. However, I am encountering an issue that is preventing the links from being properly changed. Below is the code snippet I am working with: <?php $jobTempla ...

The app is opening the index.html file instead of the expected index.jsx file

After creating a page with React.jsx, I encountered an issue where my page was initially opening index.jsx upon running npm start. However, when I deleted and then restored the index.html file to "public", the page started opening index.html instead. This ...

When using angular's ngHide directive on a button, it will still occupy space in the

At the bottom of this HTML file, there are 3 buttons displayed. The first image illustrates the layout of the 3 buttons. The second image demonstrates what happens when the middle button in the footer is commented out. The third image shows the situat ...

Can someone guide me on the process of assigning multiple classes in my situation?

Can someone assist me with combining multiple classes in an ng-class statement? I am attempting to utilize multiple classes for my element using ng-class. Here is what I have: <div id='test' ng-class="{blue: test, scale: scale, 'non-sca ...

:host-selector for Angular Material dialog

I am currently working with a dialog component provided by angular-material and I need to customize the appearance of the popup dialog. I am aware that there is some support for styling through the component generation: let dialogRef = dialog.open(MyDi ...

Styling errors with jQuery validation

How can I remove the borders in ul > li elements? Here is my current setup: errorLabelContainer: $("ul", $('div.error')), wrapper: 'li', errorContainer: $('div.error'), I have tried the following: div.message{ bac ...

I'm looking to showcase the list in a navigation bar by clicking on the hamburger menu. I want to include the 'home' and 'about' text specifically

Having trouble implementing the hamburger menu functionality in my HTML/CSS project. When clicked on a shrunken screen, I want the 'Home' and 'About' text in the nav to appear stacked on top of each other. JS is causing me some difficul ...

What could be causing my tabs to shift to the vertical side when using large, larger, and wide screens?

In my previous research, I came across a question similar to mine: Pure CSS accordion not working within CSS tabs. The solution mentioned works perfectly on monitors of any size, but the difference lies in the use of the a element. In their code, setting t ...

What is the best way to retrieve the class name of a div element that comes before with jquery?

I'm currently working on a new design and I need to retrieve the name of the class from a div element that is positioned above another div element. Here's an example: <div class="random name a"></div> <div class="the name of this ...

How to ensure a table in MaterialUI always maintains full width without requiring horizontal scrolling in CSS?

After attempting to allocate each widthPercent value on the TableHeader so they sum up to 100%, I am still experiencing the presence of a scroll bar. I have tried setting fixed widths for each column, but this results in excess space on the right side dep ...

Ways to stop a tag from causing disruption to the alignment of another

In a div with text-align: center; applied, I have two tags. My issue is that I want one of them to be centered on the same line, while the other is positioned to the right. However, both tags are currently sharing the centered space and extending equally t ...