`Inconsistencies between navbar hide and show toggling on smaller and bigger screens`

I've been experimenting with making my navbar collapsible using Flexbox. Initially, I was able to toggle the navbar correctly when the screen size is smaller. However, I encountered an issue where the navigation items do not reappear when I hide them on a smaller screen and then expand the screen size. Additionally, if I leave the navigation items open on a smaller screen and then enlarge the screen, the flex direction of the nav items switches to column orientation, which is not desired on larger screens.

Below is the snippet of my code:

$(".toggle-btn").click(function() {
  $(".nav-toggle").slideToggle("slow");
});
/* header */

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2em;
  background-color: #FFB600;
  box-shadow: 0 1px 20px #48493e;
  z-index: 1;
}

header ul {
  display: flex;
}

/*slide menu*/

.nav-btn {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  margin: 3.5em 4em 0 0;
}

.toggle-btn {
  position: relative;
  padding: 0;
  width: 4em;
  height: 2em;
  cursor: pointer;
  outline: none;
  font-size: 1.2em;
  font-family: 'Dosis', sans-serif;
  font-weight: bold;
  box-shadow: 5px 5px 5px 1px #000;
  text-transform: uppercase;
  border-radius: 4px;
  border: solid 2px #000;
  background-color: transparent;
}

.toglge-btn:active {
  box-shadow: 3px 3px 5px 1px #000;
  top: 2px;
}

@media all and (max-width: 820px) {
  header {
    flex-direction: column;
  }
  .nav-brand {
    align-self: flex-start;
  }
  .nav-btn {
    display: block;
  }
  header ul {
    flex-direction: column;
    text-align: center;
    display: none;
  }
  .nav-items {
    line-height: 2;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
  <h2 class="nav-brand"><a href="/"> Tequila Grill</a></h2>
  <div class="nav-btn">
    <button class="toggle-btn">Menu</button>
  </div>
  <ul class="nav-toggle">
    <li><a class="nav-items" href="/">Home</a></li>
    <li><a class="nav-items" href="/menu">Menu</a></li>
    <li><a class="nav-items" href="/hours">Hours</a></li>
    <li><a class="nav-items" href="/location">Location</a></li>
    <li><a class="nav-items" href="/catering">Catering</a></li>
  </ul>
</header>

Answer №1

Hey there buddy

@media all and (min-width: 821px) {
  ul.nav-toggle[style] {
    display:flex!important;
  }
  }

By using this code, you are instructing it to apply the style display:flex; to all view-port sizes that are 821px or larger. The [style] selector allows you to add CSS inline similar to how you would with JavaScript.

Check it out here

Answer №2

function adjustSize() {
  $(".toggle-btn").click(function() {
    $(".nav-toggle").slideToggle("slow");
  });
}
adjustSize();

$(window).on('resize', function() {
  adjustSize();
});

This code snippet will properly function.

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

I have implemented a validation system that prevents me from advancing to the next tab on my screen if it is left empty

<?php echo $this->Html->script('balaashjquery'); ?> <script type="text/javascript"> jQuery(function(){ $('#name').blur(function(){ $nameval = $('#name').val(); if($nameval ==""){ ...

Combine object attributes to create a new column in a table

Check out this plunker I created. What is the most efficient way to merge the contents of $scope.blacklist into $scope.friends when ng-click="showColumn('Blacklist');" is triggered, and also add a new column named Coming to the table. Ng-App &am ...

Tips for concealing the navigation bar during page printing in Bootstrap 5

When attempting to print the page body, I am encountering an issue where the vertical "navbar" continues to display and covers a portion of the content. Below is the code I am using to hide the navbar when printing: @media print and (min-width: 800px){ ...

Learn the steps to Toggle Javascript on window resize

Is there a way to toggle Javascript on and off when the window is resized? Currently, resizing the window causes the navigation bar to stick and remain visible above the content. <script> if ( $(window).width() <= 1200 ) { }else{ $('nav& ...

Creating a unique CSS style sheet tailored specifically for Internet Explorer and Microsoft Edge – here's how!

Struggling with internet explorer acting up and causing glitches on my website? Check out my problem here. The Issue: An interactive image on my website is not functioning properly in IE & Edge. The image should expand when hovered over, but glitches ...

Jquery adds a fun, wobbly effect to animations

I'm experiencing an issue with the animation I've implemented causing some slight shaking and wobbling of the text and certain elements which is affecting the overall look. You can view a live example of this behavior here: This problem specific ...

``Maintain user engagement with a dynamic menu by utilizing knockout for view switching and CSS

I'm facing a challenge with my single-page application developed using knockout/jquery. The issue lies in handling which view to display as the number of menu items increases, making the code more difficult to manage. I'm looking for a solution t ...

jQuery UI validation causing submission of a form with invalid data

Currently, I am troubleshooting an issue with a form that utilizes Ajax to submit data to the backend. For validation, I'm using the jQuery-ui validation plugin. The problem I am encountering is that the form submits even when fields are left empty. ...

Having trouble getting jQuery to function properly in Safari

I am facing an issue with jQuery in the Safari browser. While my jQuery functions are functioning perfectly on GC, FF, IE, and Opera, they seem to fail when tested on Safari. My question is: Is there a specific code snippet that I can integrate into my w ...

The datepicker fails to show the selected date in the text box following an ajax request

I have a div with the ID of inPatientDecision. The contents of this div are loaded dynamically through an Ajax call. Within the loaded content, there are HTML datepicker elements with the class attribute set to datePicker. In order to initialize the jQuery ...

"Reconfigure web page layout to be perfectly centered

Currently, I am utilizing Angular Drywall to construct my website. By default, the alignment is centered which does not suit the template requirements that call for it to occupy the full 100% body width. body{ width:100%; margin-left:0; paddin ...

How to remove an item with a click using jQuery and PHP?

I have a question about deleting an item I just clicked on. I'm having trouble tracking it and resorting to drastic measures (like posting here) to delete everything in the list. Code Snippet: <div class="image-list"> <?php $count = 1; if ( ...

Utilize the serializeArray method in AJAX to send form data, while also implementing validation for each input text field as the user

I'm looking to validate text fields and type="file" input fields individually using the onblur and onchange events. The code I am working with involves AJAX, jQuery, and PHP. Any assistance in resolving this issue would be greatly appreciated. Here i ...

What is the procedure for submitting all checkbox values through Google Apps Script?

My web app created using Google Apps Script can generate a custom table of data for users to select for calculations. I use Google Sheets to populate the table with values and checkboxes, but note that the table size may vary depending on the user. <fo ...

Could multiple values be stored in the option tag in Laravel after selection?

I have encountered an issue where I am able to display all the attribute drop-down options and obtain the attribute id in the value="{{$play->id}}" after selection, which is then stored in the select with name="attribute_id". This allows me to easily fe ...

What is the best way to create a dropdown menu within an iframe that appears on top of all other frames?

The code snippet below is from my frameset.jsp file: </head> <iframe width="100%" src="mail_frame.html"></iframe> <iframe width="105px" height="100%" src="sidenav.html" id="leftnav" name="leftnav" ></iframe> ...

How can I replicate the Firefox style in IE8 using Bootstrap and CSS?

My webpage is styled with Bootstrap v2 and looks great in Firefox. However, I'm struggling to apply the same CSS color style in IE8: .navbar-inverse .navbar-inner { background-color: #1b1b1b; background-image: -moz-linear-gradient(top, #fab ...

Prevent selection of future dates in JavaScript by using the user's chosen StartDate

Here is the code I currently have: var today = new Date().toISOString().split('T')[0]; document.getElementsByName("StartDate")[0].setAttribute('min', today); $('#StartDate').change(function myfunction() { var endDate = ...

The styled components can create identical CSS classes with varying conditions

Below are the CSS styles I am currently using: import styled, { css } from "styled-components"; const H4 = css` font-family: "Futura"; font-style: normal; font-weight: 500; font-size: 20px; line-height: 140%; color: #3a786a ...

How can I make a single <input> element that can handle both files and urls simultaneously?

Is there a way to enable my users to import both local and remote files using just one input field? A possible solution, inspired by Stackoverflow, is to implement tabs: Another approach could be using radio buttons like this: <style> .display ...