I want to make a vertical dropdown menu with the following structure(for each "button" a different "dropdown" div).The idea is when I hover "button", the "dropdown" to appear over the "button" with the same size ( the "dropdown" shrinks to the "button" size with equal heights "subnav1" ) or to appear left to the "dropdown" ( again with equal heights "subnav1" ). The problem, however , is that I can't make the "dropdown" display:blocks on "button" hover and ,in the second case, the "button" to be hovered while selecting "subnav1" element.
HTML:
<div class="nav">
<ul class="nav1">
<li class="button"><div class="subnav">BUTTON1</div></li>
<li class="button"><div class="subnav">BUTTON2</div></li>
<li class="button"><div class="subnav">BUTTON3</div><li>
<li class="button"><div class="subnav">BUTTON4</div></li>
<li class="button"><div class="subnav">BUTTON5</div></li>
</ul></div>
<div class="dropdown">
SUBBUTTON1
SUBBUTTON1
SUBBUTTON1
</div>
CSS:
.nav {
width: 180px;
padding: 1vm;
float:left;
height:450px;
position:relative;
text-align:center;
background-color:#a56d3b;
}
ul{
list-style:none;
margin: 0;
padding: 0;
text-align:right;
}
.nav a{
text-decoration:none;
}
.subnav{
font-weight:bold;
text-align:center;
color:#FCFBE3;
font-family: 'PT Serif', serif;
font-size:1.3em;
display:block;
}
.button{
border-bottom:1PX DOTTED #FFECBA;
border-right:1PX DOTTED #FFECBA;
height:90px;
width:100%;
padding:2em 0 0 0 ;
cursor:pointer;
}
.button:last-child{
border-bottom:0px;
}
.button:hover{
background-color:rgba(0,0,0,0.5);
}
.dropdown{
height:89px;
float:left;
width:auto;
min-width:100px;
text-align:center;
margin:0 180px;
position:absolute;
cursor:pointer;
border:1px solid black;
}
.subnav1{
height:30px;
width:180px;
padding:5px 0;
color:black;
font-family: 'PT Serif', serif;
font-size:1em;
color:white;
text-decoration:none;
background-color:#D8A06E;
DISPLAY:BLOCK;
}
.subnav1:hover{
background-color:rgba(0,0,0,0.5);
}
.nav.nav1 .button:hover .dropdown{
display:block;
}
I think this is how you want it to be. Let me know if this is not the result you are looking for
.dropbtn {
background-color: #E61094;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.nav {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 115px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a{
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #9E1192
}
.nav:hover .dropdown-content {
display: block;
}
.nav:hover .dropbtn {
background-color: #F576EA;
}
<div class="nav">
<button class="dropbtn">Dropdown 1</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
<div class="nav">
<butoon class="dropbtn">Dropdown 2</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
<div class="nav">
<butoon class="dropbtn">Dropdown 3</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
<div class="nav">
<butoon class="dropbtn">Dropdown 4</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
<div class="nav">
<butoon class="dropbtn">Dropdown 5</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
you are using absolutely wrong markup of the HTML navigation, correct and valid structure is:
<nav class="nav">
<ul>
<li>
1st Level
<ul>
<li>2nd Level</li>
<li>2nd Level</li>
<li>2nd Level</li>
</ul>
</li>
<li>
1st Level
<ul>
<li>2nd Level</li>
<li>2nd Level</li>
<li>2nd Level</li>
</ul>
</li>
<li>
1st Level
<ul>
<li>2nd Level</li>
<li>2nd Level</li>
<li>2nd Level</li>
</ul>
</li>
</ul>
</nav>
and then it is easy as a pie, just styling:
.nav {
display: block;
font-family: sans-serif;
}
.nav ul {
padding: 0;
list-style: none;
}
.nav ul li {
display: inline-block;
position: relative;
white-space: nowrap;
}
.nav ul li a {
display: block;
padding: 8px 16px;
text-decoration: none;
color: #000000;
}
.nav ul li ul {
display: none;
position: absolute;
top: 100%;
left: 0;
border: 1px solid #000000;
}
.nav ul li ul li {
display: block;
}
.nav ul li:hover ul {
display: block;
}
See this fiddle https://jsfiddle.net/vtt0qnpt/
Enjoy
Related
In CSS, one submenu (Link 1, Link2, Link3) is available in this link. https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_dropdown_navbar
I'm trying to create submenu (link 4.1, link4.2) under the above submenu as shown in below image.
how can I get this submenu. .
I've tried below. But this is overlapping. I'm newbie to CSS. please share your ideas
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<div class="dropdown">Link 1</div>
<div class="dropdown-content">
Link 2
Link 3
</div>
Link 2
Link 3
</div>
</div>
This can be done by simple html/css work so just to give an idea here is a simple example:
ul {
list-style: none;
padding: 0;
margin: 0;
background: #000;
}
ul li {
display: block;
position: relative;
float: left;
background: #000;
}
li ul {
display: none;
}
ul li a {
display: block;
padding: 1em;
text-decoration: none;
white-space: nowrap;
color: #fff;
}
ul li a:hover {
background: #001;
}
li:hover>ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
}
li:hover a {
background: #000;
}
li:hover li a:hover {
background: #000;
}
ul ul ul {
left: 100%;
top: 0;
}
<ul>
<li>Home</li>
<li>First level Menu
<ul>
<li>Second Level</li>
<li>Second Level with third level
<ul>
<li>Third level</li>
<li>Third level</li>
</ul>
</li>
</ul>
</li>
</ul>
I have been checking on other questions related to mine, and most of the time "text-align:center" is suggested. However, even if I implement it to my code, the navbar still doesn't center... Here's my code:
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
}
#nav {
width: auto;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
.navli {
float:left;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index:1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</div>
Thanks for any suggestions in advance!
remove float:left on your navli class ang set it to display:inline-block and add text-align:center on your #nav, and to center your #navigation add margin:0 auto;
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
margin : 0 auto;
}
#nav {
width: auto;
text-align : center;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
.navli {
/* float:left;*/
display : inline-block;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index:1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</div>
You've used float:left on the li, which aligns them to left. I've removed it and added text-align:center to the ul
Is this what you're looking for?
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
}
#nav {
width: auto;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
text-align:center;
}
.navli {
display:inline-block;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index:1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</div>
Screenshot of the header that's needing this work:
As you can see, the menu is below the logo. I was wanting the menu beside it, to the right of the logo. I don't know if it's possible, but if it is, I could use some help, please.
Here's the code for everything, separated for your convenience.
The menu and logo in their divs:
<div id="wrapper">
<div id="body-wrapper">
<div class="head">
<div class="head-wrapper">
<div class="logo">
<img src="http://i.imgur.com/sDnntOE.png">
<ul>
<li>Home</li>
<li>About Us
<ul>
<li>The Team</li>
<li>History</li>
</ul>
</li>
<li>Products
<ul>
<li>Chaotix Browser</li>
<li>Useful Beta 1.7.5</li>
<li>Chaotix Cleaner 1.4</li>
<li>Forum</li>
<li>CDev</li>
<li>Infinite-PVP</li>
<li>Ulta-Craft</li>
</ul>
</li>
<li>Contact Us
<ul>
<li>E-Mail</li>
<li>News Letter</li>
<li>Social Mediar</li>
</ul>
</li>
<li>Divisions
<ul>
<li>Gaming</li>
<li>Films</li>
</ul>
</li>
<li>Chaotix! Forum</li>
<li>Partnerships
<ul>
<li>GameFanShop</li>
<li>Forumotion</li>
</ul>
</li>
</ul>
The CSS:
<style>
*{
background-image:url('http://i.imgur.com/0u7sBsT.png');
background-color:#999999;
font-family:Tahoma;color:white;
}
div.head{
text-align:Center;
padding-top:10px;
}
div.body{
padding-top:100px;
padding-left:300px;
padding-right:300px;
text-align:center;
}
div.logo{
float:left;
}
a{
color:white;
}
a:hover{
color:gray;
}
/* Main menu
------------------------------------------*/
ul{
font-family: Lato,Tahoma,Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
padding-left:25px;
}
ul li{
display: block;
position: relative;
float: left;
}
li ul{
display: none;
}
ul li a{
display: block;
text-decoration: none;
color: #FFFFFF;
border-top: 1px solid #000000;
padding: 5px 15px 5px 15px;
background: #000000;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover{
background: #999999;
}
li:hover ul{
display: block;
position: absolute;
}
li:hover li{
float: none;
font-size: 11px;
}
li:hover a{
background: #000000;
}
li:hover li a:hover{
background: #999999;
}
</style>
Add your css
.logo img
{
float:left;
}
.logo ul
{
float:left;
}
It working ok. Hope this help!
I am working on this code its a type of menu, which sticks to the bottom of the screen. i wanted drop up menu on hovering over various items. but my problem is the submenus are increasing towards downwards where they should be increasing upwards.... here is my code please help
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>
Dropdown menu
</title>
<style type="text/css">
body{
padding: 3em;
}
#menu ul {
font: 12px georgia;
list-style-type:none;
position:fixed;
left:0px;
bottom:0px;
height:50px;
width:100%;
}
#menu a {
display: block;
text-decoration: none;
color: #3B5330;
}
#menu a:hover {
background:#E3E4FA;
}
#menu ul li ul li {
color: #B0BD97;
padding-top: 3px;
padding-bottom:3px;
padding-left: 3px;
padding-right: 3px;
background:#E3E4FA;
}
#menu ul li ul li a {
font: 11px arial;
font-weight:normal;
font-variant: small-caps;
padding-top:3px;
padding-bottom:3px;
}
#menu ul li {
float: left;
font-weight: bold;
border-top: solid 1px #283923;
border-bottom: solid 1px #283923;
background:#E3E4FA;
}
#menu ul li a {
font-weight: bold;
padding: 15px 10px;
padding-bottom:13px;
}
#menu li{
position:relative;
float:left;
}
#menu ul li ul, #menu:hover ul li ul{
display:none;
list-style-type:none;
padding-bottom:0px;
}
#menu:hover ul, #menu:hover ul li:hover ul{
display:block;
}
#menu:hover ul li:hover ul {
position: absolute;
margin-top: 1px;
font: 10px;
}
#menu>ul>li:hover>ul {
bottom:100%;
border-bottom: 1px solid transparent
}
</style>
</head>
<body>
<div id="menu">
<ul>
<li>
<center>
<a href="X">
Home
</a>
</center>
<ul>
<li>
<a href="#">
About Us
</a>
</li>
<li>
<a href="#">
Disclaimer
</a>
</li>
</ul>
</li>
<li>
<center>
<a href="#">
Practice Areas
</a>
</center>
<ul>
<li>
<a href="#">
Civil Law
</a>
</li>
<li>
<a href="#">
Criminal Law
</a>
</li>
</ul>
</li>
<li>
<a href="#">
Family Law
</a>
<ul>
<li>
<a href="#">
Joomla
</a>
</li>
<li>
<a href="#">
Drupal
</a>
</li>
<li>
<a href="#">
Wordpress
</a>
</li>
<li>
<a href="#">
Joomla
</a>
</li>
<li>
<a href="#">
Drupal
</a>
</li>
<li>
<a href="#">
Wordpress
</a>
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
Updated : Demo
#navigation {
width: 980px;
height: 38px;
margin-top:100px;
}
#navigation li {
float: left;
position: relative;
width:100px;
border:1px solid red;
} #navigation li:hover { background: silver; }
#navigation li a {
text-transform: uppercase;
color: white;
padding: 13px 33px;
line-height: 38px;
font-size: 11px;
}
#navigation li a:hover { text-decoration: none; }
#navigation li ul {
position: absolute;
display:none;
z-index: 1000;
min-width: 100%;
left:-1px;
}
#navigation li:hover ul {
bottom:20px;
display:block;
background:#eee;
}
#navigation li ul li {
background: none;
width: 100%;
}
#navigation li ul li:hover {
background: none;
background-color: #2a51b5;
}
#navigation li ul li a {
text-transform: uppercase;
color: white;
padding: 8px 10px;
line-height: 28px;
width: 100%;
}
<ul id="navigation">
<li>1</li>
<li>2
<ul>
<li>2.1</li>
<li>2.2</li>
<li>2.3</li>
</ul>
</li>
<li>3</li>
<li>4</li>
</ul>
I have a menu with tabs, and on hover of a tab a list of things appear at the bottom of the tab. Then, I wanted to do that the list of thing go down with a transition( before it was display:block). My problem is that when the transition will start, the top of the list must be a certain multiplication ( the width of a tab * the number of tabs ). But I don't want any javascript.
Is there a way to do that ?
it is my sample of css dropdown menu: i hope be useful:
in HTML, and CSS:
#menu, #menu ul
{
list-style: none;
border: 1px solid #000;
background-color: #ecffec;
padding: 0 0 26px 0;
margin: 0;
}
#menu li
{
float: left;
margin-right: 3px;
border: 1px solid #ecffec;
position: relative;
}
#menu ul
{
position: absolute;
top: 25px;
left: -1px;
width: 200px;
padding: 0;
display: none;
}
#menu ul li
{
float: none;
margin: 0;
padding: 0;
line-height: 15px;
}
#menu a:link, #menu a:visited
{
display: block;
font-family: Tahoma;
font-size: 0.75em;
font-weight: bold;
text-align: left;
text-decoration: none;
color: #000;
padding: 5px;
}
#menu li:hover
{
background-color: #ffd98a;
border: 1px solid #000;
}
#menu li:hover ul
{
display: block;
}
<ul id="menu">
<li>Programming Language
<ul>
<li>Java</li>
<li>PHP</li>
<li>Python</li>
<li>Asp Classic</li>
<li>ASP.NET</li>
<li>javascript</li>
<li>Perl</li>
</ul>
</li>
<li>Database
<ul>
<li>SQL Server 2005</li>
<li>Oracle</li>
<li>MySQL</li>
<li>DB2</li>
</ul>
</li>
<li>Help</li>
</ul>
Have you seen https://codepen.io/markcaron/pen/wdVmpB?
html part
<h2>Checkbox version</h2>
<div class="dropdown">
<input type="checkbox" id="my-dropdown" value="" name="my-checkbox">
<label for="my-dropdown"
data-toggle="dropdown">
Choose one
</label>
<ul>
<li>Coffee</li>
<li>Coverage</li>
<li>Covfefe</li>
</ul>
</div>
<h2>Anchor version</h2>
<div class="dropdown">
<ul id="my-dropdown2">
<li>Coffee</li>
<li>Coverage</li>
<li>Covfefe</li>
</ul>
<a href="#my-dropdown2"
aria-controls="my-dropdown2"
role="button"
data-toggle="dropdown"
id="my-dropdown2-btn">
Choose one
</a>
<a href="#my-dropdown2-btn"
aria-controls="my-dropdown2"
role="button"
data-toggle="dropdown"
class="close">
Close dropdown
</a>
</div>
css part
better to see the link above!
check it out!