Issues with Adaptive Headers

* {
  box-sizing: border-box;
  font-family: sans-serif;
  margin: 0;
  padding: 0;
}

html,
body {
  font-size: 14px;
}

header {
  width: 100vw;
  height: 50px;
  background-color: #222;
  margin: 0 auto;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}


/* Container for the Left and Right nav sections*/

header>div {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1140px;
  width: 100%;
  padding: 0 20px;
}


/* adjust width to set size */

nav {
  width: 70%;
  display: inline-flex;
  justify-content: space-between;
}

nav ul {
  list-style-type: none;
  display: flex;
  padding: 0 20px;
}

nav a {
  padding: .8rem 1rem;
}

header>div>a {
  font-size: 18px;
  border-bottom:
}

.a-tag-header {
  text-decoration: none;
  color: #999;
  display: block;
}

nav a:hover {
  color: #fff;
}


/* -- Menu Toggle -- */

.MenuToggle {
  display: none;
}

@media screen and (max-width: 890px) {
  /* Header */
  header {
    height: auto;
  }
  header>div {
    flex-direction: column;
    padding: 0;
  }
  nav {
    display: flex;
    width: 100%;
    flex-direction: column;
  }
  nav ul {
    flex-direction: column;
    text-align: center;
    align-items: center;
    padding: 0;
  }
  nav ul li {
    height: 50px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  nav ul li:hover {
    background-color: aqua;
  }
  header>div>a {
    padding: .8rem 0;
  }
  header>div>a:hover {
    background-image: #eee;
  }
  /* -- Menu Toggle -- */
  .MenuToggle {
    display: inline-block;
    color: #F0F0F0;
    font-size: 22px;
    cursor: pointer;
    position: absolute;
  }
}
<header>
  <div>
    <a class="a-tag-header">Chemical Finger Print Analysis</a>
    <div class="MenuToggle">
      <span id="MenuToggleButton" onclick="NavToggle()">&#9776;</span>
    </div>
    <nav>
      <ul>
        <li><a href="#" class="a-tag-header">Home</a></li>
        <li><a href="#" class="a-tag-header">About</a></li>
        <li><a href="#" class="a-tag-header">Contact</a></li>
        <li><a href="#" class="a-tag-header">Data</a></li>
        <li><a href="#" class="a-tag-header">Reports</a></li>
      </ul>
      <ul>
        <li><a href="#" class="a-tag-header">Register</a></li>
        <li><a href="#" class="a-tag-header">Login</a></li>
      </ul>
    </nav>

    <div class="MenuToggle">
      <span id="MenuToggleButton" onclick="NavToggle()">&#9776;</span>
    </div>
  </div>
</header>

Hey there, I'm in the process of building a flexible navigation bar using Flexbox approach with a desktop-first design. The navigation bar consists of a title and a menu toggle along with two separate anchor tags. While I've managed to make the navigation responsive, I'm facing a challenge making the title and menu toggle share the top container responsively when the screen size is smaller than 850px. I've experimented with several solutions but haven't been successful. Any suggestions would be greatly appreciated. Thanks!

Answer №1

Check out this solution

function toggleMenu() {
    var menu = document.getElementById("myTopnav");
    if (menu.className === "topnav") {
        menu.className += " responsive";
    } else {
        menu.className = "topnav";
    }
}
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="toggleMenu()">&#9776;</a>
</div>




</body>
</html>

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

Using a web browser control to choose a radio button and then submit the form

Currently, I am attempting to use my WebBrowser control in order to select a radio button and also click on a submit button that doesn't have any id or name attached. Below is the snippet of HTML code from the webpage: <form method="post> & ...

I am experiencing an issue where my application, built using NodeJS, Express, and Javascript, is failing to insert form data into the database despite

I am facing a challenge with my app's MVC design pattern where I am unable to insert form information into the corresponding table in the database. Despite installing the body-parser package via npm, the form is not functioning as expected. Can anyone ...

Conceal the prominent image when viewing a single post

I've been attempting to hide the featured image on single post view but so far, neither a plugin nor the theme panel option has worked for me. I even tried adding custom CSS code without success. Is there another method that can be used to hide the fe ...

Synchronize two div elements with JavaScript

The demonstration features two parent divs, each containing a child div! The first parent div's child div is draggable and resizable using JQueryUI. There are events for both dragEnd and resizeEnd associated with this div. The goal is to synchronize ...

To access the link, simply click once if there is no child menu. However, if there is a child menu attached, be sure to click

I am working on a mobile menu that is designed to slide out when clicked. Currently, the parent pages are displayed by default. I want to implement functionality where if a parent page has child pages, clicking on it will slide down the sub menu. If click ...

Selenium Webdriver is retrieving background colors with unexpected alpha values

When I need to retrieve the background-color of a WebElement, I typically use the following code: string color = IWebElement.GetCssValue("background-color"); Selenium often returns something like this for the color value: color = "rgba(153, 255, 255, 1) ...

Incorporating .txt files into a static webpage using only HTML and JavaScript

Exploring the realm of web page creation for the first time has left me in need of some guidance. I have successfully added an upload feature for text files on my web page, but I am struggling to embed a text file directly into the page source. With Java s ...

Utilize AJAX to showcase JSON data retrieved from a specified URL

We are striving to showcase the names listed in our JSON file within a div using JavaScript. Despite multiple attempts, we have not yet achieved success. JSON data: This is the approach we took: <button>fetch data</button> <div id="result ...

Display <div> exclusively when in @media print mode or when the user presses Ctrl+P

Looking for a way to create an HTML division using the div element that is only visible when the print function is used (Ctrl+P) and not visible in the regular page view. Unfortunately, I attempted the following method without success. Any advice or solut ...

CSS3 Perspective Issue: Solutions for Fixing

I'm currently working on creating a card flip animation that flips in the X axis direction. Right now, the div rotates using the rotateX() method. I attempted to add the perspective property to the upper div, but it ended up distorting my div structur ...

Using iScroll without animation by using scrolltoelement on an iPhone PhoneGap application

I've been working on an iOS PhoneGap app that uses Backbone and some other libraries. To handle scrolling, I implemented iScroll which has been functioning properly. However, I encountered a problem with the following code snippet: events: { &apo ...

Identifying selected shapes in CSS/Javascript by outlining them with small rectangles placed on each corner

Currently, I am working on a University Project that involves creating a selection function for drawn shapes such as rectangles, circles, lines, or triangles. The challenge lies in figuring out the visualization of the selection when a shape is clicked. T ...

jQuery's click event on dynamically generated AJAX elements sometimes fails to trigger randomly

When I use the getData() function to retrieve ajax content, it dynamically adds elements based on the JSON response from the server. The code snippet below shows how the elements are added: $("#list").append('<div class="ui card fluid" id="' ...

Tips for implementing a default font on a website

I've incorporated a unique font into a specific section of my website design, but I'm aware that this particular font may not be available on most of my users' computers. Is there a method to apply this font to selected text without resortin ...

Can you suggest the best HTML5 structure for a multi-chapter novel?

In the process of creating a small HTML5 book with chapters and sections, I encountered a dilemma when it comes to structuring my documents like this: chapter1.html - introduction to chapter 1 chapter1section1.html - section 1.1 chapter1section2.ht ...

Aligning content within a div using Bootstrap 4

For a demonstration of this code, please visit the following Codepen link: codepen <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstr ...

Aligning a Material UI button to the far right of a row

I am struggling to align a React material-ui button to the right-most of the page and despite trying to use the justify="flex-end" attribute within the following code, it doesn't seem to be working: <Grid item justify="flex-end" ...

Locating the value of a data attribute from a distant parent div using jQuery

How can I access the closest div from an anchor tag that has a data-id attribute without using multiple parent() methods? Even though .parent().parent().parent() works, I am trying to find a better solution. <div class="panel panel-default" data-id="@ ...

Is it Possible to Stack Multiple Rows of Images with HTML5/CSS3?

I am struggling to arrange images in rows, but I'm facing unexpected outcomes. The images only break to a new line when the page is filled. I attempted to use overflow:hidden, however, the expanding images end up hidden under white space. I can't ...

What makes Django forms the better choice over HTML forms?

Greetings! As a beginner in the world of Django, I am currently struggling to grasp the concept of Django forms. I find myself questioning the utility of Django forms when we already have the option to use HTML forms. Can someone please clarify this for ...