How can you achieve a full-width hover effect on your website?

I am working with Bootstrap 5 to create dropdown navigation, but I am facing some challenges with implementing a hover effect that covers the full visible width. I would greatly appreciate it if someone could provide me with detailed explanations on how to achieve this. The problem I am encountering is that I want the :hover effect to be black and cover the entire visible width of the Bootstrap 5 dropdown when shown. I attempted something like this:

ul.dropdown-menu.show li a{
    width: 100%;
}

but unfortunately, it did not work as expected.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

header {
  position: sticky;
  top: 0;
}

#header {
  box-sizing: border-box;
  background-color: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 70px;
}

#header ul li a {
  text-decoration: none;
  margin-top: 5px;
}

#header ul li {
  list-style: none;
  display: inline-block;
  text-decoration: none;
}

.logo {
  background: black;
  text-align: center;
  width: 123px;
  padding: 5px;
  color: #fff;
}

.menu {
  text-align: right;
  flex: 1;
}

.menu ul li {
  margin-right: 3%;
  padding-top: 10px;
  color: #fff;
}

.menu ul li {
  display: inline-block;
  text-decoration: none;
}

.menuFirst {
  flex: 2;
  padding-top: 10px;
}

.menuFirst ul li a,
.menu ul li a {
  padding: 10px 10px 12px 10px;
}

.menuFirst ul li a:hover,
.menu ul li a:hover {
  background-color: #fff;
  color: #000;
}

.menuFirst ul li {
  display: inline-block;
  text-decoration: none;
  margin-left: 3%;
}

.menuFirst a,
.menu a {
  color: #fff;
}

a {
  text-decoration: none;
}

.navgation-custom li {
  -webkit-box-shadow: 0px 0px 15px 2px rgba(0, 0, 0, 0.66);
  box-shadow: 0px 0px 6px 2px rgba(0, 0, 0, 0.66);
}

.navgation-custom li:hover {
  background: #000;
}

@media screen and (max-width: 600px) {
  .menu {
    text-align: right;
    flex: 1;
    margin: 30px;
  }
}

.menu a {
  color: black;
}

#dropdownMenuLink {
  color: white;
}

ul.dropdown-menu.show {
  display: block !important;
}

ul.dropdown-menu.show a:hover {
  background-color: #000;
  color: #fff;
}

ul.dropdown-menu.show li a {
  width: 100%;
}
<head>
  <title>@ViewData["Title"]</title>
  <link href="/css/site.css" rel="stylesheet" />
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6848989929592948796a6d3c8d6c8d4">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

  <link rel="icon" href="~/img/favicon.png" />

</head>

<body>
  <header>
    <nav class="navbar-custom">

      <div id="header">
        <div class="logo"><a asp-controller="Home" asp-action="Index">BOOK STORE</a></div>
        <nav class="menuFirst">
          <ul>
            <li><a asp-controller="Home" asp-action="Index">Home</a></li>
            <li><a href="#">Store</a></li>
          </ul>
        </nav>

        <nav class="menu" role="navigation">

          <div class="dropdown">
            <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">
            </a>
            <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
              <li><a class="dropdown-item" href="#">Action</a></li>

              <li><a class="dropdown-item" href="#">Something else here</a></li>
            </ul>
          </div>

        </nav>
      </div>
    </nav>
  </header>
  
  <main></main>
  <footer></footer>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14777b667154263a2d3a26">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a6865657e797e786b7a4a3f243a2438">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
  </script>
</body>

Answer №1

To achieve this, simply include the following code in your CSS for ul.dropdown-menu li: width: 100%;. I have added the necessary lines in the snippet below.

ul.dropdown-menu li {
  width: 100%;
}
/* Additional CSS styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

header {
  position: sticky;
  top: 0;
}
/* More CSS properties */

<ul dropdownmenu = show {
display block ! important;
}
CSS code completed!
<Head>
  <title>@ViewData["Title"]</title>
  <link href="/css/site.css" rel="stylesheet"/>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

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

Preventing users from inputting the same number more than once

Here are a series of text input fields pertaining to a single question: <input type="text" name="{{ $answer->id }}" value="" style="width:20px; text-align:center" maxlength="1" oninput="this.value=this.value.replace(/[^0-5]/g,'');" /> & ...

What causes the disparity in functionality between CSS's Left 75% and Right 25% properties?

Here is my CSS code snippet: .bloodparam > .highvalue { bottom: 12px; right: 25%; } and .bloodparam > .highvalue { bottom: 12px; left: 75%; } I expected the element to be in the same position with both lines of code, but I'm noticing differe ...

Achieving tabbed code blocks in Gatsby and Asciidoctor.js: A simple guide

As I work on developing a documentation website using Gatsby and Asciidoctor.js, I am in need of displaying code samples for multiple languages in tabbed code blocks. For example, I want to include the same code for both JAVA and Kotlin. Although I have ...

CSS: Trouble with getting SVG sprite to work

I am struggling to combine all my SVG icons into one sprite and then call it from CSS as a background-image, similar to how we do with other images. However, the icons are not showing up on the page. When I open the SVG image in my browser, I encounter thi ...

Can Redux enable real-time data synchronization across various devices?

In an office setting, there are two computer systems running a shared web application. One system is assigned to the employee while the other is for the admin. When the employee logs in, they see their schedule on their dashboard. Conversely, when the admi ...

Highlighting table rows on mouseover using CSS

I am trying to add a hover effect to my table rows, but when I apply the CSS styles for this behavior, the rows start wrapping. Here's a comparison of how the table rows look before and after applying the styles. You can see the wrapping issue in the ...

Order a portion of a JSON array according to another part of the same array

Having a json array that needs sorting in JavaScript. The EventName field should match the respective Age fields like 01-10 Days and 10-20 Days. [ {Age: "01-10 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []} ,{Age: "01-10 Days", Even ...

HTML form for selecting shipping method in PayPal

In my shopping cart setup, I offer two shipping methods: 'express' and 'standard'. I'm wondering if it's feasible to transmit this data to PayPal using HTML variables? ...

The JQuery Datepicker function consistently returns a null value when attempting to retrieve the selected date

I've encountered a challenge while working on a project recently. Currently, I'm utilizing Spring 3.0 with its integrated validation alongside Hibernate 3.6. My goal is to implement a JQuery datepicker for users to input dates. However, the dat ...

Tips for prioritizing new data input at the top of the list

Hey there, I'm having trouble figuring out how to push new data to the top of a list using vue.js and laravel. I've been trying but haven't had any luck so far. If anyone could lend a hand, I would greatly appreciate it. Below is my Control ...

Issue with Ng-style not functioning properly when setting background color

I am struggling to set the background of an element using a configuration object with ng-style. For some unknown reason, I can't seem to make it work and I'm finding it really perplexing. The element I'm attempting to configure: <div id ...

The value of the variable remains consistent throughout the .each function when using JQuery's .post() method

I am facing a dilemma with a variable value discrepancy within the $.post function compared to the parent function $(element).each(function. Below is the snippet of my code: $(document).ready(function() { $(".list").each(function() { var a = $(thi ...

Add an HTML template tag in Vuetify using scripting

This is my current template tag: <template> <h1>Explore in VR</h1> </template> Within my script tag, I've included the following code: var viewer = new AFrame.WebVRViewer(); viewer.setSize(window.innerWidth, window.innerHeig ...

What is preventing me from stopping the setInterval using window.clearInterval?

Below is the code I have written using window.setInterval and window.clearInterval. The window.setInterval function works fine, but when I attempt to call window.clearInterval, I encounter an error stating that intervalId is not defined. Is there a way t ...

Is it possible to use combox to deactivate the buttons?

My goal is to deactivate the button as soon as I select a value from the drop-down list. For example, here's what I have: <select name =""> <option> disable </option> <option> enable </option> </select> <input ...

Preserving the <script> tag when defining HTML code

During an AJAX call, I am receiving plain HTML with some JavaScript code. When I try to display this response in a container using .html (jQuery) or innerHTML (plain JavaScript), it removes the <script> tag and all JavaScript code. However, when I c ...

Using AJAX to refresh and display content with jQuery

Seeking advice on dynamically refreshing and updating the content of a div that pulls information from an AJAX call. Currently, the div is cleared before new data is added upon click. I am looking for a way to achieve this update without relying on a click ...

Leveraging a JavaScript variable declared outside the function to successfully transfer data to my AJAX function

Every time the enter key is pressed, my AJAX function gets executed. I used to pass a set of javascript variables equal to elements in the HTML (the contents of a text area) as data for the AJAX function. Now, I want these JS variables to hold values from ...

Express.js throws an error when trying to access req.session when it

I've searched through multiple posts on this topic, however, none were able to resolve my issue. Below is the code snippet from my server.js file: var express = require('express'); var app = express(); app.configure(function(){ app.set ...

Building an Angular 4 app featuring a customized default landing page and URL parameters functionality

I am in the process of developing a web portal that will be embedded within an iFrame. Below is the code snippet I am using to set up the portal: Routes Configuration const routes: Routes = [ { path: '', redirectTo: '/dash ...