designing a navigation menu - aligning two list items side by side

Struggling to create a navigation bar with multiple li elements positioned next to each other? I've been trying to figure it out but can't seem to get it right. Anyone know of a tutorial or solution for this? Spent days working on it...

CSS code and HTML code provided below:

CSS

li {
list-style-type:none;
list-style:none;
background:clear;
padding:0;
margin:0;

}

ul {
list-style-type:none;
list-style:none;
margin:0;
padding:0;
background-color:clear;
}

li.nav a {
text-align:center;
font-family:"Helvetica";
font-weight:lighter;
text-decoration:none;
display:inline;
background-color:green;
padding:10px;
margin:0;
color:#FF0;
float:none;
}

li.nav a:hover {
text-align:center;
font-family:"Helvetica";
font-weight:lighter;
text-decoration:none;
display:inline;
background-color:blue;
padding:10px;
margin:0;
color:#0FF;
}

li.sub a {
position:relative;
top:2px;
background:pink;
display:block;
margin:0;
padding:10px;
color:red;
width:100px;
}

li.sub a:hover {
position:relative;
top:2px;
background:pink;
display:block;
margin:0;
padding:10px;
color:red;
width:100px;
}

HTML

<li class="nav"><a href="#">Community</a>
<ul>
<li class="sub"><a href="#">Third Age</a></li>
<li class="sub"><a href="#">Tide Timetables</a></li>
<li class="sub"><a href="#">Schools</a></li>
<li class="sub"><a href="#">Religion</a></li>
<li class="sub"><a href="#">Clubs+Societies</a></li>
<li class="sub"><a href="#">Courses</a></li>
<li class="sub"><a href="#">The Council</a></li>
<li class="sub"><a href="#">Culture</a></li>
</ul>
</li>

<!--END OF COMMUNITY SECTION-->

<!--START OF EVENTS NAVIGATION BUTTON AND SUBMENU-->

<li class="nav"><a href="#">Events</a>
<ul>    
<li class="sub"><a href="#">Festivals</a></li>
<li class="sub"><a href="#">Family</a></li>
<li class="sub"><a href="#">Music</a></li>
<li class="sub"><a href="#">Comedy</a></li>
<li class="sub"><a href="#">Theatre</a></li>
<li class="sub"><a href="#">Exhibitions</a></li>
<li class="sub"><a href="#">Film</a></li>
<li class="sub"><a href="#">Literature</a></li>
</ul>
</li>

<!--END OF EVENTS SECTION-->

Answer №1

Consider incorporating these lines into your CSS.

li.nav{display:inline-block;position:absolute;}
li.nav:nth-child(2)
{margin-left:140px;}

OR

HTML

<div class="main-navigation">
    <nav class="navigation">
        <ul>
             <li>1
                <ul class="sub-menu"><li>1a</li><li>1b</li></ul>
             </li>
             <li>2
                <ul class="sub-menu"><li>2a</li><li>2b</li></ul>
             </li>
             <li>3</li>
             <li>4</li>
        </ul>
    </nav>
</div>

CSS

nav.navigation{
width: 75%;
height:50px;

}
nav ul ul.sub-menu{
display: none;
margin-left:-40px;
margin-top:-10px;   
}

nav ul li:hover > ul.sub-menu{
display: block;
}

nav ul{
height:50px;
list-style:none;
padding: 3px 0 0 0;
position: relative;
text-transform: uppercase;
}

nav ul:after{
content:""; 
clear:both; 
display:block;
}

nav ul li{
float: left;
padding:10px 30px;
background:yellow;
margin-left:5px;
}

nav ul ul.sub-menu{
background-color: #fcfcfc; 
padding: 0;
position: absolute; 
z-index:100;
top: 100%;
}

nav ul ul.sub-menu li{
float: none; 
margin-top: -3px;
height:30px;
background-color:green; 
}

The EXAMPLE can be viewed here

Answer №3

Incorporate the display:inline-block CSS property to style all of the li elements.

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

Steps to properly insert an SVG into a button

I have a block set up with a Link and added text along with an svg image inside. How can I properly position it to the lower right corner of the button? (see photo) Additionally, is there a way to change the background color of the svg? This is within th ...

I'm struggling to position the four divs in each of the two rows similar to the example provided. Can anyone help me figure this

I need help figuring out how to position 4 divs in a row so that when one is clicked and expands with additional information, it pushes the bottom div down. All 8 divs have the same class. I attempted using display: inline-block, but all the lower 4 divs m ...

Guide on playing two HTML5 videos simultaneously once they have been loaded

Is it possible to make two HTML5 videos autoplay simultaneously? Is there a way to delay the playback until both videos have fully loaded? I'm looking to ensure that neither video starts playing before the other has finished loading. Does that clarif ...

The previous method of using `vue/compiler-sfc ::v-deep` as a combinator has been phased out. Instead, opt for the new syntax `:deep(<

When developing with Nuxt 2, I am encountering numerous [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead. errors when running npm run dev. After attempting to resolve the issue by changing a ...

How should h tags be correctly utilized?

Is it better to use h tags or lists to style content? Take a look at this code, is using h tags the appropriate choice here or should I consider using a list instead? <div class="col-xm-12 col-sm-6 col-md-4"> <h4>Get in touch< ...

Tips on incorporating a repeating gradient instead of a linear gradient

I have the following code for a linear-gradient. Click here to view the code Is there a way to modify it to use a repeating gradient instead? The goal is to maintain the same image but with a repeating gradient effect. background-image: repeating-linear ...

Using ReactJS to toggle a div upon clicking

I am facing an issue with toggling a div by clicking on text within a toggle bar. I am in the process of recreating a WordPress website template in React JS that was originally built using bootstrap. You can view the original template here. So far, I have ...

Are there any jQuery plugins available that can display a loader image while a page is loading?

After extensive research on Google, I have been unable to find the plugin I need. If you are aware of any suitable plugins, please let me know. Thank you in advance! ...

Ways to send a POST variable to PHP without refreshing the webpage

Is there a way to pass a simple variable from a text-box on a different page to PHP without reloading it? I've been informed that Ajax is required for this task, but I'm unsure of how to go about implementing it. Can someone provide me with a sam ...

Incorporating JavaScript and Alpine.js to Monitor and Log Changes in Input Range Values

I have developed a basic app that logs the input range value to the console when the range input changes. Interestingly, it works flawlessly when I slide the slider left and right with my cursor. However, when I programmatically change the value using Ja ...

PHP version 7.2.1 is throwing an error indicating an undefined index for the file_upload

Encountering an error message: Notice: Undefined index: file_upload in C:\MAMP\htdocs\basic_files\upload.php on line 3 Each time I attempt to upload a file using the form. While many attribute this issue to problems with enctype or ...

When the height of a div is dynamically modified, triggering a mouseout

I am currently in the process of customizing selectboxes using jQuery to ensure that they are styled exactly how I envision them. Initially, I attempted to achieve this through CSS alone, but soon realized that I wanted the options to be revealed upon cli ...

How to fix the bottom white space issue on Phonegap for Windows Phone 8

I'm currently working on a Phonegap App designed for Windows Phone 8. During testing on two different devices - the HTC Windows Phone 8S and Nokia Lumia 820, I noticed that my app displays correctly on the HTC device but leaves a white space at the bo ...

Retrieving the content of a particular division within a row of a table

I have a dynamic table displaying data retrieved from a database. Each row consists of 4 different div elements each containing text content. I am trying to extract the value of a specific div element within each row. Below is a snippet of the table struc ...

Display a div when hovering over it, with its position set from the bottom of the main

I am attempting to use CSS to display another div within my main div, but positioned from the bottom of the main div rather than as a block as I am currently testing with my code. HTML <div id="main"> <a href="#"><img src="https://www.g ...

Discover the power of catching Custom DOM Events in Angular

When working with an Angular library, I encountered a situation where a component within the library dispatches CustomEvents using code like the following: const domEvent = new CustomEvent('unselect', { bubbles: true }); this.elementRef.nati ...

Top method for triggering an external JavaScript function through an HTML element's onclick event obtained from an Ajax request

index.php: <html> <head> <script type="text/javascript" src="script.js"></script> </head> <body> <div id="box"></div> <div onclick="getContent()">Open Window</div ...

Issues with Laravel capturing complete request data

In my Laravel project, I am trying to pass select2 multiple select values to the database using AJAX. Previously, I was passing the values through get requests which resulted in all values being shown as URL parameters. However, when checking with a reques ...

Adding a CSS file to Squarespace in developer mode is a simple process that can greatly

I included index.css in the styles folder and referenced it in template.conf: { "name": "Wright", "author": "Squarespace, Inc.", "layouts": { "default": { "name": "D ...

Update the session's data

Situation : I am working on a basic php form for calculations and pricing. I have set up the add, edit, and delete functions using a switch statement. Within my session, data is stored using $_POST. Issue with Edit Function : if (isset($_POST[&apo ...