Navigation Menu Spanning Across Full Width of All Screens

Hello!

I am currently working on designing a responsive menu using HTML5 and CSS. My approach involves creating a navigation menu for desktop computers and implementing a checkbox with a label to reveal the responsive menu when needed.

Here is the code snippet:

body {
  margin: 0px;
}
/*Strip the ul of padding and list styling*/

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  position: absolute;
}
/*Adjust image positioning */

#img-nav {
  padding-top: 0px !important;
}
/*Create a horizontal list*/

li {
  display: inline-block;
  float: left;
}
/*Style for menu links*/

li a {
  display: block;
  min-width: 200px;
  height: 70px;
  text-align: center;
  line-height: 50px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #fff;
  background: #2f3036;
  text-decoration: none;
  padding-top: 30px;
}
/*Hover state for top level links*/

li:hover a {
  background: #19c589;
}
...
<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <title>CSS Only Navigation Menu</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="css/home.css">
</head>

<body>
  <nav>
    <label for="show-menu" class="show-menu">Show Menu</label>
    <input type="checkbox" id="show-menu" role="button">
    <ul id="menu">
      ...
    </ul>
  </nav>
</body>

</html>

The challenge I'm facing now is ensuring that my menu spans full width on PC screens. If you have any suggestions or solutions, please share them. Thank you in advance!

Answer №1

Oops! I made a mistake. Make sure to enclose the ul in a div tag within the HTML code. Adjust the div's z-index to be greater than 0, set its position to fixed, and ensure its width is set to 100%.

Answer №2

Try moving the background from the a tag to the ul tag and apply CSS with width:100%. This adjustment worked perfectly for me.

body {
  margin: 0px;
}
/*Remove default padding and list styling from ul*/

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  position: absolute;
  width:100%;
  background: #2f3036;
}
/*Keep image in place */
#img-nav {
  padding-top: 0px !important;
}
/*Create horizontal list*/
li {
  display: inline-block;
  float: left;
}
/*Style for menu links*/
li a {
  display: block;
  min-width: 200px;
  height: 70px;
  text-align: center;
  line-height: 50px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #fff;
  
  text-decoration: none;
  padding-top: 30px;
}
/*Hover effect for top level links*/
li:hover a {
  background: #19c589;
}
/*Style for dropdown links*/
li:hover ul a {
  background: #f3f3f3;
  color: #2f3036;
  height: 40px;
  line-height: 40px;
}
/*Hover effect for dropdown links*/
li:hover ul a:hover {
  background: #19c589;
  color: #fff;
}
/*Hide dropdown links until needed*/
li ul {
  display: none;
}
/*Make dropdown links vertical*/
li ul li {
  display: block;
  float: none;
}
/*Prevent text wrapping*/
li ul li a {
  width: auto;
  min-width: 100px;
  padding: 0 20px;
}
/*Display dropdown on hover*/
ul li a:hover + .hidden,
.hidden:hover {
  display: block;
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  text-decoration: none;
  color: #fff;
  background: #19c589;
  text-align: center;
  padding: 10px 0;
  display: none;
}
/*Hide checkbox*/
input[type=checkbox] {
  display: none;
}
/*Show menu when checkbox is checked*/
input[type=checkbox]:checked ~ #menu {
  display: block;
}
/*Responsive Styles*/
@media screen and (max-width: 760px) {
  /*Show dropdown links inline*/
  ul {
    position: static;
    display: none;
    background: #2f3036;
  }
  /*Add vertical spacing*/
  li {
    margin-bottom: 1px;
  }
  /*Make all menu links full width*/
  ul li,
  li a {
    width: 100%;
    background: #2f3036;
  }
  /*Display 'show menu' link*/
  .show-menu {
    display: block;
  }
  #img-nav {
    display: none;
  }
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>CSS Only Navigation Menu</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="css/home.css">
</head>

<body>
  <nav>
    <label for="show-menu" class="show-menu">Show Menu</label>
    <input type="checkbox" id="show-menu" role="button">
    <ul id="menu">
      <li>
        <a href="#" id="img-nav">
          <img src="images/nav/logoplacehold.png" alt="">
        </a>
      </li>
      <li><a href="#">Inicio</a>
      </li>
      <li>
        <a href="#">Aula Virtual</a>
        <ul class="hidden">
          <li><a href="#">Test Online</a>
          </li>
          <li><a href="#">Test DGT</a>
          </li>
        </ul>
      </li>
      <li><a href="#">Resultado Teórico</a>
      </li>
      <li><a href="#">Nuestros Vehículos</a>
      </li>
      <li><a href="#">Permisos</a>
      </li>
    </ul>
  </nav>
</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

Eliminate the table background in a single cell

Apologies if the title is not very descriptive, I couldn't find the right words to use. Below is the table in question. https://i.sstatic.net/LHwbG.png I'm looking to remove the white border (background of the table) from just the upper-left ce ...

Align content to the bottom using Bootstrap 4

Looking for help with aligning content to the middle and bottom on a full-page bootstrap layout? The first page has a background image in the first div, and a darker layer on top in the second div. <div class="h-100 w-100"> <div class="h-100 w-10 ...

Using a global CSS file in Angular can lead to large module bundles being emitted by Webpack

In our Angular application generated with angular-cli, we are utilizing various global styles and imports defined in the styles.scss file (which begins with /* You can add global styles to this file, and also import other style files */ if that sounds fami ...

Setting intervals to change background images resulted in a delay

I've been working on creating an animation that switches between 2 images used as the background image of a div. Here's the code snippet I've been using: var spacecraftInterval = setInterval(function() { if (access == true) { var image ...

hiding elements yet passing down

Despite disabling the style sheet, a dynamically created form is displaying strangely. This form will show or hide various details based on selected options. For instance, many of the elements are generated with C#.net... formOutput += "<div class=&bs ...

Ways to customize the appearance of an iframe's content from a separate domain

I am facing a challenge with my widget and multiple websites. The widget is hosted on one domain, but the websites use an iframe to display it. Unfortunately, because of the Same Origin Policy, I cannot style the content of the iframe from the parent websi ...

Dynamic WordPress Website Designs

Seeking recommendations for responsive WordPress themes that offer extensive CSS and structural customization options. The goal is to retain all of WordPress's built-in features, such as blogging capabilities, while having the freedom to completely co ...

Inserting a cell into a duplicated table while keeping the original table intact

I am looking to insert a new TD into a cloned table so that it exists only in the cloned version and not in the original one. This way, users won't accidentally remove the original table leaving nothing to be cloned :) Currently represented as follow ...

Is it possible to dynamically alter the text and contrast of your entire website?

In the realm of MVC 4 and Visual Studio 2013: I am pondering over how to dynamically change the text on my website with the simple click of a button. The same goes for adjusting contrast. Are there any plugins or techniques that could facilitate this proc ...

What is the reason behind receiving email alerts whenever visitors access my website?

While developing a website, I ran some tests and noticed that whenever I visit the URL, an email notification is generated. However, the notification received is just a blank one. Could this issue be related to what I entered in the action field? <for ...

Troubleshooting: Issue with Deleting Table Rows using MySQL and PHP

I am having trouble deleting a table row from my database using MySQL and PHP. Despite reviewing my code, I cannot identify the issue. I believe that I am close to resolving the problem, likely something simple that I am overlooking. When hovering over th ...

Nesting Multiple Click Directives in AngularJS

I am facing a situation where I have two directives in play – one is enclosed within a specific view, while the other is applied to the container of that view. <div id="content" data-panel='{{ pNav }}' close-content> <div class="i ...

The image is not properly aligned with the background image

I'm currently working on a page design using bootstrap and css where I want images to fade and reveal the background when hovered over. The challenge I'm facing is that although all images are set to 100px in size, there is white space visible ar ...

Troubleshooting a mapping problem with CSS elements

While building my website, I have successfully mapped the elements by ID for all alphabet letters except A. When clicking on the letter A, it does not go to the respective elements. Can anyone please help me find a solution? Visit this link for reference ...

SwiperJS continuously applies additional right margin to every slide

My Swiper carousel seems to be adding an unnecessary 5px margin-right to each slide, resulting in the cards not fitting into one frame. https://i.sstatic.net/fwn5G.png I attempted to fix this by setting the margin-right to 0px for the .swiper-slide class ...

Struggling to figure out the ::before border trim issue

As I work on designing a banner for a webpage, I have added a sliced bottom right border using the ::before pseudo class. The code snippet I used includes: &::before { content: ""; position: absolute; bottom: 0; right: 0; ...

What is the best way to automatically create a line break once the User Input reaches the end of a Textbox?

I've been searching for a solution to this question without any luck. Here is the tag I'm working with: <input type="text" id="userText" class="userTextBox"> And here is my CSS: .userTextBox { /* Add marg ...

Fix for fixed scrolling in the navigation bar

Having a website that receives traffic from different countries, including Portugal and other non-English speaking places, I decided to add a translation option. However, I encountered an issue with Google's translate feature which displays a banner a ...

Determining User Consent: A Straightforward Guide to Identifying Previously Accepted Cookie Consent

I've been tasked with adding a cookie to a website. The site only tracks the number of visits, so there is no user-specific data available for cookie assignment or acknowledgment tracking. Is it possible to show the cookie notification only to users ...

The table is refusing to align itself in the center

I've been struggling to center the text inside this table despite trying various solutions. Any help would be greatly appreciated. Here's the HTML code I have: <div id="paccount"> <table style="margin: 0 auto;"> <tr&g ...