Menu text changing color on hover

I am trying to implement a unique hover effect where the color of the menu changes when a specific heading is hovered over. More specifically, I have a heading that reads "Not a restaurant, but here for them." When the user hovers over the word "restaurant" in this heading, I want the menu text color to change to white, the word "restaurant" to red, and the rest of the heading to white. Currently, I have the second part of the effect working - where "restaurant" changes to red and the rest of the heading changes to white. However, I am struggling to figure out how to also change the color of the menu. Here is the CSS code I have been using:

.headingRestaurant:hover {
  color: red;
}

.headingRestaurant {
  cursor: pointer;
}

.textb {
  pointer-events: none;
}

.headingRestaurant {
  pointer-events: initial;
}

.textb:hover {
  color: white;
}
<nav>
  <ul>
    <li>
      <a href="file:///C:/Users/.../index.html">Home</a>
    </li>
    <li>
      <a href="file:///C:/Users/.../about.html">About</a>
    </li>
  </ul>
</nav>

<h1 class="textb">
  Not a <span id="heading1" class="headingRestaurant">restaurant</span>,
  <br> but here for them.
</h1>

Answer №1

When it comes to CSS interactions, the limitation of only affecting elements within or below the current element can be overcome using Javascript to manage hover effects for you. By utilizing the addEventListener function, you can create both mouseover and mouseout events on your restaurant text to dynamically add or remove a hover class from any desired element.

var nav = document.querySelector('nav');
var headingRestaurant = document.querySelector('.headingRestaurant');

headingRestaurant.addEventListener('mouseover', function() {
    nav.classList.add('hover');
});

headingRestaurant.addEventListener('mouseout', function() {
    nav.classList.remove('hover');
});
.headingRestaurant:hover {
    color: red;
}

.headingRestaurant {
    cursor: pointer;
}

.textb {
    pointer-events: none;
}

.headingRestaurant {
    pointer-events: initial;
}

.textb:hover {
    color: white;
}

nav.hover,
nav.hover a {
    color: red;
}
<nav>
    <ul>
        <li>
            <a
            href="file:///C:/Users/.../index.html"
                        >Home</a
                        >
        </li>
        <li>
            <a
            href="file:///C:/Users/.../about.html"
                        >About</a
                        >
        </li>
    </ul>
</nav>

<h1 class="textb">
    Not a <span id="heading1" class="headingRestaurant">restaurant</span>,
    <br />
    but here for them.
</h1>

If you prefer to stick to HTML and CSS exclusively, altering the order of your elements in the HTML structure is necessary. Placing the element you want to modify below the triggering element enables this setup. In this scenario, repositioning the nav and h1 within a container div and adjusting their order accomplishes this. The presentation sequence is regulated with the properties display: flex and flex-direction: column-reverse. Hover effects can be achieved by applying the CSS selector ~, which selects an element that follows another. For example, .textb:hover ~ nav targets a hovered over .textb element followed by nav. Specific menu items can also be tailored using this method.

.headingRestaurant {
    cursor: pointer;
    pointer-events: initial;
}

.textb {
    pointer-events: none;
}

.textb:hover {
    color: white;
}

.textb:hover .headingRestaurant {
    color: red;
}

.textb:hover ~ nav,
.textb:hover ~ nav a {
    color: red;
}

.textb:hover ~ nav a.about {
    color: purple;
}

.reversed {
    display: flex;
    flex-direction: column-reverse;
}
<div class="reversed">
  <h1 class="textb">
      Not a <span id="heading1" class="headingRestaurant">restaurant</span>,
      <br />
      but here for them.
  </h1>

  <nav>
      <ul>
          <li>
              <a class="about" href="file:///C:/Users/.../index.html">Home</a>
          </li>
          <li>
              <a href="file:///C:/Users/.../about.html">About</a>
          </li>
      </ul>
  </nav>

</div>

Answer №2

Using :has is the recommended approach, although some creative individuals may devise alternative solutions. It should be noted that full support for this feature is still in progress.

/* Aesthetic CSS styles */

nav ul li {
  display: inline-block;
  padding: 0.5rem 1rem;
  margin: 0;
  border: 1px dotted red;
}

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

nav ul li a {
  text-decoration: none;
  color: inherit;
}


/* Functional CSS styles */

.headingRestaurant:hover {
  color: red;
}

.headingRestaurant {
  cursor: pointer;
}

.textb {
  pointer-events: none;
}

.headingRestaurant {
  pointer-events: initial;
}

.textb:hover {
  color: white;
}


/* Menu highlighting on hover */

body:has(.headingRestaurant:hover) nav {
  background-color: lightblue;
}
<nav>
  <ul>
    <li>
      <a href="file:///C:/Users/.../index.html">Home</a>
    </li>
    <li>
      <a href="file:///C:/Users/.../about.html">About</a>
    </li>
  </ul>
</nav>

<h1 class="textb">
  Not a <span id="heading1" class="headingRestaurant">restaurant</span>,
  <br> but here for them.
</h1>

Answer №3

To customize the styling of your menu element when hovering over the .headingRestaurant element, simply replace "target" with the class or id you are using to identify the menu.

.headingRestaurant:hover target {

}

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

I am experiencing an issue where the left sidebar div is not aligning correctly in my HTML

I am currently facing an issue with a gap that I can't seem to fix right now. Below, you will find a screenshot along with the HTML and CSS code that I have used. Despite trying various methods and looking for solutions on platforms like Youtube and ...

Turn off webpage caching in HTML5

I am looking for a way to disable the browser cache using HTML5. While I came across this helpful post (How to control web page caching, across all browsers?), it only provides information for HTML4 or other languages. In my web application, I utilize Ja ...

Tips for concealing a parent element while inside a span with jquery

Beginner asking for help! Take a look at what I've coded: <div class="Links"> <a href="/testthis1.html" data-id="button1"> <img class="icon" src="/test1.png" alt="test1"> <span>Test 1</span> < ...

How to align a div vertically in relation to another div with automatic height

My challenge is to create a vertically-aligned div in connection with an auto-height div. It's difficult to explain, so I've included a screenshot that should clarify things: The orange div represents the main container. The blue div is a 2n ...

Issue with second button in Angular Onclick() not been resolved

I'm experiencing an issue with the onClick() methods in my code. There are two onClick() methods present on the UI, but when I click on the second one, it does not call the method while the first one does. I am struggling to figure out why this is hap ...

Progress Bar Modules

I am currently working on creating a customizable animated progress bar that can be utilized as follows: <bar [type]="'health'" [percentage]="'80'"></bar> It is functional up to the point where I need to adjust different p ...

Sending form array information using jQuery AJAX

My form allows for the addition of multiple listings simultaneously. Additionally, there is a requirement to pass through an md5check. For example: <select name="master_id"></select> <select name="id2[]"></select> <select nam ...

Bootstrap creates a small and calculated width for elements

Currently, I am integrating the react-datepicker plugin into my project alongside Bootstrap 3. Upon implementation, I have noticed that the size of the datepicker on my website appears to be smaller than the examples provided in the plugin demos. It seems ...

Separate the paragraph within an HTML string and eliminate any paragraph that is empty

I need help splitting a string of HTML into an array list, with the condition that the paragraphs should not be empty. Each paragraph should contain some normal text, and if it only contains HTML text without any normal text like: <htmltag>&nbsp; ...

Adding images in real-time

I am currently working on an Angular application where I need to assign unique images to each button. Here is the HTML code snippet: <div *ngFor="let item of myItems"> <button class="custom-button"><img src="../../assets/img/flower.png ...

Issue with aligning items vertically using CSS and Javascript

I must admit, I am not well-versed in CSS. My goal is to create a performance bar using CSS and Javascript. So far, I have managed to generate a background bar with another bar inside that fills up to a specific percentage. However, my issue lies in the fa ...

Adjusting table rows in Vuejs based on different screen sizes

I am working with a table that has two <tr> tags in its first row (I know, it's not ideal but it has to be this way): First tag: <tr class="abc" @click="expanFunc"> Second tag: <tr class="abc"> My goal is to display only the ...

Leveraging font awesome ttf in conjunction with scss

I am currently attempting to incorporate Font Awesome with SCSS. @font-face { font-family: 'awesome'; src: url('../../fonts/font-awesome/fa-light-300.ttf') format('ttf'); } Although the path appears to be correct, I am ...

HTML date input with a disabled date picker

I am using a simple date input field <input type="date" id="sign-up-dob" class="sign-inputs" max="2999-12-31"></input> Here is what it looks like: https://i.sstatic.net/fdM0r.png Is there a way to remove the arrows and calendar icon on the ...

Having trouble getting custom tabs in jQuery to function properly?

I am facing an issue with a simple script I have created. The script is supposed to display a specific div when a user clicks on a link, and hide all other divs in the container. However, when I click on the link, nothing gets hidden as expected. Even afte ...

Convert the canvas to an image by right-clicking

Is it possible to treat the canvas element as an image when using drawImage() to draw on it? When I attempt to right click on the canvas element that has been drawn on, the option "Save image as" does not appear. The right click menu displays: What step ...

What is the best way to trigger ajax when the user clicks the back button to return to the previous webpage?

Check out my code snippet below: HTML Code <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="body"> <div class="dropdown_div"> <select id="q_type" class="dropdown" ...

The functionality of `config.assets.debug = true` fails to

I've encountered an issue with the configuration on development where config.assets.debug = true doesn't seem to work correctly. Instead of having multiple separate JavaScript and CSS file inclusions, I'm getting a consolidated one: <li ...

Ways to rotate just the border without rotating the icon through CSS

I need help with rotating only the border 135 deg on hover, without affecting the icon. Below is the code I am using. Any assistance would be greatly appreciated. Thank you! My html: ` <div class="container-fluid details-section"> ...

ways to modify hash URLs using JavaScript

I am looking to add separate hash links for my Facebook and Instagram social media pages. I also want to change the text color to blue for Facebook and yellow for Instagram. How can I achieve this using JavaScript? let socialMediaData = [{ s_media: &apo ...