What is the method for switching the parent element following the adjustment of the selected child's opacity

I am trying to find a way to use CSS to adjust the parent element's opacity if it contains a child element with a specific class. Currently, I have only been able to achieve this on hover by adding a background color to the parent element's ::after pseudo-element. Below is the code I have implemented:

HTML

<ul class="slider-thumbs">
  <li class="reason-item"><a>Item 1</a></li>
  <li class="reason-item"><a class="selected">Item 2</a></li>
  <li class="reason-item"><a>Item 3</a></li>
  <li class="reason-item"><a>Item 4</a></li>
  <li class="reason-item"><a>Item 5</a></li>
</ul>

CSS

.slider-thumbs .reason-item{position: relative;vertical-align: top;}
.slider-thumbs .reason-item::after {content: '\A';position: absolute;width: 100%; height:100%;top:0; left:0;background:rgba(252,194,38,0.9);opacity: 0;transition: all 1s;-webkit-transition: all 1s;}
.slider-thumbs .reason-item:hover::after, .slider-thumbs .reason-item:after + a.selected{opacity: 1;}

My goal is to change the opacity of the parent element that contains a child element with the "selected" class when an item is selected, so that users can easily see their selection when the page loads. Currently, my code only works for the hover state.

To view my code in action with the hover state, please check out the following Fiddle link.

Answer №1

It's important to eliminate :after from the last rule because there is no a.selected with a pseudo element of ::after.

Your revised code should look like this:

.slider-thumbs .reason-item{
  position: relative;
  vertical-align: top;
}
.slider-thumbs .reason-item::after {
  content: '\A';
  position: absolute;
  width: 100%; 
  height:100%;
  top:0; 
  left:0;
  background:rgba(252,194,38,0.9);
  opacity: 0;
  transition: all 1s;
  -webkit-transition: all 1s;
}
.slider-thumbs .reason-item:hover::after,
.slider-thumbs .reason-item + a.selected{
  opacity: 1;
}
<ul class="slider-thumbs">
<li class="reason-item"><a>Item 1</a></li>
<li class="reason-item"><a class="selected">Item 2</a></li>
<li class="reason-item"><a>Item 3</a></li>
<li class="reason-item"><a>Item 4</a></li>
<li class="reason-item"><a>Item 5</a></li>
</ul>

Check out your updated fiddle here:

https://jsfiddle.net/0vLmkhr2/3/

Answer №2

CSS alone cannot traverse back up the DOM structure, as it is not designed for that purpose. However, if your element has the class selected, you can achieve this using jQuery:

$(".selected").find().parent("li").css("color", "red");

Answer №3

The style has been updated to target the anchor tag instead of the list item, which has resolved the issue. Below is the revised code snippet:

.slider-thumbs li{position: relative; }
.slider-thumbs a {vertical-align: top; display:block; word-break: break-word;}
.slider-thumbs a::after {
  content: '\A';
  position: absolute;
  width: 100%; 
  height:100%;
  top:0; 
  left:0;
  background:rgba(252,194,38,0.9);
  opacity: 0;
  transition: all 1s;
  -webkit-transition: all 1s;
}
.slider-thumbs a:hover::after, 
.slider-thumbs a.selected{
  opacity: 1;
}

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

"Using html_attr with the attribute "href" does not return any value in the rvest package

My objective is to extract the URLs linked with specific CSS elements on a website using rvest. Despite trying various methods, such as using the html_attr function with the 'href' argument, my current script only returns NA values instead of the ...

Developing User-Friendly Websites with Responsive Design

While I was working on my project, I realized that using only pixel values in tables and sidebars was a big mistake. This caused issues because if someone had a different screen resolution, my website would look distorted and unappealing. Could you please ...

The Ultimate Guide to Setting up SVG's Viewbox, Width, and Height for Scalability and Responsiveness

As a beginner in SVG design, I find it challenging to scale, zoom in/out with the container and ensure responsiveness across different devices. I wonder if it's possible to create an SVG that adapts to all device sizes and effectively handles browsin ...

Transferring information from template to associated component

Is it possible to transfer data from a template to a component without the need for an event trigger like a button or form submission? For instance, in the code snippet provided, how can we pass the 'item' from the 'items' array to the ...

Avoid placing the label within a form-inline in Bootstrap to maintain proper formatting and alignment

I am working on a form where I have two fields inline within a form-inline div. I am looking to move the labels of these fields above them rather than next to them. How can I achieve this without affecting the layout of the fields? Here is a CodePen link ...

jQuery not functioning properly when included in a separate JavaScript file

I'm facing an issue with my jquery code not working properly when I place it in a separate js file instead of embedding it directly into the html. Currently, my setup looks like this: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3 ...

Fixing the Bootstrap Datepicker: A Step-by-Step Guide

Is there a way to configure the Datepicker to display today's date and tomorrow's date? Click here for the JS file Check out the image on our website https://i.stack.imgur.com/VyFUB.jpg ...

Trouble opening attached html file with Asp.net and Google Charts integration

I am facing an issue with my Asp.Net page that includes a grid and an image. The image is a Google Charts chart with a URL of approximately 1600 characters. To send this content as an email attachment, I created an .htm file containing the grid and the ima ...

Place the text (menu) adjacent to the navigation bar

I am currently using bootsrap 4 alpha 6 in combination with midnight.js to alter the color of my navigation menu toggler. I am trying to incorporate a text element (MENU) alongside it, similar to the example shown in the Capture image. For the text toggler ...

Creating a smooth image transition effect using CSS

I have successfully implemented a code that allows for crossfading between images when the mouse hovers over them. Now, I am looking to modify the behavior so that the crossfade effect happens only once when the mouse passes over the image. In other words ...

Is it possible to manipulate CSS on a webpage using javascript?

I have incorporated this piece of JavaScript to integrate a chat box onto my website: window.HFCHAT_CONFIG = { EMBED_TOKEN: "XXXXX", ACCESS_TOKEN: "XXXXX", HOST_URL: "https://happyfoxchat.com", ASSETS_URL: "https://XXXXX.cloudfront.ne ...

Limit the amount of text displayed on the main section of the webpage

I am working on creating a simple webpage with three different sections styled like this: .top { position:absolute; left:0; right:0; height: 80px; } .left { position:absolute; ...

Element Not Adjusting to Content Size

I am encountering an issue with my div. I have floated an image to the right and placed my text beside it. My aim is to create a div with a linear gradient, however, the div ends up filling the entire page. How can I make my div adjust to fit only its con ...

Exploring the world of HTML5 speed testing: getting started

Can anyone provide guidance on how to begin with an HTML5 bandwidth test? I am trying to replicate a flash-based version and any recommendations would be greatly appreciated. ...

Managing Events for a Menu Item That Cannot Be Selected

As I work on developing a form using Material UI React, my focus is on logging exercise sets to the database. To allow users to select the exercise they wish to log, I have incorporated a list of Material UI Menu Item components as options. Additionally, I ...

An error occurred while trying to retrieve a resource from the bower_components directory using Vue.js CLI

I encountered an error in my project when trying to reference CSS and JS files. Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/bower_components/bootstrap/dist/css/bootstrap.min.css This is how my file ...

The Electronjs application encountered an error while loading with the message "ReferenceError: require is not defined."

Having trouble with an electron application as I encounter an error when running npm start: $ npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="483c2d3b3c087966786678">[email protected]</a> start C:& ...

Creating an uncomplicated selector bar for a basic javascript slideshow

I've spent the last couple of hours teaching myself some basic JavaScript, so please bear with me. Currently, I have a simple code in place for a straightforward slideshow on a website. Here is the JavaScript I'm using (where the 'slide&apo ...

Clicking 'Back to Top' within an accordion

On my webpage, I have two scrolls present. One is clearly visible on the page (not loaded with ajax), while the other one is located inside an accordion. Both of them share the same class names. I want these scrolls to always be positioned at the top. I&a ...

Include an iframe with hidden overflow specifically for Safari browsers

I have a situation where I'm using a third-party iframe to display specific content. The iframe is enclosed within a div container that has border-radius and overflow: hidden properties applied. Strangely, the content inside the iframe doesn't se ...