Using CSS to create hover effects for specific classes of elements

Is there a way to make the button change color on hover when hovering over anywhere in the navigation bar (.topNav) instead of just when hovering over the individual button elements (.top, .middle, .bottom classes)? I tried using span to achieve this, but it ended up changing all spans within the .topNav class.

Here is the HTML code snippet:

<nav class="navbar-default"
 <div class="topNav">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle button_container collapsed" 
         [ngClass]="{'active': !menuCollapsed}" (click)="toggleMenu()">
         <span class="top"></span>
         <span class="middle"></span>
         <span class="bottom"></span>
       </button>
       </div>
        ....other content
 </div>
</nav>

And here is the SCSS code block:

.top, .middle, .bottom {
    background: white;
    transition: 0.25s;
}

.topNav:hover, .button_container:hover, .top:hover, .middle:hover, 
.bottom:hover {
    background: black;
    transition: 0.25s;
} 

Answer №1

Here is the corrected code:

.topNav{
  background: red;
}

.topNav:hover>.navbar-header>button {
  background: black;
  transition: 0.25s;
}
<nav class="navbar-default"><!-- OOPS YOU FORGOT TO CLOSE THIS TAG -->
 <div class="topNav">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle button_container collapsed" 
         [ngClass]="{'active': !menuCollapsed}" (click)="toggleMenu()">
         <span class="top"></span>
         <span class="middle"></span>
         <span class="bottom"></span>
       </button>
       </div>
        ....other content
 </div>
</nav>

  • You forgot to close the nav tag.
  • .topNav:hover>.navbar-header>button
    means when you hover over .topNav select a specfic child element ( the button ). Then do stuff with that ( paint it black )

Answer №2

header a:hover{
  color:#BFEFFF;
  font-weight:bold;
}

If you want to spice up your CSS code, consider incorporating something similar to this example. I implemented this on my website:

I've been advised by seasoned developers that relying heavily on CSS for large-scale projects may not be the best approach.

Answer №3

Just an FYI, I managed to make this work using two different solutions...

1) I found success by utilizing the navbar-header class and span elements. Interestingly, when I attempted a similar approach with the .button_container class instead of navbar-header, it didn't yield the same results. The reason for this discrepancy still eludes me.

Here is the code snippet employing span...

.navbar-header span {
    background: white;
    transition: 0.25s;
}    
.topNav:hover>.navbar-header span {
    background: black;
    transition: 0.25s; 
}

2) Alternatively, I achieved the desired outcome by using three separate classes...

.top, .middle, .bottom {
    background: white;
    transition: 0.25s;
}

.topNav:hover>.navbar-header>button>.top, .topNav:hover>.navbar-header>button>.middle, .topNav:hover>.navbar-header>button>.bottom {
    background: black;
    transition: 0.25s; 
}

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

How to move content without affecting the background of the `<body>` using scrolling

Can I achieve a glass morphism effect on my page without moving the background of the body? I only want to move the .container. Is this feasible? I attempted using background-attachment: fixed, but it did not work for me. Here is the code snippet I have be ...

What could be causing the sudden triggering of ngOnInit in Angular 4?

After sending a request to the server, ngOnInit() { this.service.get(1).subscribe((response) => { this.whenDateCurrent = new DateCustomDate(); }); } Within the same component, there is a method that retrieves this.whenDateCurrent: public getCurre ...

Collapse all "Read More" buttons simultaneously using Bootstrap

I am attempting to design a segment that contains multiple paragraphs that can be expanded or collapsed individually. How can I achieve this using only pure bootstrap css so that they do not all expand and collapse simultaneously? #summary { font-size: ...

Creating a loading mask for a section of a webpage using angularjs

How can I implement a loading mask/image for a $http request that loads data for a <select> tag on my webpage? I want the loading mask/image to only cover the specific section of the page where the data is being loaded. Here's the HTML code: & ...

Exploring the world of Angular CLI testing and the versatility of

Struggling with integrating Angular CLI's test framework and enum types? When creating an enum like the following (found in someenum.ts): const enum SomeEnum { Val0, Val1 } Utilizing it in this way (in app.component.ts): private someEnum = Some ...

The issue of not being able to type in the sweetalert 2 input within a material UI modal in a

Within a card displaying an order, there is a sweetalert2 popup that opens when trying to cancel the order, asking for a cancellation reason. This feature works seamlessly on the orders screen. https://i.sstatic.net/RWhOA.png <Grid item md={8} sm={12}&g ...

Problem with incorporating responsive gifs

I'm new to CSS and I'm trying to incorporate an animated GIF as a smartphone screen using Bootstrap to ensure responsiveness. I've managed to get it working for larger and medium screens, but the issue arises when resizing for smaller displa ...

What is the best way to deactivate certain JavaScript functions on my webpage when accessed through a mobile device?

Is there a way to disable specific JavaScript functions when accessing my website from a mobile device? While I can achieve this using CSS only, certain buttons require JavaScript functionality. Below is the internal JavaScript code: var menuBtn = docume ...

Is it possible to integrate ngx Pagination with Bootstrap?

Is it possible to integrate Bootstrap's pagination with ngx Pagination? I'm interested in using the functionality of ngx Pagination but styling it with the CSS from Bootstrap. ...

What is the best way to create a border of white space around the background color of a table cell using CSS?

Is there a way to create a white space around the background color of a table cell using CSS? My current code has the background color extend all the way to the edge, and padding only affects the content, not the background color. #main-monitor { widt ...

Loop through an array of div IDs and update their CSS styles individually

How can I iterate through an array of Div IDs and change their CSS (background color) one after the other instead of all at once? I have tried looping through the array, but the CSS is applied simultaneously to all the divs. Any tips on how to delay the ...

Angular compilation encounters errors

While following a tutorial from Angular University, I encountered an issue where running ng serve/npm start would fail. However, simply re-saving any file by adding or removing a blank space would result in successful compilation. You can view the screensh ...

Could one harness the power of SO's script for adding color to code within questions?

Similar Question: Syntax highlighting code with Javascript I've observed that Stack Overflow utilizes a script to apply color coding to any code shared in questions and answers, making it resemble how it would appear in an IDE. Is this script pub ...

Tips for assigning the 'store_id' value to a variable in Angular 6 within the Ionic4 environment

Trying to retrieve the store_id from the StorageService in Ionic4 (angular 6). Managed to retrieve the store_id using: let id = this.storageService.get('store_id'); id.then(data => { this.store.push(data) }); After pushing it into an ar ...

What is the best way to create a personalized image as the background in WordPress using CSS for a client?

I have this in my style.css .showcase{ background: url("x.jpg") no-repeat 0; } My website is built on WordPress and I have implemented advanced custom fields for the client to edit text. However, I am struggling to find a way for them to change the ...

What are some ways to adjust red and green blocks using CSS?

One question that arises is how to create a version of a webpage where only the yellow block can slide up, while the red and green blocks remain fixed. Currently, the green block is treated with the following CSS: position:sticky; right:0px; top:100px; ...

What is the best way to make just the background image transparent using HTML and CSS?

Hey there! I'm trying to make just the background image transparent, while leaving everything else non-transparent. Is this possible with Bootstrap? Here's a snippet of my HTML: HTML Section: <!-- Homepage Section --> <section id= ...

Is there a way in Angular2 to append a class name from a variable to the host element without removing the current classes?

I am facing a challenge where I have a class name stored in a variable and I need to apply it to the host element of my Angular2 component. However, I am struggling to find a solution for this. While I can easily add a constant string as a class using Hos ...

Retrieve data from a URL using Angular 6's HTTP POST method

UPDATE: Replaced res.json(data) with res.send(data) I am currently working on a web application using Angular 6 and NodeJS. My goal is to download a file through an HTTP POST request. The process involves sending a request to the server by calling a func ...

issues with functionality in purecss drop down menu

I have been struggling to create a vertical drop-down menu using the purecss library without success. purecss.io/menus/ This is what my CSS code looks like: <div class="pure-menu custom-restricted-width"> <ul class="pure-menu-list"> ...