Expanding the Background Color when Hovered Over

Despite my extensive search on SO, none of the answers I found seem to address my specific situation.

In a col-md-2 section where I have a navigation bar, I am trying to change the background color to extend up to the border when hovering. While I can change the color, it doesn't quite reach the border as desired.

I've put together a sample fiddle to demonstrate this issue.

If the fiddle is not accessible, here's the code snippet:

<div class="col-md-2">
    <ul>
        <li><a href="#">dfsdf</a></li>
        <li><a href="#">sdgfdgdfgdf</a></li>
        <li><a href="#">sdfdsfsd</a></li>
    </ul>
</div>
<div class="col-md-10">
    cycdc
</div>

CSS Styles:

.col-md-2{
    background-color:red;    
}
.col-md-2>ul>li>a:hover{
    background-color:yellow;
}

Update:

I realized that adding display:block in the a tag may solve the issue. However, implementing this solution in my code didn't work as expected. Here is the CSS I am currently using:

.col-md-2 .navbar-nav{
    background-color:ghostwhite;
}
.col-md-2 .navbar-nav >li{
    text-align: center;   
    display:block;
}
.col-md-2 .navbar-nav>ul>li>a{
    color:#255C89;
    display:block;
}
.col-md-2 .navbar-nav >li>a:hover{
    background-color:#255C89;
    color:white;
}

HTML Snippet:

<div class="col-md-2">
    <ul class="nav navbar-nav">
        <li>@Html.ActionLink("View System Families", "Index", "LsystemFamilies")</li>
        <li>@Html.ActionLink("Add New System Family", "Create", "Lsystemfamilies")</li>
        <li>@Html.ActionLink("View all Systems", "Index", "Lsystems")</li>
    </ul>
</div>

Answer №1

Check out this link for a solution

To solve the problem, make sure to include display: block in the a tag

Update:

Make sure your code selector is as follows:

.col-md-2 .navbar-nav > li > a{
    display: block
}

Second update:

.col-md-2 .navbar-nav {
    background-color: ghostwhite;
}
.col-md-2 .navbar-nav>li {
    text-align: center;   
    display: block;
    float: none;
}
.col-md-2 .navbar-nav>li>a {
    color: #255C89;
    display: block;
    float: none;
}
.col-md-2 .navbar-nav>li>a:hover {
    background-color: #255C89;
    color: white;
}

Answer №2

Here is an example that will also function correctly:

.col-md-2{
    background-color:red;
    
}
.col-md-2>ul>li:hover{
    background-color:yellow;
}
<div class="col-md-2">
    <ul>
        <li><a href="#">dfsdf</a></li>
        <li><a href="#">sdgfdgdfgdf</a></li>
        <li><a href="#">sdfdsfsd</a></li>
    </ul>
</div>
<div class="col-md-10">
    cycdc
</div>

Answer №3

If you want to make the entire area clickable on hover, simply add display: block; to the a tag in your CSS. This will allow users to hover over the element and achieve your desired effect:

.col-md-2 {
    background-color: red;
}
.col-md-2 > ul > li > a {
    display: block;
}
.col-md-2 > ul > li > a:hover {
    background-color: yellow;
}

Answer №4

There are two ways to accomplish this:

Method 1:

Hover over li instead of a

.col-md-2{
    background-color:red;
    
}
.col-md-2>ul>li:hover{
    background-color:yellow;
}
<div class="col-md-2">
    <ul>
        <li><a href="#">dfsdf</a></li>
        <li><a href="#">sdgfdgdfgdf</a></li>
        <li><a href="#">sdfdsfsd</a></li>
    </ul>
</div>

Method 2:

Set display:block for a as it is by default set to display:inline

.col-md-2 {
  background-color: red;
}
.col-md-2>ul>li a:hover {
  background-color: yellow;
}
a {
  display: block
}
<div class="col-md-2">
  <ul>
    <li><a href="#">dfsdf</a>
    </li>
    <li><a href="#">sdgfdgdfgdf</a>
    </li>
    <li><a href="#">sdfdsfsd</a>
    </li>
  </ul>
</div>

Your feedback has been considered, and the code has been updated accordingly:

Method 1:

Hover over li instead of a

.col-md-2 .navbar-nav {
  background-color: ghostwhite;
  /*remove margin/padding from ul default if not already */
  margin: 0;
  padding: 0;
}
.col-md-2 .navbar-nav li {
  text-align: center;
  display: block;
}
.col-md-2 .navbar-nav>ul>li>a {
  color: #255C89;
  display: block;
}
.col-md-2 .navbar-nav>li>:hover {
  background-color: #255C89;
  color: white;
  display: block;
}
<div class="col-md-2">
  <ul class="navbar-nav">
    <li><a href="#">dfsdf</a>
    </li>
    <li><a href="#">sdgfdgdfgdf</a>
    </li>
    <li><a href="#">sdfdsfsd</a>
    </li>
  </ul>
</div>

Method 2:

Set display:block for a because by default it is set to display:inline

.col-md-2 .navbar-nav {
  background-color: ghostwhite;
  /*remove margin/padding from ul default if not already */
  margin: 0;
  padding: 0;
}
.col-md-2 .navbar-nav li {
  text-align: center;
  display: block;
}
.col-md-2 .navbar-nav>ul>li>a {
  color: #255C89;
  display: block;
}
.col-md-2 .navbar-nav>li>a:hover {
  background-color: #255C89;
  color: white;
  display: block;
}
<div class="col-md-2">
  <ul class="navbar-nav">
    <li><a href="#">dfsdf</a>
    </li>
    <li><a href="#">sdgfdgdfgdf</a>
    </li>
    <li><a href="#">sdfdsfsd</a>
    </li>
  </ul>
</div>

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

What is the method for including HTML special characters in d3.js?

I am attempting to incorporate the degree symbol in my HTML code using ° const degreeNum = d3 .select(`#windBox${order}`) .append("text") .attr("x", 250) .attr("y", 130) .style("font", "bold 50px sans-serif") ...

Issue with FusionCharts rendering correctly arises when the <base> tag is present in the HTML head section

Combining AngularJS and FusionCharts in my web application has led to a unique issue. The upcoming release of AngularJS v1.3.0 will require the presence of a <base> tag in the HTML head to resolve all relative links, regardless of the app's loca ...

How to incorporate text into the white circle object using three.js

View the current state of my project on this JS Fiddle. I am looking to incorporate rotating text, whether in 3D or 2D. The text should rotate in sync with the white circle. I am open to any method that achieves the desired outcome. Below is the provided c ...

Refreshing the parent page in Oracle Apex upon closing the child window.When the child window

I have created an interactive report with a form where the interactive report is on page 2 as the parent page and the form page is on page 3 as the child page. In the parent page, I have written JavaScript to open a modal window (form page - page 3) using ...

Dimensions of images on Read the Docs (small image widths)

Using MkDocs along with the Read the Docs theme, I am facing an issue with small images not scaling properly on mobile devices. It seems like this problem is related to the CSS of Read the Docs theme, but I'm unsure how to override the default behavi ...

Verify if the value of localStorage matches the specified value, then conceal the element

This is my second query and I'm hoping it covers everything. My knowledge of javascript is limited, which has made it difficult for me to get my code working properly. Despite trying various different approaches, I have been unable to resolve the issu ...

What is the best way to connect to a specific part of a webpage that functions properly on Safari?

On my website, I have a testimonials page with a featured testimonial on the homepage that has a "Read More" option. When users click on this link, I want them to be taken directly to the testimonials page and to the specific section where that particular ...

Component rendering based on conditions is not working properly when using Next.js, especially on the initial load

While utilizing Ant Design and a Custom Stylesheet, I have encountered an issue where the style appears broken upon first load. However, if I navigate to another page and return to the original broken page, it displays correctly. This problem only occurs d ...

Troubleshooting Issue with Absolute Positioning and Hover Effect on Material UI Button

I have a div containing a hidden button and an inner div filled with graphs and text. Occasionally, I need to blur the inner div and make the button float on top in the center of the blurred section, similar to the layout seen on subscription prompts found ...

What is the optimal choice: Using Stack with direction="horizontal" or employing display flex in React Bootstrap?

In my opinion, the two functions may seem similar, but from personal experience I tend to favor using display flex over Stack in react boostrap. I find that display flex offers more flexibility when it comes to spacing out objects. Would anyone be able to ...

Is there a way to eliminate the 'All Files' option from an HTML file input field?

I have implemented a feature to allow users to upload custom files using . Currently, I am restricting the allowed file types to only ".Txt, .SVG, .BMP, .JPEG" by using accept=".Txt,.SVG,.BMP,.JPEG". This setting causes the browser's file-select dial ...

"Utilizing the power of Angular 6's JSON pipe

Looking for a well-structured formatted JSON, but all I get is confusion and this strange image: Does anyone have any insights on what might be causing the issue? HTML <span style="font-weight: 500;">Payload Data: </span> <pre><co ...

What is the predefined value for a multi-select generated by the ng-for directive in Angular?

I am having trouble setting default selected values for the multi-select. Despite trying various methods such as initializing the ngModel to bind the variable and using [selected] = "selectedSegment == 'S1'", none of them seem to be effective fo ...

Tips for adjusting font size automatically to fit within its parent container every time

On my blog, the post titles are displayed in a div with a video playing in the background. The length of the title varies, ranging from 2 words to over 20 words. When the title is too long, it may overflow from its parent container where the video is playi ...

What sets apart +variable+ and ${variable} in JavaScript?

Just starting to code and eager to learn. While I was taking in a lecture, I came across this interesting snippet of code: var coworkers = ['go', 'hello', 'hi', 'doit']; <script type="text/javascript&q ...

Is it possible to align an entire column to the right in the Material UI Data Grid?

I'm currently exploring Material UI and grappling with a specific challenge. Is there a method within Material UI to create a Data Grid with two columns, one left-aligned and the other right-aligned? I've managed to align the headers as desired, ...

Vue application experiencing issues with applying Sweet Alert CSS styles

I successfully integrated vue-sweetalert2 into my Vue app (version 2.6). The setup in my main.js file looks like this: import VueSweetalert2 from 'vue-sweetalert2'; Vue.use(VueSweetalert2); In one of my components, I have a basic button with a ...

Tips for waiting for a page to fully load before clicking the "load more" button using selenium

When using Selenium to load a page and attempting to click the "load more" button, encountering an issue after clicking it more than 100 times. An error of "element click intercepted" is being displayed, as the page takes longer to load after multiple cli ...

Aligning Content in the Header

Currently, I am attempting to place a logo on my WordPress site right at the top of the header above the menus and in the center. Even though I've positioned it at the top, I'm struggling to align it horizontally in the center. I've experime ...

Adjust the width of a textarea dynamically as you type by detecting keyup events

Two text areas have the same text formatting. Their IDs are textareaA and textareaB. I've successfully added functionality to resize textareaA on keyup. How can I make textareaB resize its WIDTH while the user is typing in textareaA? Here's wha ...