Aligning Submenus Horizontally in HTML5

I am struggling with aligning the horizontal submenu correctly on my webpage. I have a header with a logo on the left and a menu on the right. When hovering over the main menu, the submenu appears but does not align properly and extends off the page. I tried adjusting the "header nav li:hover ul" CSS code by changing it to relative positioning, which helped prevent the submenu from going off the page, but caused another issue of the main menu moving from its original position. Can someone provide assistance with this CSS issue and help me fix the problem?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
a {text-decoration:none;color:#000;}

/* header */
header {width:980px;height:100px;margin:0 auto;margin-top:20px;background-color:Ff5f5f5;border-style:solid;}
header #logo {margin-left:10px;float:left;font-size:36px;}

/* menu */
header nav {margin:0;padding:0;float:right;margin-top:40px;display:block;}
header nav ul {list-style:none;}
header nav li {float:left;padding:0 8px;display:inline-block;}
header nav li a {display:inline-block;text-decoration:none;font-weight:700;}
header nav li a:hover {color:#09F;}
header nav li ul{display:none;}

/* submenu */
header nav li:hover ul{display:block;position:absolute;}
header nav li:hover ul li {display:inline-block;float:left}
header nav li:hover ul li a{font-weight:400;}
header nav li ul li a{width:auto;}
</style>
</head>

<body>
<header>
<div id="logo">
<p>LOGO</p>
</div> <!-- end logo -->
<nav>
<ul>
<li><a href="#">Menu 1</a>
<ul>
<li><a href="#">SubMenu 1</a></li>
<li><a href="#">SubMenu 2</a></li>
<li><a href="#">SubMenu 3</a></li>
</ul>
</li>
<li><a href="#">Menu 2</a>
<ul>
<li><a href="#">SubMenu 1</a></li>
<li><a href="#">SubMenu 2</a></li>
<li><a href="#">SubMenu 3</a></li>
</ul>
</li>
<li><a href="#">Menu 3</a>
<ul>
<li><a href="#">SubMenu 1</a></li>
<li><a href="#">SubMenu 2</a></li>
</ul>
</li>
<li><a href="#">Menu 4</a>
<ul>
<li><a href="#">SubMenu 1</a></li>
<li><a href="#">SubMenu 2</a></li>
<li><a href="#">SubMenu 3</a></li>
<li><a href="#">SubMenu 4</a></li>
<li><a href="#">SubMenu 5</a></li>
</ul>
</li>
</ul> <!-- end main ul -->
</nav> <!-- end nav -->
</header>
</body>
</html>

Answer №1

I put in a lot of effort to understand your requirements. Are you looking for a setup with the main menu on the right and submenus below it? I hope this solution meets your needs.

a {text-decoration:none;color:#000;}

/* header */
header {width:980px;height:100px;margin:0 auto;margin-top:20px;background-color:Ff5f5f5;border-style:solid;}
header #logo {margin-left:10px;float:left;font-size:36px;}

/* menu */
header > nav {margin:0;padding:0;float:right;margin-top:40px;display:block;}
header > nav > ul {list-style:none; padding-left: 0px;}
header > nav > ul > li {float: left; width: 100px;text-align:right;}

header > nav > ul > li > ul {display: none;list-style:none;padding-left: 0px; background-color: white; border: 1px solid black;}
header > nav > ul > li > ul > li {display: inline-block;}
header > nav > ul > li:hover > ul {display: block;list-style:none;white-space: nowrap; float: right;}
<header>
<div id="logo">
<p>LOGO</p>
</div> <!-- end logo -->
<nav>
<ul>
<li><a href="#">Menu 1</a>
<ul>
<li><a href="#">SubMenu 1</a></li>
<li><a href="#">SubMenu 2</a></li>
<li><a href="#">SubMenu 3</a></li>
</ul>
</li>
<li><a href="#">Menu 2</a>
<ul>
<li><a href="#">SubMenu 1</a></li>
<li><a href="#">SubMenu 2</a></li>
<li><a href="#">SubMenu 3</a></li>
</ul>
</li>
<li><a href="#">Menu 3</a>
<ul>
<li><a href="#">SubMenu 1</a></li>
<li><a href="#">SubMenu 2</a></li>
</ul>
</li>
<li><a href="#">Menu 4</a>
<ul>
<li><a href="#">SubMenu 1</a></li>
<li><a href="#">SubMenu 2</a></li>
<li><a href="#">SubMenu 3</a></li>
<li><a href="#">SubMenu 4</a></li>
<li><a href="#">SubMenu 5</a></li>
</ul>
</li>
</ul> <!-- end main ul -->
</nav> <!-- end nav -->
</header>

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

Match the test choices with the corresponding `radio` buttons

Looking for help with my quiz application. How can I align text vertically with radio buttons? Take a look at the code here on Codepen. One issue I'm facing is the alignment of long label texts, particularly in questions 9, 12 & 14. I've tried va ...

The image displayed in a received email on Outlook 2010 appears excessively large

Currently, I am in the process of developing a webshop for the company I work for. One of the tasks on my agenda is to work on the order confirmation emails that are sent out to our customers once they complete their purchase. The webshop itself is built ...

Using rtl direction in Firefox does not function properly

Is there a way to create a select menu that opens from right to left? When I use Google Chrome, it appears correctly. https://i.sstatic.net/tgVvM.png However, when I use Firefox, it displays an error. https://i.sstatic.net/xys5v.png Below is the code ...

Guide on utilizing multiple ng-apps alongside multiple controllers

Having trouble accessing controller values in different ng-apps? Here's a setup with two ng-apps and their controllers, where you may encounter errors when trying to access the value of one controller in another. Need some assistance with this issue. ...

Resolving problems with image dimensions in Angularjs and ionic framework

I'm attempting to achieve a design where the first image occupies 50% of the screen's height and 100% of its width, while the second image does the same. Please refer to the image below: https://i.sstatic.net/nwmRP.jpg ...

Is it possible to create a CSS design where alternating table rows have two different heights?

Looking to design a table where the height of every other row is approximately one-third of the preceding row. I've managed to style this with an inline method, but I'm curious if it's possible to create separate tags for different tr styles ...

Utilizing onmouseover and onmouseoff events with images to dynamically alter text content

I have 6 images and I want to dynamically change the text next to them based on which image is being hovered over with the mouse. Even though I attempted to achieve this using JavaScript with the onmouseover and onmouseout events, my code doesn't see ...

Looking to utilize Bootstrap to create a responsive two-column form design

I have a form with two columns arranged like this on desktop view |A|B| |C|D| |E|F| However, on mobile, I want it to look like this |A| |B| |C| |D| |E| |F| Unfortunately, I'm currently seeing |A| |C| |E| |B| |D| |F| This is the structure of my c ...

Insert data into a table without any formatting

I'm trying to enhance my table by adding inputs within the td tags, but I want them to appear as plain text cells. Below is the code snippet I have been working with: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js ...

Beginner in html, css, and javascript: "Tips for saving jsfiddle demos?"

Recently, I developed an interest in JavaScript and CSS and came across some impressive examples on jsfiddle. I was wondering if there is a way to "export" these examples to my computer. Simply copying and pasting doesn't seem to work. How can I modi ...

PHP: Locate a class within an HTML element using a regular expression

I have a question about customizing my HTML ul menu with submenus. Specifically, I am looking to dynamically add a "first-item" class to the first item within the submenu using PHP. <ul id="menu-main" class="menu"> <li id="menu-item-68" class=" ...

Issue in Chrome where hover state does not persist after clicking, occurring sporadically

It's surprising that I couldn't find any information about this on Google. This bug is really annoying, and I'm not sure if it can be fixed without an update from Google for their browser. Description of the issue: When clicking on an eleme ...

Switching effortlessly between Fixed and Relative positioning

As I work on creating a unique scrolling experience, I aim to have elements stop at specific points and animate before returning to normal scroll behavior once the user reaches the final point of the page. Essentially, when div X reaches the middle of the ...

Is it possible to store values in LocalStorage by clicking?

Is there a way to store this value in localstorage whenever the user clicks either up or down??? <script> var Clicks = 800; function UpClick() { Clicks = Clicks + 25; document.getElementById('CountedClicks').innerHTML = C ...

What are the best practices for setting up responsive margins?

Looking to create a div using bootstrap but facing an issue. The challenge is setting a specific margin on the left and right sides of the div. When I use col-md-10, the margin exceeds 10px on wider screens. Here's what I'm aiming for: <div ...

"Obtain the PHP post ID from the dropdown selection, not its value

In my PHP PDO statement, I am querying a table in the database to retrieve a list of users. The purpose of the form is to record maintenance events, with the username being used to identify the person responsible for the maintenance task. However, upon f ...

The public folder in Node.js is known for its tendency to encounter errors

I'm facing an issue with displaying an icon on my website. Here is the current setup in my code: app.js const http = require('http'); const fs = require('fs'); const express = require('express') const path = require(&apo ...

JavaScript is used to retrieve the data, however, Internet Explorer is unable to display the data correctly

My web UI looks great in Firefox and Chrome, but for some reason, Internet Explorer is having trouble displaying the data correctly. I am using XMLHttpRequest to fetch data and display it dynamically in my UI. However, IE seems to have issues with this me ...

Discover the seamless method of transferring data from an HTML5 form to a PHP page for processing without the need to refresh the page or manually navigate to the PHP file

When creating a website, I encountered an issue with a form that requires the user to select three options from select elements and then click a search button to search the database. The problem is that PHP does not retrieve the form data unless the button ...

Unable to parse multi-line HTML content retrieved from a webpage

Here is the snippet of HTML code: <td class="foobar" id="12345POE46" data="more &amp; data"> <a href="http://www.stackoverflow.com" more="{data}">Value</a></td> Although I have experience with regex, I am new to using it i ...