When hovering over a flex container, the list items inside it will shift to

Currently, I am utilizing bootstrap 4 and font awesome icons in my project.

I have been working on developing a side navbar with animation. However, I am facing an issue where whenever I hover over a ul item, all of the items below it shift up and hide underneath the item that is being hovered.

Is there a way for me to prevent this behavior?

<style type="text/css">
        .sidenav .container {
            position: fixed;
            overflow-x: visible;
            max-width: 50px;
            top: 15%;
            max-height: 70%;
            justify-content: left;
            padding-left: 0;
            z-index: 1;
        }

        .nav-item .btn {
            color: lightgray;
            width: 100%;
            background-color: #343a40;
            border-bottom: #0c0c0c solid;
            border-radius: 0 10px 10px 0;
            transition: color 400ms, width 400ms;
        }

        .sidenav .btn:focus {
            box-shadow: none !important;
        }

        .nav-item:hover > .btn:hover {
            background-color: lightgray;
            color: #343a40;
            width: 75px;
            position: absolute;
        }
</style>
<div class="sidenav">
        <div class="container">
            <ul class="nav flex-column">
                <li class="nav-item">
                    <button class="btn" data-toggle="modal" data-target="#notebookModal"><i
                            class="fas fa-clipboard-list fa-2x"></i></button>
                </li>
                <li class="nav-item">
                    <button class="btn" data-toggle="modal" data-target="#addNoteModal"><i
                            class="fas fa-plus-circle fa-2x"></i></button>
                </li>
            </ul>
        </div>
    </div>

Answer №1

To stop them from shifting upward, eliminate the position: absolute from the hovered class:

.nav-item > .btn:hover {
    background-color: lightgray;
    color: #343a40;
    width: 75px;
}

UPDATE:

When hovering over one button, it impacts the width of the other buttons because they were all set to 100%. To resolve this issue, adjust the non-hover buttons to something different than 100% (as other non-hovered buttons become 100% wide during hover transition).

You can decide on the value, such as width: 55px;, or calculate it based on the bootstrap .btn padding, which is a left padding of .75rem upon inspection of the element. Since you assigned a width of 50px and left padding of 0 to the sidenav container, you can utilize calc() to determine what 100% would be if rem values change:

.nav-item > .btn {
  color: lightgray;
  width: calc(50px + .75rem);
  background-color: #343a40;
  border-bottom: #0c0c0c solid;
  border-radius: 0 10px 10px 0;
  transition: color 400ms, width 400ms;
}

Answer №2

Instead of applying the :hover selector only to .btn, consider setting it for .nav-item as well. Adjust your code like this:

.nav-item > .btn:hover {
        background-color: lightgray;
        color: #343a40;
        width: 75px;
        position: absolute;
    }

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

Could this be a glitch in the Safari CSS transition?

I've encountered a strange issue that seems to be a Safari CSS bug. The problem occurs when the element is being animated and it actually disappears during the CSS transition, only to reappear at its conclusion. Interestingly, this error only happens ...

Struggling to line up the header perfectly with the button

I am brand new to CSS and I have set up a header and a navbar. However, when attempting to position the button next to the header, it is appearing in the navbar line instead. I have tried using the float property, but the issue remains. Here is a snippet o ...

Tips for reordering div elements on mobile screens

Currently in the process of re-ordering these elements for mobile responsiveness. The current layout is as follows: DESKTOP: {image}{paragraph} MOBILE: {paragraph} {image} <div class="row" id="showcase"> <div class="col-md-6"> ...

Import CSS and JavaScript files from the Assets directory

After hours of searching for a solution, I've come to realize that my situation is somewhat unique. I have a website loaded into a webview in an Android app. setContentView(R.layout.activity_main); WebView myWebView = (WebView) findViewById(w ...

Using the address bar content to navigate to different HTML pages

I am interested in adding a feature to my website similar to what facebook has for its pages section. Essentially, I would like users to be able to access: www.mysite.com/school123 My plan is to use PHP to locate 'school123' in the URL, find th ...

Guidance on creating a custom color selection tool for a specific task

Looking for assistance in converting a code snippet that uses <button> elements to select colors into a color picker. I am unsure how to retrieve the selected color and use it within a JavaScript function. Can someone provide guidance on this? Here i ...

What are the pros and cons of embedding background image data into CSS as Base64?

While examining the source code of a greasemonkey userscript, I came across some interesting CSS: .even { background: #fff url(data:image/gif;base64,R0lGODlhBgASALMAAOfn5+rq6uvr6+zs7O7u7vHx8fPz8/b29vj4+P39/f///wAAAAAAAAAAAAAAAAAAACwAAAAABgASAAAIMAAVCBxIsK ...

Maximizing the potential of Bootstrap slider within OpenCart

Has anyone attempted to use Bootstrap slide with the Opencart slideshow module? Here is my code, but I am getting all active classes. Can someone please help me with this? <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"& ...

The Toggle Switch effectively removes the CSS class when set to false, but fails to reapply the class when set to true

Implementing a toggle switch using Bootstrap5 to control the visibility of grid lines. The setup includes adding a class to display grid lines when the toggle is true, and removing the class to hide the lines when the toggle is false. However, the issue ar ...

Step-by-step guide on adding a div inside another div:1. Start

Hey there! I'm currently in the process of building a website using PHP. One of the features on the site is a form for adding appointments. Users can input the date, start time, and end time of their appointment. Additionally, users have the ability t ...

Switch the angular attribute by clicking on ng-click

I have a Google Map set up with markers that I need to switch on and off. <marker id='{{marker.id}} 'visible="{{ condition ? 'true' : 'false'}}"> </marker> Additionally, I have created a button to control the v ...

Programmatically extracting a list of utilized CSS images from IE WebBrowser (IHTMLDocument2)

Exploring the IHTMLStyleSheetsCollection, IHTMLStyleSheet, IHTMLStyleSheetRulesCollection etc. of IHTMLDocument2 to compile a list of all styles in the current document is quite simple. Does anyone have any suggestions on how to generate a list of only th ...

The absolute positioned div on the same level is impacted by negative margin

I am facing an issue with styling elements in my HTML code. I have a div with the ID of #left that is absolutely positioned, along with two other divs on the right side. Strangely, when I apply margin to the #top div on the right side, it also affects the ...

Need help setting up a time-based query in Laravel? Here's how to schedule it!

I am currently using the following query to update a status value. public function updateStatus(Request $request) { $customer = Customer::findOrFail($request->user_id); $customer->status = $request->status; $customer->new_customer_s ...

Is there a component that functions as a dropdown menu? I've searched online but can't seem to

I have been working on creating a website that allows users to download PDF and MP3 files of sermons. However, I am facing challenges in implementing a dropdown menu for this functionality. Despite searching resources like Bootstrap and W3schools, I have ...

Develop a rule to make all non-null fields read-only in ASP.NET Core 6.0 MVC

As I embark on creating my very first web application using ASP.NET Core 6, I encounter a challenge with a form that displays fields pre-populated with data from my database. The issue at hand is that these fields must be readonly as the user should only ...

Width of flex container overflowing from relative position is at its full capacity

I'm attempting to create a horizontal line across an overflowing flex container by using an absolutely positioned child element with both the left and right set to 0. This requires the container to have a relative position to contain the absolutely po ...

HTML5 Video validation is not working properly

In my current project, I initially used embedded Youtube videos but faced issues with validation. Decided to explore HTML5 video elements as an alternative, but encountered some challenges. While attempting to validate the code, strange errors popped up: ...

"Angular.js makes it easy to drag and drop elements on your

After discovering this example on GitHub, specifically on this link, I noticed that the ReadMe was quite messy and the examples didn't work smoothly upon downloading the repository. Despite my attempts to make the simple example function properly, I h ...

The application of border-radius to a solitary side border forms a unique "crescent moon" shape rather than a traditional semi-circle

Is there a way to transform this shape into a regular semicircle, avoiding the appearance of a moon, and change the image into a circle rather than an ellipsis? http://jsfiddle.net/226tq1rb/1/ .image{ width:20%; border-radius:500%; border-rig ...