Creating a fully responsive navigation bar for all screen sizes

Recently, I came across a helpful YouTube tutorial on creating a navigation menu using HTML and CSS. The main challenge I encountered was getting the width of my navigation bar to span 100% of the screen on all devices while maintaining a height of 30px. In addition, I wanted the navigation bar to be centered automatically as more tabs were added, filling the screen evenly. Below is the code snippet I used:

HTML:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
        <title>My Website</title>
    </head>
    <body>
        <div id="body">
            <div style="display:none">
                <audio controls autoplay>
                    <source src="Audio/welcome 2.4.mp3" type="audio/mpeg">
                    Your browser does not support the audio element.
                </audio> 
            </div>
            <img src="Images/web-development-banner.jpg" width="100%" height="400">
            <h1 class="webheading">Website Development</h1>
            <div id="navMenu">
                <ul>
                    <li><a href="index.html">Learn HTML</a>
                        <ul>
                            <li><a href="index.html">1. Things You Need</a></li>
                            <li><a href="setup.html">2. Setting Up Your Website Folders</a></li>
                            <li><a href="extrainfo.html">3. Extra Information</a></li>
                            <li><a href="layout.html">4. HTML Layout</a></li>
                        </ul>
                    </li>   
                </ul>
               ...
               // Additional UL elements
               ...
           </div>

CSS:

#body {
    background: url(Images/bigimage.jpg);
    background-color:#000000;
    background-size: 100% 100%;
    width: auto;
    height: auto;
    background-repeat: no-repeat;
    background-attachment: fixed;
    margin: 0;
    margin-top: 0;
    color: #FFFFFF;
}

#navMenu {
    margin: 0 auto;
    padding: 0;
    width: 100%;
}

#navMenu ul {
    margin: 0;
    padding: 0;
    line-height: 30px;
}

#navMenu li {
    margin: 0;
    padding: 0;
    list-style: none;
    float: left;
    position: relative;
    background: #999;
}

// Remaining CSS styles...

Answer №1

#body {
    background: url(Images/bigimage.jpg);
    background-color:#000000;
    background-size: 100% 100%;
    width: auto;
    height: auto;
    background-repeat: no-repeat;
    background-attachment: fixed;
    margin: 0px;
    margin-top: 0px;
    color: #FFFFFF;
}

#navMenu {
    margin: 0 auto;
    padding: 0;
    width: 100%;
    text-align:center;
    display:table;
}

#navMenu ul {
    margin: 0;
    padding: 0;
    line-height: 30px;
    display: table-cell;
}

#navMenu li {
    margin: 0;
    padding: 0;
    list-style: none;
    float: none;
    position: relative;
    background: #999;
}

#navMenu ul li a {
    text-align: center;
    font-weight: bold;
    font-family: "Comic Sans MS", cursive;
    text-decoration: none;
    height: 30px;
    
    display: block;
    color: #FFF;
    border: 1px solid #FFF;
    text-shadow: 1px 1px 1px #000
}

#navMenu ul ul {
    position: absolute;
    visibility: hidden;
    top: 32px;
}

#navMenu ul li:hover ul {
    visibility: visible;

}

#navMenu li:hover {
    background: #09F;
}

#navMenu ul li:hover ul li a:hover {
    background: #CCC;
    color: #000;
}

#navMenu a:hover {
    color: #000
}

.clearFloat {
    clear: both;
    margin: 0;
    padding: 0;
}

<ul>
                <li><a href="index.html">Learn HTML</a>
                    <ul>
                        <li><a href="index.html">1. Things You Need</a></li>
                        <li><a href="setup.html">2. Setting Up Your Website Folders</a></li>
                        <li><a href="extrainfo.html">3. Extra Information</a></li>
                        <li><a href="layout.html">4. HTML Layout</a></li>
                    </ul>
                </li>   
            </ul>
<div id="body">
            <div style="display:none">
                <audio controls autoplay>
                    <source src="Audio/welcome 2.4.mp3" type="audio/mpeg">
                    Your browser does not support the audio element.
                </audio> 
            </div>
            <img src="Images/web-development-banner.jpg" width="100%" height="400">
            <h1 class="webheading">Website Developement</h1>
            <div id="navMenu">
                <ul>
                    <li><a href="index.html">Learn HTML</a>
                        <ul>
                            <li><a href="index.html">1. Things You Need</a></li>
                            <li><a href="setup.html">2. Setting Up Your Website Folders</a></li>
                            <li><a href="extrainfo.html">3. Extra Information</a></li>
                            <li><a href="layout.html">4. HTML Layout</a></li>
                        </ul>
                    </li>   
                </ul>
                <ul>
                    <li><a href="index.html">Learn HTML</a>
                        <ul>
                            <li><a href="index.html">1. Things You Need</a></li>
                            <li><a href="setup.html">2. Setting Up Your Website Folders</a></li>
                            <li><a href="extrainfo.html">3. Extra Information</a></li>
                            <li><a href="layout.html">4. HTML Layout</a></li>
                        </ul>
                    </li>   
                </ul>
                <ul>
                    <li><a href="index.html">Learn HTML</a>
                        <ul>
                            <li><a href="index.html">1. Things You Need</a></li>
                            <li><a href="setup.html">2. Setting Up Your Website Folders</a></li>
                            <li><a href="extrainfo.html">3. Extra Information</a></li>
                            <li><a href="layout.html">4. HTML Layout</a></li>
                        </ul>
                    </li>   
                </ul>
                <ul>
                    <li><a href="index.html">Learn HTML</a>
                        <ul>
                            <li><a href="index.html">1. Things You Need</a></li>
                            <li><a href="setup.html">2. Setting Up Your Website Folders</a></li>
                            <li><a href="extrainfo.html">3. Extra Information</a></li>
                            <li><a href="layout.html">4. HTML Layout</a></li>
                        </ul>
                    </li>   
                </ul>
                <br class="clearFloat">
            </div>

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

Why isn't the dropdown menu displaying beneath the background in bootstrap / HTML / CSS?

Having trouble creating a navbar with a dropdown menu on the right? I encountered an issue where the menu doesn't show up when clicked. However, after removing all CSS styles, the button starts working. .info { margin-top: 15%; display: table; ...

Header components struggling with proper alignment in CSS

I'm facing an issue with the navigation bar elements in my header. They are currently aligned to the left side of the screen, but I want them to move to the right. Does anyone know why they are not moving? Also, is there a specific rule or method for ...

Step-by-step guide on completing and submitting a form through a Windows application

Many are questioning the age and repetition of this query, but here is where my issue begins. Are you noticing two input fields with the same name? HTML CODE <html> <head><title></title> </head> <body> <input type= ...

Conceal the Form Upon Submission

I'm currently working on a project where I want to hide my form after the submit button is pressed using Jquery. Firstly, I have included the Jquery library in my code. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jque ...

Is there a simple way to create a row of triangle-shaped divs using Bootstrap, similar to the design shown

Is it possible to have a white section and background image in the same row? I am aiming for a layout similar to the example shown https://i.stack.imgur.com/J0S2t.jpg ...

What is the best way to implement a sidebar closing animation?

Utilizing both react and tailwindcss, I successfully crafted a sidebar menu that elegantly appears from left to right when the user clicks on the hamburger icon. However, my attempts to create a reverse animation as the sidebar disappears from right to lef ...

As I populate my mobile content, background images are being stretched out

I'm currently in the process of enhancing my website by incorporating background images using the background property. My goal is to ensure that each image fills the width and height of its container without sacrificing quality or aspect ratio. Here i ...

Mistakes in the Horizontal Alignment of Bootstrap Forms

I'm having trouble aligning my textboxes with my form in Bootstrap within ASP.NET MVC 4. Despite using the form-horizontal div, my elements are displaying misaligned. How can I ensure that the elements align properly with the labels? <div clas ...

"Develop a mobile application using PhoneGap designed specifically for use on iPad and

Looking to create an app for iPad and iPad Mini using PhoneGap. While I have some basic knowledge of HTML and CSS, I am struggling with adjusting the image proportion, width, and height of the app. I want to write code that will adapt properly to the vary ...

Use Google script to input drop down options directly into a Google spreadsheet

After gathering coding advice from various StackOverflow examples, I've hit a roadblock while attempting to take my script to the next level. The task seems simple enough, but for some reason, I just can't figure it out. Here's the situati ...

Choosing an option in a Dropdown using Semantic-UI-React through code

I designed a Dropdown menu in my project using Semantic-UI-React to allow users to choose colors. https://i.sstatic.net/PkCLa.png Here is the code snippet for the dropdown menu: import React, { Component } from 'react'; import { Dropdown } fro ...

To insert a new row into a table with an onClick function, along with removing this attribute from all other rows except for the newly added one

I recently created a piece of code that enables the addition of a new row and removes an (onClick) attribute when clicking on an existing row within a table. <table style="vertical-align:middle;margin:20px;" id="table_insert"> <tr id="tra" > ...

Does anyone know the ins and outs of how the website www.nikebetterworld.com was created?

Hello, I am new to web development and I am interested in creating a page similar to the style of nikebetterworld. Can you point me in the right direction on where to start studying to achieve this design? My impression is that it involves multiple scrol ...

Employing state management in React to toggle the sidebar

A working example of a sidebar that can be toggled to open/close using CSS, HTML and JavaScript is available. Link to the Example The goal is to convert this example to React by utilizing states instead of adding/removing CSS classes. To ensure the side ...

The text is not rendering properly, with some characters being replaced by different ones

RESOLUTION: To fix the issue, eliminate font-family, font-size, and color properties from the parent div. UPDATE: The problem only occurs when I press CTRL + F5. UPDATE 2: After investigating, I found that the culprit is .site-footer with a position:abso ...

unable to modify background color when mouse hovers in CSS

Recently, I implemented this code to style the navigation bar on my website. However, I encountered an issue where the color does not change when hovering over the links. .nav { list-style-type:none; margin:0; padding:0; overflow:hidden; } ...

The jQuery carousel script is not functioning

Currently developing a jQuery slideshow for my website, and I stumbled upon a code that fits my requirements perfectly. I made some adjustments to the code from this source: . My specifications were that the code should be created from scratch, lightweight ...

Utilize Bootstrap cards to display images across the full width of the page

I am using the Bootstrap card component https://getbootstrap.com/docs/4.0/components/card/ to display an image. The issue I'm facing is that on the Bootstrap template, it adds a margin around the image. I would like the image to take up the full width ...

Simulating a mobile device screen size using an embedded iframe

Imagine this scenario: What if instead of adjusting the browser window size to showcase a responsive web design, we could load the site into an IFRAME with the dimensions of a mobile screen. Could this concept actually work? Picture having an IFRAME like ...

What could be causing my radio button list to stop functioning correctly? (After applying CSS)

While working on a simple postage calculator in Visual Studio with Web Forms (yes, we had to use Web Forms in Class), I included some Bootstrap for styling. However, there was an issue where my RadioButtonList suddenly stopped functioning properly. None ...