CSS - How can I make a simple navigation menu with submenus? - html

I am learning HTML and CSS at the moment. I have my navigation menu done, but I have submenus under the main selections. Here is my code:
#navigation ul {
font-family:Arial;
list-style-type:none;
margin:0;
padding:0;
}
#navigation li {
font-family:Arial;
font-size:11px;
display:inline;
float:left;
background-color:#FFF;
}
#navigation a {
display:block;
width:60px;
background-color:#FFF;
}
And here is my HTML (I am making a site on Half Life and Portal to learn):
<div id="menus">
<div id="navigation">
<ul>
<li>Aperture Science</li>
<ul>
<li>GLaDOS</li>
<li>Testing Chambers</li>
<li>The Player (Chell)</li>
</ul>
</ul>
<ul>
<li>Black Mesa</li>
<ul>
<li>The Combine</li>
<li>The Resistance</li>
<li>The Player (Gordon Freeman)</li>
</ul>
</ul>
</div>
What do I need to change?
Note: Yes, I haven't finished putting in quick 'a' tags...

you can use this code---
<div id="menus">
<ul>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home
<ul class="submenu">
<li>Sub</li>
<li>Sub</li>
<li>Sub</li>
<li>Sub</li>
<li>Sub</li>
</ul>
</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
css--
#menus ul{
padding:0
}
#menus ul li{
display:inline-block;
margin-right:-2px;
position:relative;
}
#menus ul li a{
display:inline-block;
padding:5px 10px;
background:red;
color:#FFF;
}
#menus ul ul{
position:absolute;
left:0;
width:0px;
transition:all .5s;
}
#menus ul.submenu li{
display:block;
transform:scale(0, 1);
transition:all .5s;
}
#menus ul.submenu li:nth-child(2){
transition-delay:.05s;
}
#menus ul.submenu li:nth-child(3){
transition-delay:.1s;
}
#menus ul.submenu li:nth-child(3){
transition-delay:.15s;
}
#menus ul.submenu li:nth-child(4){
transition-delay:.2s;
}
#menus ul.submenu li:nth-child(5){
transition-delay:.25s;
}
#menus ul li:hover ul{
display:block;
width:auto;
}
#menus ul li:hover ul li{
transform:scale(1, 1);
transform-origin: right top;
}
view the jsfiddle live demo

Check code
#menus ul{
padding:0
}
#menus ul li{
display:inline-block;
margin-right:-2px;
position:relative;
}
#menus ul li a{
display:inline-block;
padding:5px 10px;
background:#ccc;
color:#000;
text-decoration: none;
}
#menus ul li a:hover{background: #666;}
#menus ul ul{
position:absolute;
left:0;
width:200px;
transition:all .5s;
max-height: 0;
overflow: hidden;
}
#menus ul.submenu li{
display:block;
}
#menus ul.submenu li a{
display:block;
background:blue;
color: #fff;
}
#menus ul.submenu li a:hover{background: #333;}
#menus ul li:hover ul{
max-height: 10000px;
}
<div id="menus">
<ul>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Sub Menu
<ul class="submenu">
<li>Sub</li>
<li>Sub</li>
<li>Sub</li>
<li>Sub</li>
<li>Sub</li>
</ul>
</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>

Try This code:
HTML CODE:
<div id="menus">
<div id="navigation">
<ul>
<li>Aperture Science</li>
<ul>
<li>GLaDOS</li>
<li>Testing Chambers</li>
<li>The Player (Chell)
<ul>
<li>GLaDOS</li>
<li>Testing Chambers</li>
<li>The Player (Chell)</li>
</ul>
</li>
</ul>
</ul>
<ul>
<li>Black Mesa</li>
<ul>
<li>The Combine</li>
<li>The Resistance</li>
<li>The Player (Gordon Freeman)</li>
</ul>
</ul>
</div>
CSS CODE:
#navigation ul {
font-family:Arial;
list-style-type:none;
margin:0;
padding:0;
}
#navigation li {
font-family:Arial;
font-size:11px;
display:inline;
float:left;
background-color:#FFF;
position: relative;
}
#navigation a {
display:block;
width:60px;
background-color:#FFF;
}
#navigation ul li ul{
position: absolute;
top: 28px;
opocity: 0;
display: none;
}
#navigation ul li:hover ul{
opocity: 1;
display: block;
}
SEE DEMO http://jsfiddle.net/JentiDabhi/j50Lncva/

Related

Drop down menu hover effect

I have a CSS drop down menu with a hover effect. The a element is not taking up the full width of the li and I want it to use the whole width of the li. Anyone know what I can do to take up the whole width on hover? Codepen link below:
https://codepen.io/darenjm/pen/VWdjWb?editors=1100
<header>
<nav class="clearfix">
<ul>
<li>Home</li>
<li>Projects</li>
<li>Customers
<ul>
<li>Our Markets</li>
<li>Questionaire</li>
</ul>
</li>
<li>Employment
<ul>
<li>Work With Us</li>
<li>Open Positions</li>
<li>Apply</li>
</ul>
</li>
<li>Advantage</li>
<li>About Us
<ul>
<li>Colombian Railways History</li>
<li>Company History</li>
<li>Partners</li>
<li>Strategic Partners</li>
<li>Videos</li>
<li>News</li>
<li>FAQ</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
</header>
<div class="box">
</div>
body{
margin:0;
padding:0;
}
header{
background:#001d45;
width:100%;
}
nav{
background:#001d45;
width:85%;
margin:0 auto;
font-family: helvetica;
font-size: 80%;
}
nav ul{
display: flex;
justify-content:space-between;
padding:0;
margin:0;
transition:all .3s ease-in-out;
}
nav ul li{
list-style:none;
flex:1 1 0%;
}
nav ul li > ul{
background:#af2922;
flex-direction: column;
}
nav ul li > ul > li:hover a{
background:#cc534d;
}
nav ul li a{
display:inline-block;
color:#fff;
text-decoration:none;
padding:10px 20px;
}
nav ul li:hover > a{
background:#af2922;
}
nav > ul ul{
position:absolute;
visibility:hidden;
opacity:0;
}
nav ul li:hover > ul{
visibility:visible;
opacity:1;
}
nav > ul > li{
float:left;
}
.clearfix:after{
display:block;
content:"";
clear:both;
}
.box{
background:#d1d3d3;
height:100px;
}
position:relative; on the parent li and width:100% on the dropdown ul.
https://codepen.io/anon/pen/pwKRox?editors=1100

On hovering through main menu only my last submenu is displaying and remaining first three submenus are not displaying

HTML code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="menu3.css">
<title>Menu Bar</title>
</head>
<body>
<div id="nav">
<ul id="menu">
<li>home
<ul>
<li>Home2</li>
<li>Home3</li>
<li>Home4</li>
<li>Home5</li>
</ul>
</li>
<li>about us
<ul>
<li>About2</li>
<li>About3</li>
<li>About4</li>
<li>About5</li>
</ul>
</li>
<li>services</li>
<li>careers</li>
<li>contacts</li>
</ul>
</div>
</body>
</html>
CSS code:
#nav ul,#nav ul ul
{
padding:0;
margin:0;
}
#nav ul li,#nav ul ul li
{
list-style-type:none;
display:inline-block;
}
#nav ul li a
{
font-size:18px;
text-transform:uppercase;
text-decoration:none;
color:white;
background:black;
padding:5px;
display:inline-block;
}
#nav ul ul li a
{
font-size:18px;
text-transform:uppercase;
text-decoration:none;
color:white;
background-image:url('bg.jpg');
padding:5px;
display:inline-block;
}
#nav ul li
{
position:relative;
}
#nav ul li a:hover
{
background:grey;
}
#nav ul ul li
{
display:none;
position:absolute;
top:30px;
left:0px;
width:400px;
}
#nav ul ul li a
{
padding:15px;
}
#nav ul li:hover ul li
{
display:block;
}
You only see the last submenu item because they are all displayed at the same place because of the position:absolute rule.
You should use position:absolute with the whole submenu ul and not each li items.
ul {
margin:0;
padding:0;
list-style-type: none;
}
ul > li {
display:inline-block;
height:20px;
position:relative;
}
ul > li > ul {
position: absolute; top:20px;
display: none;
}
ul > li:hover > ul {
display: block;
}
<ul>
<li>Home
<ul>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</li>
<li>About
<ul>
<li>About</li>
<li>About</li>
<li>About</li>
<li>About</li>
<li>About</li>
</ul>
</li>
<li>Services
<ul>
<li>Services</li>
<li>Services</li>
<li>Services</li>
</ul>
</li>
</ul>

Center Align Navigation Bar

I'm having a hard time centering the navigation bar I've created.
HTML Code:
<div class="menu">
<ul>
<li>Home</li>
<li>About Mr. A</li>
<li>Intro
<ul>
<li>Daily Agenda</li>
<li>Handouts</li>
<li>Student Work</li>
</ul>
</li>
<li>Digital Design
<ul>
<li>Daily Agenda</li>
<li>Handouts</li>
<li>Student Work</li>
</ul>
</li>
<li>Web Design
<ul>
<li>Daily Agenda</li>
<li>Handouts</li>
<li>Student Work</li>
</ul>
</li>
<li>AP Computer Science
<ul>
<li>Daily Agenda</li>
<li>Handouts</li>
</ul>
</li>
<li>FBLA
<ul>
<li>Membership</li>
<li>Competitions</li>
<li>Conferences</li>
</ul>
</li>
</ul>
</div>
</body>
CSS Code:
html {
background-color:#2f2f4f;
}
div {
position:relative;
}
.menu {
position:absolute;
}
.menu ul {
padding:0;
margin:0;
line-height:30px;
}
.menu li {
position:relative;
float:left;
list-style:none;
border-bottom-style:solid;
border-top-style:solid;
border-color:#c5b358;
border-width:1px;
}
.menu ul ul {
position:absolute;
visibility:hidden;
padding:0;
margin:0;
}
.menu ul li a {
text-align:center;
font:"Georgia", serif;
font-size:18px;
color:#c5b358;
width:250px;
height:50px;
display:table-cell;
text-decoration:none;
vertical-align:middle;
}
.menu ul li:hover {
background-color:#2f4f2f;
text-decoration:none;
}
.menu ul li:hover ul {
visibility:visible;
z-index:1;
}
.menu ul li:hover ul li a {
background:#2f4f2f;
z-index:1;
border-bottom:none;
opacity:0.9;
text-decoration:none;
}
.menu ul li ul li:hover {
background:#2f4f2f;
opacity:0.8;
text-decoration:underline;
}
Nothing I do seems to work. I've tried several different options and can't find a solution. I'm fairly new to web design and it took me a while to get to this point.
I am expecting that I understood your question correctly. and on that basis I have modified your css code to get the desired result. please check the fiddle:https://jsfiddle.net/nileshmahaja/0k76hxc1/
Modified CSS
.menu ul {
text-align:center; /* Added */
}
.menu li {
/*float:left; Removed */
display:inline-block; /* Added */
}

Menu at different level visible simultaneously in CSS

I have this CSS:
Here is the example: Here is the example: http://jsfiddle.net/GrkkW/1/
#menu{
width:952px;
height:36px;
line-height:36px;
padding:0 0 0 32px;
background-image:url(/images/menu_bg.gif);
background-position:top left;
background-repeat:repeat-x;
position:relative;
z-index:100;
float:left;
}
#menu li{
display:inline;
list-style-type:none;
float:left;
position:relative;
z-index:100;
}
#menu li a{
font-family:Tahoma;
font-size:11px;
color:black;
text-decoration:none;
margin:0 15px;
padding:3px 8px;
display: block;
}
#menu li > ul
{
display: none;
line-height: 20px;
height: 20px;
}
#menu li:hover ul{
display: block;
position:absolute;
margin: -5px ;
}
#menu li:hover li{
float: none; }
#menu li:hover li a{
background-color: #0C325F;
border-bottom: 1px solid #0C325F;
color: #D2DEEC; }
#menu li li a:hover {
background-color: #8db3ff; }
and HTML is this:
<ul id="menu">
<li>Home</li>
<li>Item1
<ul>
<li>SubItem1
<ul>
<li>Sub1</li>
<li>Sub2</li>
</ul>
</li>
<li>SubItem2
<ul>
<li>Sub1</li>
<li>Sub2</li>
</ul>
</li>
</ul>
</li>
<li>Item2</li>
I'm using this to display a menu and submenu. What I want to achieve is that when an option from the second level is clicked then its submenu is visible. However currently when I click on menu, submenu at level 1 and level 2 are displayed simultaneously.
I'm wondering if somebody can help me with this and give some advise of the modifications I need to add to the CSS to achieve this?
Here is the example: http://jsfiddle.net/GrkkW/1/
html
<ul>
<li>Home</li>
<li>About
<ul>
<li>About-1</li>
<li>About-1</li>
<li>About-1
<ul>
<li>About-2</li>
<li>About-2</li>
<li>About-2
<ul>
<li>About-3</li>
<li>About-3</li>
<li>About-3
<ul>
<li>About-4</li>
<li>About-4</li>
<li>About-4</li>
<li>About-4</li>
</ul>
</li>
<li>About-3</li>
</ul>
</li>
<li>About-2</li>
</ul>
</li>
<li>About-1</li>
</ul>
</li>
<li>profile</li>
<li>Contact
<ul>
<li>contact-1</li>
<li>contact-1</li>
<li>contact-1</li>
<li>contact-1</li>
</ul>
</li>
</ul>
css
*{ padding:0px; margin:0px;}
ul{padding:0px; margin:0px; list-style:none; width:100%}
ul li{ float:left; padding-right:1px; position:relative;}
ul a{ text-decoration:none; display:table-cell; vertical-align:middle; text-align:center; color:#000; height:30px;
width:100px; background-color:#0CF; color:#fff;}
ul a:hover{ background-color:#36C;}
li > ul{ display:none; position:absolute; left:0px; top:100%;}
li:hover > ul{ display:block;}
li > ul li { padding:0px; padding-right:1px; padding-top:1px;}
li > ul li > ul{ top:0px; left:100%; padding-left:1px;}
li > ul li > ul li{width:100px;}
see this link
http://jsfiddle.net/bipin_kumar/DjvZ8/7/

wide spread sub menu

Actually I want my menu to be something like sub-menu in this website http://www.maxhealthcare.org.
Here is my HTML
<ul>
<li>Patient and Visitor Services</li>
<li>Hospital Network</li>
<li>Our Specialities
<ul>
<p><span>Eye Care</span></p>
<li>List of Services</li>
<li>Team that cares</li>
<p><span>Gasterology</span></p>
<li>List of Services</li>
<li>Team that cares</li>
<p><span>General Surgery</span></p>
<li>List of Services</li>
<li>Team that cares</li>
<p><span>Internal Medicine</span></p>
<li>List of Services</li>
<li>Team that cares</li>
</ul>
</li>
<li>Preventive Health Plans</li>
<li>For Medical Professionals</li>
</ul>
and CSS is;
#menu ul
{
margin:0px;
padding:11px 150px;
background-color:#013773;
float:left;
width:77.7%;
list-style:none;
}
#menu ul li
{
height:100%;
width:auto;
margin:0px 5px;
padding:12px;
cursor:pointer;
position:relative;
font-weight:bold;
color:#FFF;
font-size:15px;
}
#menu ul li:hover
{
background-color:#FFF;
color:#013773;
}
#menu ul li ul
{
display:none;
background-color:#FFF;
}
#menu ul li ul li
{
display:block;
width:90%;
position:relative;
color:inherit;
margin:0px;
font-weight:normal;
font-size:12px;
padding:0px;
}
#menu ul li:hover ul
{
display:block;
position:absolute;
margin:10px 0px;
padding:20px;
height:auto;
width:300px;
border-bottom:1px #CCC solid;
border-right:1px #CCC solid;
border-left:1px #CCC solid;
float:left;
}
#menu ul li:hover ul li
{
display:inline-block;
position:relative;
padding:10px;
font-weight:normal;
}
#menu ul li ul li:hover
{
font-weight:normal;
}
In the sub-menu i will expand my menu items as it is in the example website. Can some one tell me how to do this?