Hover effect on nested divs with the same structure

Check out this link: http://jsfiddle.net/2hDB9/

I'm trying to achieve the following effect:

  • When hovering over Line 1, only the border of Line 1 should show.
  • If you hover over Line 2, only the border of Line 2 should be displayed, not both at the same time.

Here is the HTML structure:

<div class="container">
    <div class="box">
        Line 1.
        <div class="box">
            Line 2.
        </div>
    </div>
</div>

These are the CSS styles applied:

.container {
    width: 500px;
}

.box {
    padding: 12px;
    border: 1px solid grey;
}

.box:hover {
    border-color: red;
}

Answer №1

To achieve the desired effect, you will need to create two separate CSS classes for each Div element. Simply assign both Divs with the same class="box", this way the .box:hover command will be applied to both of them.

In the CSS code, specify that only the inner .box class should respond to the hover command:

.box .box:hover {
      border-color: red; }

Check out the solution here: http://jsfiddle.net/2hDB9/2/

Answer №2

If you want all classes to have the same style, it's not entirely achievable using only CSS.

Although the current responses touch on some aspects of this issue, I can offer a jQuery solution where all elements share the same class. However, I recommend renaming the classes and avoiding jQuery if possible.

Nevertheless, here is an alternative approach that doesn't necessitate changing the class names:

Explore this jsFiddle example - suitable for various scenarios.

$(".box").children().hover(function () {
    $(this).parent().css("border-color", "grey");
    $(this).css("border-color", "red");
}, function () {
    $(this).parent().css("border-color", "");
    $(this).css("border-color", "");
});

Answer №3

Changing its class would be a good idea. Instead of relying on JavaScript to determine the hovered element, a better alternative might be:

Consider using

.container > .box > .box:hover

Answer №4

Latest Update: http://jsfiddle.net/2hDB9/11/

In order to achieve the desired effect, you need to get a little creative. It involves some trickery to make it look like one element is nested within another. Below is the code snippet:

HTML:

<div class="container">
    <div class="box1">
        Line 1.
    </div>
    <div class="box2">
        Line 2.
    </div> 
</div>

CSS:

.container {
    height:50px;
    padding:50px;
}

.container:hover {
}

.box2 {
    height:50px;
    width:75%;
    border:2px solid black;
    position: relative;
    top: -110px;
}

.box2:hover {
    border:2px solid red;
}

.box1{
    position: relative;
    height:50px;
    padding: 20px 20px 40px 20px;
    top: -50px;
    left: -50px;
    border:2px solid black;

}

.box1:hover{
    border:2px solid blue;
}

By creatively using relative positioning in CSS, box2 can be visually manipulated to appear as a child of box1, even though they are actually siblings.

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

Focusing on a particular iframe

I am currently using the "Music" theme from Organic Theme on my WordPress site and have inserted this code to prevent SoundCloud and MixCloud oEmbeds from stretching the page width: iframe, embed { height: 100%; width: 100%; } Although the fitvid ...

The output of console.log in a browser context versus a code environment can vary

As I was building a carousel, I became completely lost with my HTML structure: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="Carousel.css&q ...

The Struggle with Bootstrap Accordion

I'm currently in the process of learning bootstrap. I copied the code for the 'Flush' Accordion from the bootstrap docs, but my version looks completely off. Despite linking the CSS stylesheet and JS Bundle, it doesn't seem to align pro ...

Ideas for designing nested grids/boxes with a touch of style

Hey there, I'm on the hunt for some creative ways to construct nested boxes like the ones illustrated in the image below. My preference is to achieve this using HTML and CSS, but I am open to utilizing frameworks such as Bootstrap for styling if neede ...

Troubleshooting Gulp Conversion of Bootstrap-Material Scss to CSS: Error Message "Import Not Found"

Upon completing my gulp task styles, I encountered the following error: Error in plugin "sass" Message: static\node_modules\bootstrap-material-design\scss\_variables.scss Error: File to import not found or unreadable: ~bootstrap/sc ...

How can I display only the initial image using php?

I need to hide all images except the first one. I do not want to use a SELECT query with LIMIT 0,1 every time. How can I select all images but display only the first one? Currently, I am using the Photoswipe plugin - you can find more information here. Th ...

What could be causing my CSS code to not function properly within Vue.js?

Having trouble with css in VueJs. I can't seem to figure out what I'm doing wrong. Generally, my scoped style css works fine in vuejs, I can target ids and classes without any issues. The problem arises when trying to change the internal css va ...

Boost the dimensions of the content property within a CSS class

I am currently using the content property to display a dashed arrow "-->" in my CSS class. However, the arrow appears very small and I would like to increase its size directly in the CSS class itself. This arrow is meant to be displayed before each elem ...

The mobile menu pop-up is malfunctioning

Encountering some challenges with the mobile menu on a website. The opening and closing animation of the background is functioning correctly, displaying the information as intended. However, when testing and clicking on one of the links, the animation simp ...

Content within Drawer featuring Material UI Scrollable Tabs

I am currently utilizing a material-ui drawer with the mini variant drawer structure. Inside this drawer, I have incorporated a series of tabs utilizing the prevent scroll buttons tabs design. The version of material-ui being used is 4.12.3. Regrettably, ...

Struggling to toggle the visibility of a table with a button - successfully hiding it, but unable to make it reappear?

I need a button that can toggle (show/hide) a table. Currently, my code hides the table successfully, but it fails to show the table again when I click the button. It seems like there is an issue with refreshing or redirecting after clicking the button for ...

Incorporating SASS/SCSS syntax within the <style> element

Can I include SASS/SCSS syntax directly in an .html file within a style tag? I'm interested in defining a variable within the .html file and then utilizing it in a .sass file. ...

What is the best way to adjust the height of a div based on its child content

I've been experimenting with various methods to extend the white background around the title to cover the rest of the content. I've tried using overflow, clear, min-height, max-height, and even the '*' selector but nothing seems to work ...

Is there a way to spin these hexagons around?

Hey there, I need some assistance with a problem. I have to rotate these three hexagons slightly, about 15 degrees or so. The catch is, it needs to work in Internet Explorer only. I've been struggling with this all day and it's getting ...

Align the HTML element to the right edge of its sibling element that is centered on the

Is it possible to achieve a specific layout using CSS where there is text inside a centered div, and underneath that, another div with right-aligned text? I'm unsure if this can be done effectively. I am currently unaware of how to accomplish this us ...

How come my DHTML navigation bar is displaying incorrectly in Internet Explorer 7?

This particular issue has me completely baffled. Normally I can navigate through most IE7 CSS bugs with some clever adjustments here and there, but this one has really stumped me! Take a look at the example page in IE7 and you'll notice that when hov ...

Guide to incorporating external CSS and JavaScript into an Angular 2 project with Express.js and Node.js

As a newcomer to Angular2 and NodeJs, I am eager to learn how to integrate external CSS and JS in an Angular2 and Nodejs express app. Specifically, I am looking to integrate a bootstrap admin theme into my Nodejs application. The screenshots below provide ...

Unable to move the content inside a fixed container by scrolling

Having difficulties with a menu layout where the parent container spans across the entire site and a div contains the actual content. Everything looks fine on larger screens, but on mobile devices, it's challenging to display all the content. I' ...

Troubleshooting the inability to set percentage values in Bootstrap styling

My goal is to create a bar with thin bars, but my use of Bootstrap limits my control over sizes. <template> <div class="container-fluid mx-auto"> <div class="row"> <div class="square rounded-pill&qu ...

CSS designs overlapping

I am facing an issue with a modal window that I dynamically load onto the client's website. The modal includes certain CSS styles, but sometimes these styles cause my form to break. For instance, I have an input with the class .my_btn { backgroun ...