What is the best way to select individual containers within a larger container in order to modify their background color?


Hey everyone!

I am brand new to the world of HTML and CSS.

I am currently developing a website that features 5 unique cards:

  • The first card contains a single container with an image on the left and a paragraph on the right
  • The second card is split into two containers with a paragraph on the left and an image on the right
  • The third card is also split into two containers, this time with an image on the left and a paragraph on the right
  • The fourth card has a single container with an image on the left, a paragraph in the middle, and another image on the right
  • The fifth card features a single container with a centered paragraph

#home {
      &-a {
        .card {
          &-container {
            display: flex;
            background: $main-color;
            box-shadow: 5px 5px 10px 5px rgba(0, 0, 0, 0.3);
            width: auto;
            height: auto;
            margin: 5rem 1rem;
            padding: 0.5rem;

            .text-wrapper {
              width: 150%;
            }

            &:nth-child(4) {
              background: $secondary-color;
            }

            &-split {
              display: flex;
              margin: 5rem 1rem;
              height: auto;
            }

            &-text {
              background: $secondary-color;
              box-shadow: 5px 5px 10px 5px rgba(0, 0, 0, 0.3);
              margin-right: 2rem;
              width: 300%;
            }

            &-image {
              background: $secondary-color;
              box-shadow: 5px 5px 10px 5px rgba(0, 0, 0, 0.3);
              padding: 0.5rem;
            }
          }
        }

        .image-wrapper {
          background: $dark-color;
          padding: 3rem;
        }

        .text-wrapper {
          padding: 1rem;
          margin: 2rem;
        }
      }
    }

    .container {
      max-width: $website-width;
      margin: auto;
      padding: auto;
      overflow: hidden;
    }

    .reverse {
      flex-direction: row-reverse;
    }
<section id="home-a">
  <div class="container">
    <!-- 1st Card -->
    <div class="card-container my-2">
      <div class="image-wrapper">
        <img src="img/darts.jpg" alt="Darts" />
      </div>
      <div class="text-wrapper">
        <h2>Darts</h2>
        <h3>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Nam ab
          inventore ratione architecto velit labore quae consequatur minus
          rem incidunt.
        </h3>
      </div>
    </div>

    <!-- 2nd Card - Split -->
    <div class="card-container-split my-2">
      <div class="card-container-text">
        <div class="text-wrapper">
          <h2>Unsere Getränkekarte</h2>
          <h3>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Nam ab
            inventore ratione architecto velit labore quae consequatur minus
            rem incidunt.
          </h3>
        </div>
      </div>
      <div class="card-container-image">
        <div class="image-wrapper">
          <img src="img/drinks.jpg" alt="Getränke" />
        </div>
      </div>
    </div>

    <!-- 3rd Card - Split Reverse -->
    <div class="card-container-split my-2 reverse">
      <div class="card-container-text">
        <div class="text-wrapper">
          <h2>Reservierung</h2>
          <h3>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Nam ab
            inventore ratione architecto velit labore quae consequatur minus
            rem incidunt.
          </h3>
        </div>
      </div>
      <div class="card-container-image">
        <div class="image-wrapper">
          <img src="img/reserved.jpg" alt="Reservierung" />
        </div>
      </div>
    </div>

    <!-- 4th Card Reverse -->
    <div class="card-container my-2 reverse">
      <div class="image-wrapper">
        <img src="img/open.jpg" alt="Öffnungsschild" />
      </div>
      <div class="text-wrapper">
        <h2>Öffnungszeiten</h2>
        <h3>
          Montag: 11:00 - 23:30 Uhr
        </h3>
        <h3>
          Dienstag: 11:00 - 23:30 Uhr
        </h3>
        <h3>
          Mittwoch: 11:00 - 23:30 Uhr
        </h3>
        <h3>
          Donnerstag: 11:00 - 23:30 Uhr
        </h3>
        <h3>
          Freitag: 11:00 - 23:30 Uhr
        </h3>
        <h3>
          Samstag: 11:00 - 23:30 Uhr
        </h3>
        <h3>
          Sonntag: 11:00 - 23:30 Uhr
        </h3>
      </div>
    </div>

    <!-- 5th Card -->
    <div class="card-container my-2">
      <div class="text-wrapper">
        <h2>Covid 19</h2>
        <h3>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Nam ab
          inventore ratione architecto velit labore quae consequatur minus
          rem incidunt.
        </h3>
      </div>
    </div>
  </div>
</section>

Now I am looking to style the background of these cards with alternating $main-color and $secondary-colors. I successfully applied the background color change to the 4th card using nth-child(4), but I am struggling to target the two inner containers of the 3rd card to add margin and change the background color!

Any help would be greatly appreciated! Thank you!

Answer №1

https://www.w3schools.com/cssref/sel_element_element.asp

It would be beneficial to assign a unique ID to each card rather than relying solely on shared classes. Here is an example:

<div class="card-container my-2" id="FirstCard"></div>
<div class="card-container my-2" id="SecondCard"></div>

Continue this pattern for each card...

However, the following code should still function as intended:

/*3rd Slide Div 1*/
card-container-split:nth-child(2) card-container-text{
    /*your styling*/
}

/*3rd Slide Div 2*/
card-container-split:nth-child(2) card-container-image{
    /*your styling*/
}

If you opt to transition to using IDs, simply replace

card-container-split:nth-child(2)
with the ID for the third slide.

Answer №2

To achieve a different style, you have the option to utilize :nth-child(odd) and :nth-child(even). If you want to target the two inner containers within the third container, consider using the following code snippet:

.container > :nth-child(3) > * {margin: 2rem; background-color: blue}

This code will specifically select all immediate children of the third child element of .container.

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

Ways to change the border color of Bootstrap cards

While working on my webpage, I utilized bootstrap cards to maintain a clean and organized layout. By default, bootstrap cards are white in color. Unlike the navbar, where you can easily set color options like navbar-dark, changing the color of the cards re ...

Ensure that a div remains active even after it has been selected through AJAX using jQuery

I am currently utilizing ajax to display information from a database. The application I am working on is a chat app, where clicking on a specific conversation will append the data to a view. The structure of my conversation div is as follows: <div clas ...

A variety of menu items are featured, each one uniquely colored

In the user interface I developed, users have the ability to specify the number of floors in their building. Each menu item in the system corresponds to a floor. While this setup is functional, I am looking to give each menu item a unique background color ...

Ensure that the Div element is able to wrap lines in the same way that the Span element

Is there a method to ensure that a div element wraps the lines similar to how a span does? JSFiddle for testing In this example, I utilized the span tag to create an appearance as if the text was highlighted by a yellow highlighter pen. Everything functi ...

Calculating values within dynamically generated elements requires utilizing JavaScript to target and extract the

I am working on creating input fields inside an HTML table using Vue.js. On click of a button, I want to perform some calculations based on the input values. However, it seems that the calculations are not happening as desired. What I have attempted so fa ...

Positioning the close button on the top right edge of a Material-UI Dialog: A step-by-step guide

https://i.sstatic.net/ARTtq.png How can I include a close icon in the top right corner of the header section? I'm using the Material UI Dialog and everything works well, but I need a close button in the top section. Can anyone assist me with this? ...

Generate a new row for every value in a C# dropdown either before or after a Pipe character

I am facing a challenge with the custom attribute values for products in my database. To store these values, I save them as a character-separated list using the pipe symbol to separate each size. For instance, let's consider an Orange Dress with a cu ...

I'm having trouble adjusting the background color in the bootstrap template

------------ I am customizing the bootstrap SB Admin template for my asp.net core Identity project. However, I am facing an issue with changing the background color of the template and the navigation menu color. Even though I can see the color changes when ...

Design your website with CSS by incorporating hanging ::before labels that feature unique formatting

I am looking to create labels that designate specific groups of words. Here are the criteria: The label should appear to the left of the word group The words should be enclosed in lines, while the label should not The words should be indented, while the ...

Is it possible to replace multiple li elements with multiple ul elements within a single ul for better organization?

In the world of HTML and CSS, there are endless possibilities for creating unique designs. Recently, I stumbled upon an interesting question that has been lingering in my mind without a clear answer. Typically, people use a single "ul" element to create a ...

Having trouble showing Bootstrap Toasts on my website

I successfully implemented Toast functionality upon button click, but I am facing an issue where I have two buttons and I want a separate Toast to appear for each button click. How can I achieve this? Another problem I encountered is related to the docume ...

Flex bug in safari causing issues with inline form display

I have created a simple inline form using flexbox. However, I encountered an issue in Safari where the button loses its width. Here is a screenshot for reference: https://i.stack.imgur.com/3s5AR.jpg Interestingly, the form looks fine in all other browsers ...

What is the best way to make my input tags move downwards when resizing the screen?

I need help with a grid layout. My goal is to have the input tags (marked in blue) shift below when resizing the browser or viewing on a mobile or tablet device, while keeping the other three tags on the same line. I attempted to position the small box to ...

Issue with Table Content Being Truncated After Conversion from HTML to MS Word 2003

I am experiencing an issue where the table content is being cut off on the right side of the page when converting from an HTML page into MS Word 2003. Below is a sample HTML code (where placeholder $CLOB_DATA will be replaced by large CLOB data): <html ...

What could be causing my ASP.Net MVC script bundles to load on every page view?

I'm a bit puzzled. The _layout.cshtml page I have below contains several bundles of .css and .js files. Upon the initial site load, each file in the bundles is processed, which makes sense. However, every time a new view is loaded, each line of code f ...

Converting an NPM package into a gulp workflow

I'm currently generating html files from Nunjucks using my gulp file. Now, I need to convert these html files into text files. I came across a useful NPM package called html-to-text that can help with this task. However, I'm facing some challenge ...

Troubleshooting: Jquery show effects not functioning as expected

Currently, I am facing an issue where I am attempting to display a fixed div using the show function of jQuery. Although the show function itself is working properly, when I try to include an effect from jQuery UI, it does not seem to work as expected. Bot ...

Absence of MVC5 Vanilla Layouts

Creating my first MVC5/Razor application from the ground up, I want to avoid including unnecessary references that cause bloat. In the Views folder, I have separate Home and Shared subfolders. The Home folder contains Index.cshtml, while the Shared folder ...

Styled-components is not recognizing the prop `isActive` on a DOM element in React

In my code, I have an svg component that accepts props like so: import React from 'react'; export default (props) => ( <svg {...props}> <path d="M11.5 16.45l6.364-6.364" fillRule="evenodd" /> </svg> ) ...

creating an animated loop within an Angular template

How can we display a dynamic loop in Angular template using an array of object data? [ { "name": "name", "text": "text", "replies": [{ "name": "Reply1", "t ...