Discovering a specific element within a parent container using CSS

When hovering over an element with the class .parent, it triggers the application of CSS to child elements with the class .child.

Check out the code on jsFiddle

Can this effect be achieved using only CSS?

Sample HTML:

<div class="parent" style="background:red;margin:20px;padding:20px;">
    <div class="child" style="background:blue;margin:20px;padding:20px;"></div>
</div>

<div class="parent" style="background:red;margin:20px;padding:20px;">
    <div class="child" style="background:blue;margin:20px;padding:20px;"></div>
</div>​

Using jQuery:

$('.parent').hover(
  function () {
    $(this).find('.child').css('background','yellow');
  }, 
  function () {
    $(this).find('.child').css('background','blue');
  }
);​

Answer №1

Reorganize the CSS by removing the inline styles and placing them in an external stylesheet:

/* External Stylesheet */
.parent { background: red; margin: 20px; padding: 20px; }
.child {background blue; margin: 20px; padding: 20px;}
.parent:hover > .child { background: yellow; }

Answer №2

Yes.

.parent .child {
    background: blue;
}

.parent:hover .child {
    background: yellow;
}

This code mimics the effect of your jQuery code. If you specifically mean "child" (not just any descendant), then use the following:

.parent > .child {
    background: blue;
}

.parent:hover > .child {
    background: yellow;
}

As noted by @hobbs, this won't work in IE 6 because :hover is only supported on links in IE 6.

Answer №3

A suggestion is to transfer your inline styles into the classes so you can avoid using the !important declaration (or else the inline styles will always take precedence over the style sheet).

<div class="parent">
    <div class="child"></div>
</div>

<div class="parent">
    <div class="child"></div>
</div>​
.parent {
    background: red;
    margin: 20px;
    padding: 20px;
}
.child {
    background: blue;
    margin: 20px;
    padding: 20px;
}
.parent:hover .child {
    background: yellow;
}

Take a look at the jsFiddle.

Answer №4

.container .element {
    background: blue;
}

.container:hover .element {
    background: yellow;
}

This code should work fine, however, keep in mind that it may not behave as expected on certain versions of Internet Explorer where the use of :hover is limited to A elements only. Thankfully, jQuery has a workaround for this limitation whereas CSS does not have one.

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

Leveraging the power of PHP includes with nested subdirectories

Hello, I recently configured my web server to run all .html files as .php files, allowing me to utilize the php include function, which has been very beneficial. However, I have various subdirectories with multiple files in each one. Homepage --banners ...

Using Cookies with Jquery Tabs

I am attempting to implement the Jquery cookie plugin in order to store the last selected tab. Despite my efforts, I have not been able to make it work. Is there something else required in addition to the jquery library and cookie plugin? Here is the cod ...

Exploring the complete range of bootstrap classes within collapsible buttons and editing them to suit your needs

I am currently working on implementing collapsible buttons into my web application, which is created using Google Apps Script (GAS). The buttons are functioning correctly and toggle the content visibility as intended through jQuery. I have inserted the ne ...

Configuring static files in Django for hosting a website in production mode

I have been attempting to serve my static files from the same production site in a different way. Here are the steps I took: First, I updated the settings.py file with the following: DEBUG = False ALLOWED_HOSTS = ['12.10.100.11', 'localhos ...

Jquery auto-suggest form validator

I have implemented a search dropdown menu using jQuery. Everything is working fine, and the 'not match' message displays properly when entering data that does not match any items. However, if I select an item from the dropdown and then modify it ...

How to Retrieve a Specific Line with jQuery

Could jQuery be used to retrieve the offset of the first letter in the 5th visual line of a block's content? I am referring to the visual line determined by the browser, not the line seen in the source code. ...

Having trouble with the locality function in Google Places v3 API autocomplete?

After successfully using the code below for about a week, I returned to work on it and found that it was no longer functioning properly. My goal is to only display localities. According to Google's documentation, "locality" is the correct option for a ...

Difficulty integrating Bootstrap with JavaScript file in Visual Studio Code

I am attempting to incorporate a dynamic progress bar by utilizing Bootstrap's progressbar and creating a custom JavaScript file. The goal is to continuously adjust the width of the progress bar at regular intervals to give the appearance of a moving ...

Enable the central bootstrap button to expand and occupy the entire width

Here is a code snippet that I have: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>test</tit ...

Are there varying levels of efficiency when retrieving elements by ID versus by class using JavaScript/jQuery?

When it comes to JavaScript/jQuery, is there a performance disparity between locating elements by id versus by class? Is one method superior over the other? Could it be attributed to ID or Class indexes being generated in the DOM? Ultimately, does the di ...

Reactively created menu using JQuery

Looking to implement a dynamic JQuery menu using React (ES6). Utilizing the JQuery menu examples (https://jqueryui.com/menu/) as a guide, however, those examples only cover static menus. It doesn't clarify how to modify menu items like updating labels ...

Using multiple selectors in an if statement

I'm currently working on ensuring that a button is disabled to prevent users from saving data when two specific conditions are met: The checkbox is checked There is no input in the specified form field ( #read_return_response_rate_ids ) This is the ...

Display a preview of a React component

Currently facing a challenge where I need to create a form for users to adjust the title, background color, button background, text color, and other settings of a component. I want to provide a live preview of the component as the user makes changes. I en ...

Issues with the functionality of the bootstrap modal jQuery function in Firefox, causing problems with scrollbars and dynamic modal height adjustments

My newly launched website is built using bootstrap v3 and modals to showcase detailed information about each game server that is queried. Each modal provides insights into the current players on the server, their score, and play time. While implementing ...

The AJAX functionality seems to be failing to load the additional JavaScript and CSS files

Using a brief AJAX code, I fetch Facebook content from a PHP file where class="gallery-img-link" is defined as <a> and class="img-responsive img-thumbnail" is declared as <img>. The issue arises when attempting to load these classes via an AJA ...

The dropdown menu displaying the img-dropdown-content with a display set to none is not functioning as anticipated

Attempting to hide drop down menu with img-dropdown-content but it's not working, possibly being overridden by another element - only using html and css I have experimented with visibility: hidden; both with and without !important as well as display: ...

Avoid placing the teg p on top of the image

index.html {% extends 'main/base.html' %} {% load static %} {% block title %} {{title}} {% endblock %} {% block content %} <link rel = 'stylesheet' href="{% static "main/css/index.css" %}"> <body> <d ...

Troubleshooting issue with CSS3 linear-gradient not functioning as expected

After watching a CodePen demo where CSS3 linear-gradient was used to create color effects, I attempted to use the same property on jsFiddle but it wasn't working. Can someone help me troubleshoot this issue? You can view the original CodePen code her ...

Is there a way to create an <a> element so that clicking on it does not update the URL in the address bar?

Within my JSP, there is an anchor tag that looks like this: <a href="patient/tools.do?Id=<%=mp.get("FROM_RANGE") %>"> <%= mp.get("DESCRITPION") %></a>. Whenever I click on the anchor tag, the URL appears in the Address bar. How can ...

Tips for creating a menu that stays fixed to the page even when positioned absolutely

I'm currently working on creating a navbar that needs to be positioned to touch the edges of the screen. Here is the CSS code snippet that I have implemented so far: nav { position: absolute; top: 0; left: 0; right: 0; height:65px; displa ...