Troubleshooting a CSS problem: Changing the background of a parent div when the checkbox is selected

After spending hours trying to figure this out, I'm hopeful that someone can provide some insight into my issue.

The task at hand is to define a CSS selector that changes the background-color of a DIV (.first) only when a specific checkbox (#test1) is checked.

Here's an example of what I'm working on: http://jsfiddle.net/Zg6Vf/1/

HTML:

<div class="container">


        <div class="first">
        <input id="test1" name="accordion" type="checkbox" />
        <label for="test1">
    <img src="http://designmodo.github.io/Flat-UI/images/icons/svg/toilet-paper.svg" class="hide1"><img src="http://designmodo.github.io/Flat-UI/images/icons/svg/mail.svg" class="hide2"></label>
        <article class="content">
            <p>Spring onion parsley squash lotus root gumbo swiss chard spinach amaranth scallion collard greens coriander brussels sprout caulie leek ricebean. Dandelion lotus root plantain mung bean spring onion radish soko fennel cucumber komatsuna. Shallot bell pepper broccoli welsh onion kohlrabi kale celery corn fava bean epazote.</p>
        </article>
        </div>


                <div class="first">
        <input id="test2" name="accordion" type="checkbox" />
        <label for="test2">
    <img src="http://designmodo.github.io/Flat-UI/images/icons/svg/retina.svg" class="hide1"><img src="http://designmodo.github.io/Flat-UI/images/icons/svg/clocks.svg" class="hide2"></label>
        <article class="content">
            <p>Spring onion parsley squash lotus root gumbo swiss chard spinach amaranth scallion collard greens coriander brussels sprout caulie leek ricebean. Dandelion lotus root plantain mung bean spring onion radish soko fennel cucumber komatsuna. Shallot bell pepper broccoli welsh onion kohlrabi kale celery corn fava bean epazote.</p>
        </article>
        </div>


            </div>

CSS:

.container{
    width: auto;
    height: auto;
    margin: 0 auto;
    font-family:Georgia;
    font-size: 1em;
    line-height: 1.5em;
    display: block;
    color: black;
}
.container label{
    position: relative;
    display: block;
    cursor: pointer;
    margin: 10px;
    float: none;
    width: auto;
}
.container input{
    display: none;
}
.container article{
    overflow: hidden;
    height: 0px;
    position: relative;
}
.container input:checked ~ article.content{
    height: auto; 
}
input[type='checkbox']:checked + label > .hide1 {
    display: none !important;
}
input[type='checkbox']:checked + label > .hide2 {
    display: block !important;
}
.hide2 {
    display: none !important;
}

Essentially, clicking on the Yellow Toiletpaper should change the background of the .first div to yellow, but only when the #test1 checkbox is checked. How can this be achieved with CSS?

I'm looking forward to any assistance :)

Answer №1

When it comes to CSS, there is no direct parent selector available. However, you can achieve a similar effect by using the sibling selector to apply a yellow background to article.content.

.container input#test1:checked ~ article.content{
    background-color: yellow;
}

Here's a link to the updated code on JSFiddle.

If you're okay with having a white gap between an image and text, you can also make use of the following:

.container input#test1:checked + label {
    background-color: yellow;
}

You can view how this works on JSFiddle.

Update:

If jQuery is an option for you, consider implementing the following:

$('#test1').change(function() {
    $('#test1:checked').parent().css('background-color', 'yellow');
});

For a demonstration, refer to this JSFiddle.

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

Unable to display tooltip on added <li> items

I implemented a live search feature that retrieves a complete list element and appends it to a container. However, I am facing an issue with the tooltip not working. Below is the code snippet: $(".hb_search_text_job").on('keyup', function() { ...

My coding skills leave much to be desired and I am encountering some challenges with my current Hangman Game project

I recently started learning JavaScript and I'm encountering some difficulties with my code. My assignment is to create a hangman game, and I've successfully implemented the loops for displaying "right" and "wrong" letters. However, I'm strug ...

Alert: mssql_execute() encountered an issue and was unable to locate the stored procedure

Despite spending nearly a day trying to solve this issue, I am still unable to find a solution. I am encountering a problem with an MSSQL procedure in PHP. I have a form with 5 fields that calculate an "Amount." The procedure works perfectly in SQL Managem ...

What could be the reason for the container div's height not being properly refreshed?

When adding elements to a container div with an initial height of 'auto', I assumed that the height would adjust based on the children elements added. However, this is not happening. Can anyone assist me in ensuring that the container div's ...

Is it possible to duplicate content from one div to another?

With over 90 divs, each containing an image or icon, I am looking for a way to display the clicked image in another div. I am not very familiar with JS yet, so I would appreciate a simple example using HTML5, Ajax, and js. EDIT: Just to clarify, the desti ...

Troubleshooting AJAX hover functionality issue

Here is the content that loads through AJAX: <div class="grid"> <div class="thumb"> <img alt="AlbumIcon" src="some-image.jpg"> <div style="bottom:-75px;" class="meta"> <p class="title">Title< ...

Masking of the Navigation Button

I designed a responsive navigation bar, but in mobile view, the menu icon is being hidden by the menu headings when displayed. Check out the scenario below with a provided CodePen link. I attempted to adjust padding and float properties without success. An ...

Design for a mobile webpage layout featuring a carousel-style format

Recently, I've been experimenting with creating a mobile application using PhoneGap and I have some questions about defining the layout. I am trying to create a single-page layout with side scrolling, similar to a carousel image gallery, but instead ...

With Jquery, my goal is to position a div outside of another div element

I need to place a div tag in a specific way, like this: <div class="abc"> <div class="mySlides fade"> <img src="img_nature_wide.jpg" style="width:100%"> </div> <div> Using jQuer ...

Restrict Scrolling on Large Screens with Bootstrap, Allow Scrolling on Medium or Smaller Screens

I have a website that utilizes the Bootstrap 4 framework (v4.4.1). I implemented CSS properties height: 100% in HTML and body along with overflow: hidden to restrict scrolling and keep the content within the browser window only (I don't have much cont ...

Steps for activating mandatory text field upon submission in an Angular 5 application

Here is a template I am working with that includes a textbox: <input type="checkbox" id="s_i{{i}}" name="s_i{{i}}" (change)="clickObj($event, myObjs[i])" [(ngModel)]="myObjs[i].isSelected"> <input type="text" [ngModelOptions]="{ updateOn: 'b ...

Tracking mouse positions across an HTML document

I've been working on a project inspired by the voxel painter example on the three.js page. In this particular example, the mouse coordinates are utilized to move a roll-over helper that indicates where a box will be placed upon click. mouse.set((even ...

Having trouble converting a string to a date format in Firefox using JavaScript code

Having trouble converting a String to date format in Firefox, but it works perfectly in Chrome. Check out the code snippet below: <input type="hidden" name="userDate" id="userDate" value="26-Aug-2014"/> <script> $(document).ready(function ( ...

Is there a way to add an HTML element using the Firefox inspector tool?

Is there a way to insert new HTML elements in Firefox Inspector Tool? I've been searching for a solution but can't seem to find one. Here is a related question, but it pertains to the Google Chrome inspector. ...

In Java using Selenium, I am able to retrieve the text of an element but experiencing difficulty clicking on its link

I am encountering difficulties with clicking on a link within a webpage using the Selenium webdriver on Firefox. Below is a snippet of the code related to the issue: WebElement option = driver.findElement(By.id("AccNumSpan" + i)); if(opt ...

React-bootstrap's Modal is glitching and only partially appearing on the screen

I am a beginner in frontend development and I've been struggling to position the modal at the center of the screen; it keeps appearing on the right side. I am currently using "bootstrap/dist/css/bootstrap.min.css" for my CSS. Should I create a new CSS ...

Transform CSS filters into SVG filters

Implementing CSS filters on the <img /> elements has been successful for me. For example: filter: brightness(1) contrast(67%) saturate(250%) grayscale(0%) invert(0%) hue-rotate(330deg) sepia(40%) blur(0px); Now, I am trying to apply the same filte ...

Android experiencing issues with dynamically loading Border XML

I am having trouble setting a border dynamically for a RelativeLayout. Oddly enough, when I manually add the border in the activity XML file, it displays perfectly. However, when I try to generate the border dynamically, it doesn't appear. border.xml ...

Getting CSS source maps working with LESS in Webpack 4: A user's guide

I have been attempting for an extended period to configure CSS sourcemaps in webpack with no success. I am unsure where the issue lies within the process. I am hopeful that someone can provide guidance in the right direction. Here is the situation: https ...

FullCalendar fails to load in Bootstrap 4 tab

My current project involves utilizing Bootstrap 4 tabs, and I encountered an issue with displaying a FullCalendar on the second tab. Specifically, I am using version 5.2.0 of the FullCalendar library. While the calendar functions correctly when placed on t ...