The animation for collapsing a flex-column navigation is appearing oddly strange

I'm currently working on creating a sidebar navigation using the Bootstrap 4 framework. The menu should be displayed at the top of the page on small devices.

Here is my approach:

.sidebar {
  background-image: linear-gradient(180deg, rgb(33, 243, 96) 0%, #077710 70%);
}

.sidebar .navbar-brand {
  font-size: 1.1rem;
}

.nav-item {
  font-size: 0.9rem;
  padding-bottom: 0.5rem;
}

.main {
  flex: 1;
}

.main .top-row {
  background-color: #e6e6e6;
  border-bottom: 1px solid #d6d5d5;
}

.nav-item:first-of-type {
  padding-top: 1rem;
}

.nav-item:last-of-type {
  padding-bottom: 1rem;
}

.nav-item a {
  color: #d7d7d7;
  border-radius: 4px;
  height: 3rem;
  display: flex;
  align-items: center;
  line-height: 3rem;
}

.nav-item a.active {
  background-color: rgba(255, 255, 255, 0.25);
  color: white;
}

.nav-item a:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
}

.content {
  padding-top: 1.1rem;
}

.navbar-toggler {
  background-color: rgba(255, 255, 255, 0.1);
}


/* @media (max-width: 767.98px) { */

.main .top-row {
  display: none;
}


/* Burger-icon animation */

.navbar-toggler {
  position: relative;
}

.navbar-toggler:focus,
.navbar-toggler:active {
  outline: 0;
}

.navbar-toggler span {
  display: block;
  background-color: #444;
  height: 3px;
  width: 25px;
  margin-top: 4px;
  margin-bottom: 4px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
...
<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</head>

<body>
  <contentwrapper>
    <div class="sidebar">
      <div class="top-row pl-4 navbar navbar-dark">
        <a class="navbar-brand" href="">MyTestPage</a>
        <button class="navbar-toggler navbar-toggler-right collapsed" data-toggle="collapse" data-target="#navbarContent" aria-expanded="false" type="button">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
      </div>
      <ul class="nav flex-column collapse" id="navbarContent">
        <li class="nav-item">
          <a class="nav-link active" href="#">
                        Home
                    </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">
                        Page1
                    </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">
                        Page2
                    </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">
                        Page3
                    </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">
                        Page4
                    </a>
        </li>
      </ul>
    </div>
    <div class="main">
      <f:render section="Main" />
    </div>
  </contentwrapper>
</body>

Please note that I have commented out the media queries in the CSS to ensure it displays correctly on mobile devices.

When clicking the hamburger icon to expand the menu, you may notice that initially, the menu items are displayed horizontally instead of vertically. After about half a second, they change to the correct layout.

If you know what mistake I made or have a workaround for this issue, please share your insights. Thank you in advance.

Answer №1

Bootstrap4 includes a handy .nav class with the following styling:

.nav {
  display: flex;
  flex-wrap: wrap;
  padding-left: 0;
  margin-bottom: 0;
  list-style: none;
}

In order to customize your navigation, you can override one rule specifically using flex-wrap:nowrap;

If you're concerned about potential conflicts elsewhere on your site, it might be wise to target just your navigation section like this:

#navbarContent.nav{
  flex-wrap:nowrap;
}

I've also removed the @media (max-width: 767.98px) { rule since it appears unnecessary given the mobile-first nature of your site.

Here's a snippet of the relevant code:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<style>
/* Custom CSS goes here */
#navbarContent.nav{
  flex-wrap:nowrap;
}

 @media (min-width: 768px) {
 /* Additional styles for larger screens */
 }
</style>

<contentwrapper>
    <div class="sidebar">
      <div class="top-row pl-4 navbar navbar-dark">
        <a class="navbar-brand" href="">MyTestPage</a>
        <button class="navbar-toggler navbar-toggler-right collapsed" data-toggle="collapse" data-target="#navbarContent" aria-expanded="false" type="button">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
      </div>
      <ul class="nav flex-column collapse" id="navbarContent">
        <li class="nav-item">
          <a class="nav-link active" href="#">
                        Home
                    </a>
        </li>
        <!-- More List Items -->
      </ul>
    </div>
    <div class="main">
      <f:render section="Main" />
    </div>
  </contentwrapper>

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

Deselect a radio button with a click event using jquery

When the page refreshes, radio buttons are left unchecked. <input type="radio" name="test"> 1<input type="radio" name="test">2 <input type="button" id="btn" /> $("#btn").click(function(){ $(':radio').each(function () { ...

Navigate to a different URL following a post request using AJAX and EXPRESS

Currently, I am diving into the world of node.js servers by creating a login website. Users can input their username, which is then sent via an ajax post request to the server and stored in an array of all users. My goal is to redirect users to a personali ...

Can you explain the function of the "Normalize CSS" feature in Fiddle?

I recently came across this interesting fiddle: Demonstration of rounded corners It achieves the desired outcome, however, I noticed that it only works properly when the "Normalised CSS" checkbox is selected. When it's not checked (like in my code), ...

Styling with CSS using the em unit to make an image half the size of its parent div element

I'm struggling with resizing an image within a div using the em size unit. I want the image to be half the size of its parent div, so I set both the width and height CSS properties of the image to 0.5em. However, when looking at the code below, you c ...

"Ajax has the ability to alter the vertical scroll position

I have a quick query for you. Can anyone assist me in understanding why the page scroll resets to the top after an Ajax request? I suspect it has something to do with JQuery. There isn't much information available online, so I'm reaching out for ...

Switching from a horizontal orientation to a vertical layout for navigation

My current horizontal navigation bar needs to be transformed into a vertical left layout. Here is the HTML code: <div ng-controller="PortalController"> <header> <h1>E_Zuite</h1> <nav role='navigation&apo ...

Something seems off with Chrome

I am facing a unique issue that I need help with. Unfortunately, the code is too long to post here but essentially it involves creating a menu for a website. The menu works perfectly and looks great on all browsers except for Chrome. This is unusual as Ch ...

PHP - Extract Information from Table Upon Form Submission without User Input

I'm facing a challenge with my web form that includes a table allowing users to delete rows before submitting. Although there are no input fields in the table, I need to capture the data from these rows when the form is submitted. The issue is that th ...

Is there a way for me to automatically update a webpage every 5 minutes, beginning at 8:01 a.m., and subsequently at 8:06 a.m., and so forth?

<html> <head>Harshal</head> <script> var limit="5:0" var doctitle = document.title var parselimit=limit.split(":") parselimit=parselimit[0]*60+parselimit[1]*1 function beginrefresh(){ if (parselimit==1) window.location.reload ...

Tips for securing my div in place when interacting with an expanding panel

I am attempting to create a visualization similar to this: https://i.sstatic.net/Wxdqr.png Instead of using the built-in card and card-column classes in Bootstrap, I noticed that when I expand a panel, all my <div>s start moving around haphazardly: ...

Node.js file successfully establishes a database connection, but script tags fail to do so

Having some trouble connecting to my MongoDB database (or MongoLab server). It works perfectly fine when the code is in the name.js file, but for some reason it fails when placed inside <script> tags within an HTML file. My folder contains only thes ...

Discovering an element within an HTML response using jQuery AJAX

Check out the code snippet below: ajax_obj = $.ajax( { data: data, success: function(answer) { console.log(answer); console.log($('#table_conferences_tbody', answer).html()); ...

Trigger a function upon browsing an image in angular-bootstrap-lightbox

Currently, I am utilizing angular-bootstrap-lightbox and have the need to implement a callback function that triggers when the image is altered. In particular, for the mobile view, when a user swipes left or right, I aim to capture both the image ID and t ...

Identifying Internet Explorer version through CSS functionality and feature detection

As of IE10, browser detection tags are no longer supported for identifying a browser. In order to detect IE10, I am utilizing JavaScript and a technique that tests for certain ms prefixed styles like msTouchAction and msWrapFlow. I would like to do the s ...

How can JavaScript be used to display the related value in another drop down list when a user selects an option in one drop down list?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="Practical_5.View.Registration" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset ...

The functionality of collapsing rows of cards in Bootstrap does not seem to work efficiently for multiple

There is a button that appears and the #more div is hidden. However, when I click "Show more," the hidden content does not appear. Could this be due to multiple rows? Should I add collapse to every card? I am using Bootstrap 4. Here is the code snippet: ...

"Interactive" - Utilizing javascript and html5 to create engaging game animations

Imagine I have a base class that looks like this: function Tile(obj) { //lots of default variables such as colors and opacity here } Tile.prototype.Draw = function () { ctx.fillStyle = "rgba(" + this.Red + "," + this.Green + "," + this.Blue + "," ...

Ensuring Proper Implementation of Tailwind CSS Styles on Next.js Image Component with Relative Positioning

Having some challenges while attempting to integrate the Next.js Image component into my React project alongside Tailwind CSS. Specifically, I'm facing issues with applying relative positioning and other styles like objectFit. It seems that the root c ...

Modify the CSS of CKEditor using JavaScript after the instance has been initialized

When creating a CKEditor instance, the following steps are typically taken: require_once 'ckeditor/ckeditor.php'; $CK = new CKeditor(); $CK->basePath = './ckeditor/'; $config = array(); echo '<textarea name="content" id="con ...

Create space in the bootstrap framework between an image and text

I'm trying to increase the space between the title/description and the icon image in my Bootstrap CSS layout. I attempted adding ms-5 to the div tag, but it didn't have the desired effect. NOTE: Please refrain from suggesting margin-right:5px as ...