Achieve full width navigation without compromising the website layout

Is there a way to make my navigation menu fill the entire page? I tried using absolute positioning and setting the top, left, bottom, and right values to 0, but it ended up overlapping the content and changing the page color. Here's the HTML/CSS code I used:

//JS
function toggleMenu() {
  var menu = document.getElementById("nav-links");
  if (menu.style.display === "block") {
    menu.style.display = "none";
  } else {
    menu.style.display = "block";
  }
}
/* CSS */

body {
  font-family: 'Roboto', Arial, sans-serif;
  background: white;
}

.topnav {
  overflow: hidden;
  background-color: #333;
  position: relative;
  width: 100%;
}

.topnav #nav-links {
  display: none;
}

.topnav a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;
}

.topnav a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
  top: 0;
}

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

.active {
  background-color: #b47b5a;
  color: white;
}

.body {
  display: flex;
  margin: auto;
  min-height: 300vh;
}
<!--HTML-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="css/styles.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <script src="script/server.js"></script>
  <title>Hello</title>
</head>

<body>
  <header>
    <div class="topnav">
      <a href="#" class="active">>Hello World!</a>
      <div id="nav-links">
        <a href="#">Home</a>
        <a href="#">Contact</a>
        <a href="#">About</a>
      </div>
      <a href="javascript:void(0);" class="icon" onclick="toggleMenu()">
        <i class="fa fa-bars"</i>
      </a>
    </div>
  </header>
  <div class="body">
    <h1>Hello World!</h1>
  </div>
  <script src="script/script.js"></script>
</body>

</html>

I have exhausted all my options and resources trying to solve this issue.

Answer №1

Adjusting the topnav position to absolute requires specific modifications for optimal results. This may entail tweaking the padding or margin of the content below the topnav or applying additional padding to the body element to prevent any undesirable overlaps.

//JS
function toggleNav() {
  var x = document.getElementById("nav-links");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}
/* CSS */

body {
  font-family: 'Roboto', Arial, sans-serif;
  background: white;
  padding-top: 50px;
}

.topnav {
  overflow: hidden;
  background-color: #333;
  position: absolute;
  z-index: 999;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
}

.topnav #nav-links {
  display: none;
}

.topnav a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;
}

.topnav a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
  top: 0;
}

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

.active {
  background-color: #b47b5a;
  color: white;
}

.body {
  display: flex;
  margin: auto;
  min-height: 300vh;
}

You may also consider adjusting the width of the topnav to 100vw to ensure it spans the entire width of your device. However, caution should be exercised as resetting the body's margin and padding to zero may lead to unexpected horizontal scrolling behavior. Stick to the original recommendation provided for a more stable outcome.

// JS
function toggleNav() {
  var x = document.getElementById("nav-links");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}
/* CSS */

body {
  font-family: 'Roboto', Arial, sans-serif;
  background: white;
  padding: 0;
  margin: 0;
}

.topnav {
  overflow: hidden;
  background-color: #333;
  position: relative;
  width: 100vw;
}

.topnav #nav-links {
  display: none;
}

.topnav a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;
}

.topnav a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
}

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

Click on the link to go to another page according to the drop-down option selected

Here's a puzzling query that even Google fails to address. I've got two pages: page-1 and page-2. On page-2, there is a <ul> element and a toggle-button-box with the following code: <ul> <li><button class="button is-checked ...

Why does clicking to add to an existing array only result in the array being cleared instead?

Need help with an AngularJS question relating to scope binding and click events. I want to add one value on the first click and another value on the second click, but it's only returning an empty array and filling the first value again. Why is that ha ...

Differences in React projects utilizing materialize-css compared to those using react-toolbox or material-ui

Is there a difference in technical benefits or code reliability when directly using material-css in JSX versus utilizing JSX specific libraries like material-ui or react-toolbox? Conversely, could using JSX libraries like material-ui or react-toolbox provi ...

Enhance data granularity in Jquery Flot by zooming in as the user interacts

I'm currently working on creating an interactive chart that allows users to zoom in for a more detailed view of the data. For instance, when viewing data over a default zoom period of 3 months, there would be one data point displayed every 24 hours. H ...

Creating an image and video slider using bootstrap techniques

Currently, I am attempting to create an image and video slider using bootstrap. However, the slider I have created is not functioning as expected. For instance, the slider is not pausing for videos, and the videos are not autoplaying. I attempted to sear ...

How can I use D3.js to form a circular group in an organization structure, link it with a circular image, and connect

Is it possible to create a radial grouped circle using d3.js, similar to the image below: https://i.sstatic.net/1Hwd2.jpg I have written some code as shown below. However, I am facing challenges in connecting every circle with a curved line and displayi ...

Problem with Converting C# Objects into JavaScript

I need help transferring a double array from C# to JavaScript dynamically. Currently, I am using the following approach: var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var jsLat = serializer.Serialize(lat); After that, I use ...

JQuery selector is successfully working while vanilla JavaScript is not functioning as expected

This problem is unique because I am experiencing issues with querySelector and querySelectorAll in my JavaScript code. While JQuery works fine, vanilla JS does not work as expected. I would appreciate any insights on why this might be happening. Thank you ...

Guide to utilizing JavaScript and JQuery for retrieving a PDF from a specific URL and subsequently uploading the file to another website

I have a link to a PDF file: http://www.example.com/HelloWorld.pdf My objective is to download the file using JavaScript/JQuery/AJAX and temporarily store it in the browser, without saving it on the user's machine. Then, I want to POST it to . To ac ...

The x-axis scale in d3 is malfunctioning

I'm categorizing data into groups such as rich, poor, and all. I'm using a dropdown to select these values to be displayed on a scatterplot. The first transition works correctly, with text labels appearing as expected. However, upon selecting ano ...

Differences between Angular's $injector and Angular's dependency injectionAngular

As a newcomer to Angular, I am exploring the use of $injector and its get function to retrieve specific services. For instance: app.factory('$myService', function($injector) { return { ... var http = $injector.get('$http&apos ...

What is the relationship between `overflow:hidden` and the inability of `position:sticky` to function properly?

Within the code snippet provided, there is a sticky div positioned inside a container. It adheres to the top of the scrolling panel while remaining within its designated container at all times. This mimics the behavior seen in iOS' UITableView headers ...

Next auth does not provide authentication functionality for Firebase

I've implemented next-auth with a firebase adapter, and while everything seems to be functioning properly in terms of saving users in the database, I'm encountering some issues with authentication. import NextAuth from "next-auth" impo ...

Is there a way to obtain the current time upon clicking the timepicker?

Here is a snippet of my code: // TIME $('#timepicker').datetimepicker({ minDate: minTime, }) .on('dp.show', function(e){ console.log(selectedDate); if($(this).val()==""){ $(this).val(minTime.format("HH:mm") ...

The issue with displaying inline block is that the divs are not appearing side by side on the

Two of my div elements, namely form-panel and data-panel, are currently not aligned on the same line. How can I use display:inline-block to align them in a single line? Please review the code provided below. I have already used display:inline-block on both ...

Ensuring full height on Bootstrap 4 columns without triggering a scrollbar in the browser

I'm currently working with Bootstrap 4 and attempting to make 3 columns on the page fill 100% height, only displaying scrollbars within the columns and not on the entire page. I have experimented with applying h-100 to the row div, as well as implemen ...

What is the process for configuring NextJS to recognize and handle multiple dynamic routes?

Utilizing NextJS for dynamic page creation, I have a file called [video].tsx This file generates dynamic pages with the following code: const Video = (props) => { const router = useRouter() const { video } = router.query const videoData = GeneralVi ...

What is the proper way to implement React's Link component from an external npm package to avoid the error message stating, "you should not use link outside router"?

A custom NPM package has been developed to display an icon menu that can be used across multiple projects. Users have the flexibility to provide a 'route' prop to each icon, allowing them to act as links to different pages. Despite importing and ...

Is it possible to extract information from a string that includes HTML code within a browser using CSS selectors without actually generating the DOM elements?

I've been struggling with this basic task for hours. I can't find any libraries that work and none of the questions here address my specific issue. Here's what I need to do: The entire page's markup is in a string format. I must use ...

Utilizing ReactJs refs to set focus on an input element

Exploring the use of refs in ReactJs to focus a textbox from a button click. Encountering the following error message: bundle.js:114 Uncaught TypeError: Cannot read property 'focus' of undefined Check out the Source Code below: class FocusTex ...