Prioritizing nth child over the first child

I have a situation where I need to maintain 3 different styles in a patterned design without altering the classes on the HTML side. I have been attempting to achieve this using nth-child selectors. However, I am facing an issue where my first class is being overridden by my odd class. I even tried changing it to nth-child(2n+3), but that didn't work either. How can I ensure that my first child div retains its background image?

/* Styling for Even Featured Items */

.home-feature-container:nth-child(even)
{
    background-color:#393939;
    padding-bottom: 20px;    
}    

.home-feature-container:nth-child(even) .home-featured-left
{
    width:50%;
    display:inline-block;
    background-image: url('/images/featured-top-gray.png');
    background-repeat:no-repeat;
    background-position:top;
    background-size: 100%;
    vertical-align: top;
}    

.home-feature-container:nth-child(even) .home-featured-right
{
    width:50%;
    display:inline-block;
    background-image: url('/images/featured-top-spacer-gray.png');
    background-repeat:no-repeat;
    background-position:top;
    background-size: 100%;
    vertical-align: top;
}    

/* Styling for Odd Featured Items */

.home-feature-container:nth-child(2n+3)
{
    background-color:#252424;
    padding-bottom: 20px;    
}    

.home-feature-container:nth-child(2n+3) .home-featured-left
{
    width:50%;
    display:inline-block;
    background-image: url('/images/featured-top-black.png');
    background-repeat:no-repeat;
    background-position:top;
    background-size: 100%;
    vertical-align: top;
}    

.home-feature-container:nth-child(2n+3) .home-featured-right
{
    width:50%;
    display:inline-block;
    background-image: url('/images/featured-top-spacer-black.png');
    background-repeat:no-repeat;
    background-position:top;
    background-size: 100%;
    vertical-align: top;
}  

/* Styling for First Featured Item */

.home-feature-container:first-child 
{
    background-color:#252424;
    padding-bottom: 20px;    
}    

.home-feature-container:first-child  .home-featured-left
{
    width:50%;
    display:inline-block;
    background-image: url('/images/featured-top-first.png');
    background-repeat:no-repeat;
    background-position:top;
    background-size: 100%;
    vertical-align: top;
}    

.home-feature-container:first-child  .home-featured-right
{
    width:50%;
    display:inline-block;
    background-image: url('/images/featured-top-spacer-first.png');
    background-repeat:no-repeat;
    background-position:top;
    background-size: 100%;
    vertical-align: top;
}  

Now let's take a look at the HTML structure:


<div class="div_wrapper home-feature-container">
        <div class="home-featured-left">
            <div class="home-featured-left-content">
            <h3 class="title">Feature</h3>
            <h3>Sed tincidunt purus</h3>
            <div class="home-featured-copy">Eu lectus varius auctor. Integer et elit bibendum, fermentum velit a, aliquam est. Donec varius arcu rutrum lorem ultrices, et tristique leo pretium. Nam porttitor lacinia nunc, sit amet maximus justo placerat ac. Curabitur ut tellus sed nisi faucibus.
            </div>
            </div>
        </div><div class="home-featured-right">
            <div class="home-featured-right-content"><img src="/images/featured-image-home.jpg" class="home-featured-image" /></div>
        </div>
    </div>

Answer №1

If I grasp the question correctly, it seems that the selectors are not targeting anything in your HTML. You need to focus on selecting the children of the home-feature-container div.

To simplify things, I utilized the wildcard (*) selector. You can view a demonstration in this JSFiddle link

Your HTML:

<div class="div_wrapper home-feature-container">
  <div class="home-featured-left">
    <div class="home-featured-left-content">
      <h3 class="title">Feature</h3>
      <h3>Sed tincidunt purus</h3>
      <div class="home-featured-copy">Eu lectus varius auctor. Integer et elit bibendum, fermentum velit a, aliquam est. Donec varius arcu rutrum lorem ultrices, et tristique leo pretium. Nam porttitor lacinia nunc, sit amet maximus justo placerat ac. Curabitur ut tellus sed nisi faucibus.
      </div>
    </div>
  </div><div class="home-featured-right">
  <div class="home-featured-right-content"><img src="/images/featured-image-home.jpg" class="home-featured-image" /></div>
  </div>
</div>

Modified CSS:

/* Even Featured */

.home-feature-container *:nth-child(even)
{
    background-color:#393939;
    padding-bottom: 20px;    
}    

.home-feature-container *:nth-child(even) .home-featured-left
{
    width:50%;
    display:inline-block;
    background-image: url('/images/featured-top-gray.png');
    background-repeat:no-repeat;
    background-position:top;
    background-size: 100%;
    vertical-align: top;
}    

.home-feature-container *:nth-child(even) .home-featured-right
{
    width:50%;
    display:inline-block;
    background-image: url('/images/featured-top-spacer-gray.png');
    background-repeat:no-repeat;
    background-position:top;
    background-size: 100%;
    vertical-align: top;
}    

/* Odd Featured */

.home-feature-container *:nth-child(2n+3)
{
    background-color:#252424;
    padding-bottom: 20px;    
}

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

Tips for adding a solid background color to a div element

I am struggling with trying to make a full width background-color for a div in my ruby on rails app. Despite setting the background color and margin to 0, I still end up with unwanted margins on the left, right, and top. Below is the code snippet: <!D ...

Enhancing Canvas with a JPEG layer using the Caman library

I'm currently working with the Caman library to manipulate an image. My goal is to overlay a jpeg onto the manipulated image, but I'm facing a challenge. Although I can briefly see the jpeg beneath the manipulated image, it quickly gets overlai ...

Show only the portion of the image where the cursor is currently hovering

Information in advance: You can check out the current code or view the live demo (hover image functionality not included) at . The concept: I would like to have it so that when I hover my cursor (displayed as a black circle) over the image, only the s ...

Discovering the XPath for a Tag containing both span and div attributes

Can anyone assist me in finding the XPath for this specific element: <a class="channel_code" href="javascript:void(0)" oldtitle="<span style="text-decoration: underline; margin: 4px">CID:</span><div style="margin: 8px 4px 4px;">channe ...

Utilizing v-data-iterator in Vuetify to display data fetched from an API in a table

After a long day of searching and experimenting, I've finally arrived here. Initially, I attempted to create a simple table with 2 columns using Row and iterate over the object to populate the left column with its keys. However, I hit a roadblock when ...

Creating code to ensure a div element is displayed only after a specific duration has elapsed

Can anyone help me with coding to make a div appear on a website after a specific amount of time? Here's an example of what I'm trying to achieve: <div class="container"> <div class="secretpopout"> This is the hidden div that should ...

Discover how to apply unique styles to specific HTML elements using their index values in jQuery

In the process of creating a party game similar to Tambola or Housie, I am faced with the challenge of automatically detecting and highlighting the numbers called out on each ticket in the game. For instance, if number 86 is announced, I aim to visually di ...

A simple guide on how to surround every incorrect input index in mapped inputs with red borders

I am incorporating a modal that corresponds each element of the object newCompanies to a specific row: {newCompanies.map((company, index) => { return ( <div> <div className="side- ...

Tips for marking a textarea as mandatory when a choice is made

I'm creating an .html document for conducting a complete system check and confirming various aspects of its functionality. In this page, there is a table within a form structure where each row represents a step in the system verification process (e.g. ...

Switching Column Content with jQuery on Mobile Devices

In my design, I have a dynamic two-column layout created using Twitter Bootstrap 3. The layout switches between image-text and text-image alignment for each row. The goal is to adjust it for smaller screens (less than 768px) so that rows with images on th ...

AngularJS - How to loop through a div to display all items

I am working with AngularJS to create a repeatable shopping cart items feature. Below is a sample JSON data structure: [{"src": "img/T1.jpg", "itemname": "solid green cotton tshirt", "style": "MS13KT1906", "colour": "Blue", "size": "s", "qty": "1", "price ...

Tired of searching for a way to activate the Inspect function in the right-click menu on a custom dialog while debugging Google apps

I'm currently working on implementing a file picker function within Google Sheets by using the Google Picker API. Although I have managed to display a customized dialog window for selecting files from my Google Drive, the output is not exactly what I ...

Splitting a td tag into multiple columns dynamically with Angular

I am attempting to dynamically split the table element into separate columns. My desired layout should resemble this: https://i.sstatic.net/C81tg.png The values for name and surname are fixed at 1, but the values for subjects and grades can vary (there ma ...

Warning on React component listing due to key issue originating from the parent component

Alert: It is essential that each child in a list has a distinct "key" prop. Please review the render method of ForwardRef(ListItem). A child from RecipientsList was passed. I have located this issue using the react-components debugger tool on Chrome. I ad ...

Using HTML div tags to create a looping expression that can interact with Javascript

Currently, I am working on a Ruby on Rails project and I am facing a challenge with looping variables in an HTML form to reduce the amount of manual code I have to write. However, when I attempt to evaluate an expression in the id attribute, the JavaScript ...

What is the best way to have dual backgrounds displayed on a single webpage?

Looking to recreate the background design featured on this website: www.zucoffee.com. Does anyone have any tips? In addition, I'm curious about how to achieve that curved separation effect. I understand it involves two divs, but how did they manage t ...

Simply drag your files onto the form for quick and easy uploading upon submission

Would like to know how to enable drag-and-drop file selection for uploading files synchronously upon form submission. Understanding the basics of file uploading using HTML and PHP, I am seeking a way to process these files in PHP along with those in the $_ ...

Elements with fractional heights containing absolutely-positioned child elements

I've been experimenting with styling elements using absolutely positioned :before pseudo-elements. My current approach looks like this: .element:before { content: ""; display: block; height: 0; width: 0; border: 10px solid #ad ...

What impact does the use of CSS in emails have on various email clients and reading formats?

As I delve into customizing my very first (woocommerce) email template, I am considering the option of concealing data using CSS display: none. However, a few questions arise regarding the compatibility and support of CSS and display: none: Do all email ...

What is causing the body to be partially hidden on the right side of every mobile device screen?

I'm currently addressing the mobile optimization issues on a website that I've been developing for some time, and I've encountered my first obstacle. When viewing it on an iPad, the body of the site doesn't extend all the way to the rig ...