Hovering on the division element will only trigger an effect when the mouse

Currently working on designing a navbar, where each button consists of a <div> for the box and an anchor tag for text along with a hyperlink.

The challenge I'm facing is changing the color of the div when hovering over it. Adding a :hover pseudo-class in CSS only impacts the color change when hovering over the anchor tag itself, not the entire div. As a result, the box's color changes only when the text is hovered over.

To achieve the desired effect, I have to duplicate all styles from the normal div to the hover div. This is necessary as modifying only the color property affects only the text. (refer to the last image showing how the div looks with just a background-color property)

I hope this explanation was clear enough.

Code snippet and example provided below:

(within a <nav>)

<div class="menu">
            <div class="mapbox"><a class="map" href="">Erangel</a><br></div>
            <div class="mapbox"><a class="map" href="">Miramar</a><br></div>
            <div class="mapbox"><a class="map" href="">Sanhok</a><br></div>
            <div class="mapbox"><a class="map" href="">Vikendi</a><br></div>
            <div class="mapbox"><a class="map" href="">Taego</a><br></div>
            <div class="mapbox"><a class="map" href="">Karakin</a></div>
</div>
.map{
    font-family: 'Rubik Mono One', Arial;
    font-size: 1vw;
    color: white;
    text-decoration: none;
    text-shadow: 0.125vw 0.125vw #282b30;
}
.mapbox{
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #424549;
    border-radius: 1vw;
    width: 12.5vw;
    height: 2vw;
    margin: auto;
    margin-bottom: 0.85vw;
    box-shadow: 0.225vw 0.225vw #1e2124;
}
.mapbox:hover{
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #7289da;
    border-radius: 1vw;
    width: 12.5vw;
    height: 2vw;
    margin: auto;
    margin-bottom: 0.85vw;
}

https://i.sstatic.net/RlRrr.png https://i.sstatic.net/CQ4Gn.png https://i.sstatic.net/g28pB.png

Answer №1

The problem lies in the incorrect spacing before :hover.

Before: .mapbox :hover

After: .mapbox:hover

.map {
  font-family: 'Rubik Mono One', Arial;
  font-size: 1vw;
  color: white;
  text-decoration: none;
  text-shadow: 0.125vw 0.125vw #282b30;
}

.mapbox {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #424549;
  border-radius: 1vw;
  width: 12.5vw;
  height: 2vw;
  margin: auto;
  margin-bottom: 0.85vw;
  box-shadow: 0.225vw 0.225vw #1e2124;
}

.mapbox:hover {
  background-color: #7289da;
}
<div class="menu">
  <div class="mapbox"><a class="map" href="#">Erangel</a><br></div>
  <div class="mapbox"><a class="map" href="#">Miramar</a><br></div>
  <div class="mapbox"><a class="map" href="#">Sanhok</a><br></div>
  <div class="mapbox"><a class="map" href="#">Vikendi</a><br></div>
  <div class="mapbox"><a class="map" href="#">Taego</a><br></div>
  <div class="mapbox"><a class="map" href="#">Karakin</a></div>
</div>

Answer №2

To solve the issue, I simply switched the positions of the <div> and <a>.

Rather than having the <a> nested inside the <div>, I moved the <div> within the <a> tag and updated the CSS hover class accordingly.

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

When using the navbar in Tailwind CSS, the image is appearing distorted instead of aligning properly with the adjacent

I am trying to add a flag, login button, and sign up button Using class="flex", I placed an image inside it However, when I add the buttons, the image gets distorted. How can I prevent this? Expected Outcome I want to create the flag, login but ...

In search of assistance to update a list during the creation of a Windows 8 application with HTML, CSS, and AngularJS, all powered

I'm currently working on an app that acts as a virtual warehouse, keeping track of item balances. After experiencing the challenges of using WinJS in comparison to AngularJS, I decided to combine both frameworks for my project. Adding and deleting i ...

Easier JavaScript for numerous HTML elements

I am an aspiring JavaScript learner seeking advice. Currently, I am working on a website that features numerous items with multiple images for each. I am utilizing JQuery to display a larger image when the user hovers over a thumbnail image. My query is ...

What specific CSS property creates the distinction in text vertical alignment between a <button> and a <div>?

I recently discovered that the text inside a <button> is automatically vertically centered, whereas the text inside a <div> is aligned to the top. Despite my best efforts, I couldn't determine which CSS rule was responsible for this disti ...

Incorrect formatting of HTML email on Outlook

Here is the code snippet I am using for an HTML email: <div style="background-color:red;max-width:600px;height:140px;margin-left:auto;margin-right:auto;"> <img src="https://via.placeholder.com/140x99" height="140" wi ...

Is there a way for me to duplicate a complex element multiple times within the same page?

As an illustration, let's say I have a social tab located in my header that I would like to duplicate in the footer. This tab is comprised of various buttons with SVG images on top, event listeners linked to button IDs, and CSS styling. One option is ...

What could be causing the header of the datatable to be out of alignment with the rest of

I'm facing an issue with my datatable where the header is misaligned with the content. Can someone please assist me in adjusting the header and content so that they are parallel? It doesn't look right at the moment. <div style="margin-top:1 ...

ASP.NET ensures that the entire page is validated by the form

Is it possible to validate only a specific part of the form instead of the entire page? Currently, when I try to validate textboxes on the page, the validation is applied to all textboxes. Here are more details: https://i.stack.imgur.com/eowMh.png The c ...

I must update a bootstrap class name within multiple layers of divs by referring to the parent class

My code structure is set up like this: <div id="main-parent"> <div class="child2"> <div> child2 </div> </div> <div>child3</div> - - - - <div class="ch ...

What is the process for utilizing jQuery's advanced ticker feature to extract content from a text file?

I am currently implementing this code on my website: <script> var file = "http://newsxpressmedia.com/files/theme/test.txt"; function getData(){ $.get(file,function(txt){ var lines = txt.responseText.split("\n"); for (var i = ...

Prevent a div from being displaced by the transform property once it reaches the window's border

Is it possible to prevent the viewer I created using CSS transform from moving when its borders reach the window borders? Should I consider using a different method instead? If you'd like to see the code, you can check it out here. var x=0, y=0 ...

Colorbox's functionality struggles to uphold several groups simultaneously without encountering errors

I have incorporated Colorbox on this page. On the setup, I have various groups listed as groups 1 to 9. However, when clicking on the second and third items, they display correctly according to the provided code. My aim is to make everything appear in the ...

The Opera browser displays a horizontal orientation for vertical menus

Having some trouble with my vertical menu rendering correctly in different browsers. It's appearing horizontal in Opera, but looks good in Firefox and Chrome. I'm pretty sure it's just a small tweak needed in the code, but I can't quite ...

Automatically scroll a div when the internal div is in focus

I want to create a scrollable div that allows for scrolling even when the mouse is over the content inside the div, not just on the blank areas beside it. Here is the code I am currently using: var main = document.getElementById('main-site'); va ...

The alignment of the text in the footer is off

Currently experiencing an issue: The alignment of the footer text seems off. When using text-align center, it appears slightly left of center. Text-align left places it further to the left and text-align right works correctly. I attempted using display: t ...

Guide to Including Captions and Spans in a Table

In the given HTML code, nested tables are used by the developer which cannot be changed. However, there is a requirement to replace the table class and add a caption and span only to the main table. <table class="two_column_layout" align="center"> & ...

Jekyll is not beginning line numbers from the first line as they should be

My website was put together using Jekyll, specifically with the Beatiful-Jekyll theme. To make syntax highlighting possible, I utilized Rouge. The issue arises when displaying line numbers alongside the code - sometimes they don't align properly or ar ...

How to customize nouislider appearance using Bootstrap styles

I've been attempting to style the noUi tooltips to match the bootstrap design, but I've been unsuccessful so far. I'm wondering if there's an easy way to achieve this. I tried to structure the markup based on the bootstrap template ava ...

Hide the element once the CSS animation has finished

I have successfully implemented an animation using pure CSS that starts on load. However, I am facing an issue with both opacity and visibility as the div continues to take up space even when hidden. Question Is there a way to make the div disappear comp ...

Tips for defining conditions within the style attribute in JSP

Below is the code that I attempted: <div style="width:85%;"> <input class="formbutt" value="AddNew" title="AddNew" type="button" style="{(${projectEnvironmentBean.divStyle}=='dipslay:none') ? 'display:block' :'display: ...