I have two pages of code. I want the first page's dropdown menu to look like the second page's dropdown menu. The second page is some code I copied and pasted from W3 Schools.
The problem is on the first page the drop down menu's width is the same as the navigation bar. I want to have a smaller width for the navigation bar and I can't figure out how why it is the same width of navigation bar.
First Page
ul {
margin: 0;
padding: 0;
background-color: green;
overflow: hidden;
list-style-type: none;
}
li {
float: left;
}
li a,
.dropbtn {
display: inline-block;
padding: 50px 100px;
text-decoration: none;
color: white;
}
li a:hover {
color: white;
background-color: #333;
}
.dropdown {
display: inline-block;
}
.dropcont {
display: none;
position: absolute;
background-color: #333 min-width:200px;
z-index: 1;
}
.dropcont a {
color: white;
padding: 12px 16px;
display: block;
text-align: left;
}
.dropdown:hover .dropcont {
display: block;
}
<ul>
<li> Home</li>
<li> Your Home</li>
<li>Home Sales</li>
<li class="dropdown">
Home profile
<div class="dropcont">
Home2
Home3
Home4
</div>
</li>
</ul>
Second Page
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
li.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;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
Here is the updated css :
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color:green;
}
li {
float: left;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color:#333;
}
li.dropdown {
display: inline-block;
}
.dropcont {
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;
}
.dropcont a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropcont a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropcont {
display: block;
}
<ul>
<li> Home</li>
<li> Your Home</li>
<li>Home Sales</li>
<li class="dropdown">
Home profile
<div class="dropcont">
Home2
Home3
Home4
</div>
</li>
</ul>
Related
I made a navigation bar in HTML and trying that the elements of the navigation bar are on the right site. However, the elements are on the right side but in the wrong direction. The direction of the elements is, Kontakt, Über uns, Klimawandel and Home. The direction I will is the reverse of this.
This is the HTML I wrote for the navigation bar:
nav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #96CB49;
}
li {
float: right;
border-right: 1px solid black;
}
.active {
background-color: #254a01;
}
li:last-child {
border-right: none;
}
li a:hover:not(.active) {
background-color: #254a01;
}
li a,
.dropbtn {
display: inline-block;
color: #F9FCEA;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: #254a01;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #96CB49;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: #F9FCEA;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #254a01;
}
.dropdown:hover .dropdown-content {
display: block;
}
<nav>
<li><a class="active" href="#home">Home</a></li>
<li class="dropdown">
Klimawandel
<div class="dropdown-content">
Der Klimawandel
Die Ursachen des Klimawandels
Die Auswirkungen des Klimawandels
</div>
</li>
<li>Über uns</li>
<li>Kontakt</li>
<li style="float:left"><a>Logo</a></li>
</nav>
The way to approach this is to make the container float right, and the items inside of it to float left, if you want to use floats for this purpose.
And since you are using float which will cause the element width to depend on its content, you will need to add a wrapper to your <nav> element, that will have the same background color, so that you achieve full width background visually. In my example below, I wrapped the list elements in the <ul>, and made <nav> be the top level container to add the background.
nav {
background-color: #96CB49;
overflow: hidden;
}
ul {
float: right;
list-style-type: none;
margin: 0;
padding: 0;
}
li {
float: left;
border-right: 1px solid black;
}
.active {
background-color: #254a01;
}
li:last-child {
border-right: none;
}
li a:hover:not(.active) {
background-color: #254a01;
}
li a,
.dropbtn {
display: inline-block;
color: #F9FCEA;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: #254a01;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #96CB49;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: #F9FCEA;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #254a01;
}
.dropdown:hover .dropdown-content {
display: block;
}
<nav>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li class="dropdown">
Klimawandel
<div class="dropdown-content">
Der Klimawandel
Die Ursachen des Klimawandels
Die Auswirkungen des Klimawandels
</div>
</li>
<li>Über uns</li>
<li>Kontakt</li>
<li style="float:left"><a>Logo</a></li>
</ul>
</nav>
However, today there are more effective approaches to doing layout in CSS, such as flex-box which would make your task very easy:
ul {
background: green;
display: flex;
list-style: none;
justify-content: end;
}
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
.header{
display:inline-block;
vertical-align: top;
list-style-type: none;
}
.header .dropbtn {
font-size: 16px;
border: none;
color: #111;
padding: 14px 16px;
margin: 0;
background: inherit;
}
.header:hover .dropbtn {
background-color: #00b5cc;
}
.dropdown-content {
list-style-type: none;
margin: 0px;
padding: 0px;
display: none;
list-style-type: 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 li a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content li a:hover {
background-color: #ddd;
}
.header:hover .dropdown-content {
display: block;
}
.drop-button {
font-size: 16px;
border: none;
color: #111;
padding: 14px 16px;
margin: 0;
background: inherit;
}
.sub-menu{
list-style-type: none;
display:none;
}
.dropdown-content:hover .submenu{
background-color: red;
}
<ul class="header">
<li>
<a class="dropbtn ">
Apparel
</a>
<ul class="dropdown-content">
<li>
<a>Girls
<ul class="sub-menu">
<li><a>Shoes</a></li>
<li><a>Pants</a></li>
<li><a>Skirts</a></li>
<li><a>Tops</a></li>
</ul>
</a>
</li>
</ul>
</li>
</ul>
I'm new to css and want to create a navigation bar.When I click on the Apparel section, the Girl section comes. But when I click on the girl section the sub items are not displayed.I want to display the menu items when I hover on Girl section Can someone please help me on this. I'm not able to figure it out.
You forget dashed into submenu class now .sub-menu also forget display: block; Please Try following Code for good design.
.header{
display:inline-block;
vertical-align: top;
list-style-type: none;
}
.header ul {
padding: 0;
}
.header .dropbtn {
font-size: 16px;
border: none;
color: #111;
padding: 14px 16px;
margin: 0;
background: inherit;
display: inline-block;
}
.header:hover .dropbtn {
background-color: #00b5cc;
}
.dropdown-content {
list-style-type: none;
margin: 0px;
padding: 0px;
display: none;
list-style-type: 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 li a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content li a:hover {
background-color: #ddd;
}
.header:hover .dropdown-content {
display: block;
}
.drop-button {
font-size: 16px;
border: none;
color: #111;
padding: 14px 16px;
margin: 0;
background: inherit;
}
.sub-menu{
list-style-type: none;
display:none;
}
.dropdown-content:hover .sub-menu{
background-color: red;
display: block;
}
<ul class="header">
<li>
<a class="dropbtn ">Apparel</a>
<ul class="dropdown-content">
<li>
<a>Girls</a>
<ul class="sub-menu">
<li><a>Shoes</a></li>
<li><a>Pants</a></li>
<li><a>Skirts</a></li>
<li><a>Tops</a></li>
</ul>
</li>
</ul>
</li>
</ul>
Please Try This Code
.header{
display:inline-block;
vertical-align: top;
list-style-type: none;
}
.header .dropbtn {
font-size: 16px;
border: none;
color: #111;
padding: 14px 16px;
margin: 0;
background: inherit;
}
.header:hover .dropbtn {
background-color: #00b5cc;
}
.dropdown-content {
list-style-type: none;
margin: 0px;
padding: 0px;
display: none;
list-style-type: 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 li a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content li a:hover {
background-color: #ddd;
}
.header:hover .dropdown-content {
display: block;
}
.drop-button {
font-size: 16px;
border: none;
color: #111;
padding: 14px 16px;
margin: 0;
background: inherit;
}
.sub-menu{
list-style-type: none;
display:none;
}
.dropdown-content:hover .submenu{
background-color: red;
}
.dropdown-content li:hover .sub-menu { display: block; }
HTML:-
<ul class="header">
<li>
<a class="dropbtn ">
Apparel
</a>
<ul class="dropdown-content">
<li>
<a>Girls</a>
<ul class="sub-menu">
<li><a>Shoes</a></li>
<li><a>Pants</a></li>
<li><a>Skirts</a></li>
<li><a>Tops</a></li>
</ul>
</li>
</ul>
</li>
</ul>
I have added a second CSS dropdown menu on my page and it appears on the left of the page. The first one has text center-aligned and everything works great. I added a second one for when an admin is logged in for admin operations.
The text is right-aligned in this menu and the dropdown appears on the left of the screen.
Here is a jsfiddle - https://jsfiddle.net/q0nsrdgo/
Html
<div id="loginbar">
<ul>
<li>Welcome Xenarious | </li><li>Logout |</li>
<li class="dropdown">Admin Controls
<div class="dropdown-content">
Add Customer
Edit Customer
Add Product
Update Product
</div>
|</li> <li>Orders</li>
</ul>
</div>
<header>
<img src="../common/techtitan-title.png">
</header>
<nav>
<ul>
<li>Home</li>
<li class="dropdown"><a href="products.html"
class="dropbtn">Products</a>
<div class="dropdown-content">
<!--Desktops-->
Notebooks
Tablets
Smartphones
</div>
</li>
<li>Services</li>
<li>About Us</li>
<li>Feedback</li>
</ul>
Css
#loginbar {
text-align: right;
background-color: #1D6000;
color: #FFD700;
font-size: 10pt;
z-index: 2;
overflow: auto;
width: 100%;
display: block;
margin: auto;
}
header {
background-color: #1D6000;
margin-bottom: 0;
text-align: center;
z-index: 1;
}
header img {
margin: auto;
text-align:center;
}
nav {
overflow: auto;
width: 100%;
display: block;
margin: auto;
margin-bottom: 20px;
background-color: #1D6000;
text-align: center;
box-shadow: 0px 8px 16px 0px rgba(5,5,5,0.5);
border-bottom: 3px ridge #FFD700;
font-size: 14pt;
}
nav ul, #loginbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #1D6000;
}
nav li, #loginbar li {
/*float: left*/
display: inline;
margin: 0;
padding: 0;
}
li a, .dropbtn {
display: inline-block;
color: #FFD700;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #FFD700;
color: #1D6000;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #1D6000;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(5,5,5,0.5);
/*z-index: 1;*/
}
.dropdown-content a {
color: #FFD700;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #FFD700;
color: #1D6000;
}
.dropdown:hover .dropdown-content {
display: block;
}
Update your CSS as follows -
nav li, #loginbar li {
display: inline-block;
margin: 0;
padding: 0;
}
Since only your .dropdown class was set to inline-block, the dropdown wasn't being displayed as required.
The hover div of items should stay stable but it doesn't, when I move cursor from hover a tag it goes away
#ul1 {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
width: 100px;
}
li {
width: 100px;
display: block;
}
li a,
.dropbtn {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: orange;
}
li.dropdown {
display: block;
}
li.dropdown2 {
display: block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: green;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content2 {
display: none;
position: absolute;
background-color: green;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
color: white;
padding: 20px 20px;
text-decoration: none;
display: block;
text-align: center;
}
.dropdown-content2 a {
position: relative;
top: 100px;
left: 500px;
color: white;
background-color: red;
padding: 20px 20px;
text-decoration: none;
display: block;
text-align: center;
}
.dropdown-content a:hover {
background-color: pink;
}
dropdown-content2 a:hover {
background-color: pink;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown2:hover .dropdown-content2 {
display: block;
}
<ul id="ul1">
<li> Home </li>
<li> Services </li>
<li class="dropdown">
Products
<div class="dropdown-content">
<ul id="ul2">
<li class="dropdown2"> Mobiles
<div class="dropdown-content2">
Mobile1
Mobile2
Mobile3
Mobile4
Mobile5
</div>
</li>
<li> Televisions </li>
<li> Microwave </li>
<li> Clothing </li>
<li> Footware </li>
</ul>
</div>
</li>
</ul>
I have tried many things to deal with this like scaling padding to max or increasing width and height but nothing works.
The div goes away as soon as I move my cursor for hovered item
I removed position:absolute from the dropdown <a> tags and fixed the positioning of each dropdown level. This resolves the positioning of submenus in relation to the parent menu.
The hover mechanism works well with the arrangement of the menus. When you hover out, the menus disappear, which is standard behavior for drop-down menus. If you wish to leave the menu visible on hover out, you will need to define a javascript function that controls menu visibility, for instance, hiding when clicking on a menu item.
#ul1 {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
width: 160px;
}
li {
width: 160px;
display: block;
}
li a,
.dropbtn {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: orange;
}
li.dropdown {
display: block;
}
li.dropdown2 {
display: block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: green;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
left:166px;
margin-top:-46px;
}
.dropdown-content2 {
display: none;
position: absolute;
background-color: green;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
left:160px;
margin-top:-46px;
}
.dropdown-content a {
color: white;
/*padding: 20px 20px;*/
text-decoration: none;
display: block;
text-align: center;
}
.dropdown-content2 a {
color: white;
background-color: red;
/*padding: 20px 20px;*/
text-decoration: none;
display: block;
text-align: center;
}
.dropdown-content a:hover {
background-color: pink;
}
dropdown-content2 a:hover {
background-color: pink;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown2:hover .dropdown-content2 {
display: block;
}
#ul2 {padding:0px;}
<ul id="ul1">
<li> Home </li>
<li> Services </li>
<li class="dropdown">
Products
<div class="dropdown-content">
<ul id="ul2">
<li class="dropdown2" style="position:relative;"> Mobiles
<div class="dropdown-content2">
Mobile1
Mobile2
Mobile3
Mobile4
Mobile5
</div>
</li>
<li> Televisions </li>
<li> Microwave </li>
<li> Clothing </li>
<li> Footware </li>
</ul>
</div>
</li>
</ul>
I am new to CSS and I am making a navigation bar. Currently, my navigation bar is situated on the left and I would like to move the whole bar to the middle. How can I do that?
Below are the codes. Thanks in advance!
/* Navigation bar */
#navigation_bar {
list-style-type: none;
margin: 0;
position: fixed;
background-color: #333;
top: 0;
Left: 0;
width: 100%;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active), .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
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;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.active {
background-color: #4CAF50;
}
<!--Navigation bar-->
<ul id="navigation_bar">
<li><a class="active" href="navigation_bar/home.html">Home.</a></li>
<li>Promotion.</li>
<!--drop down menu-->
<li class="dropdown">Hot Products.
<div class="dropdown-content">
<a herf="navigation_bar/sub_menu/sandwiches.html">Sandwiches</a>
<a herf="navigation_bar/sub_menu/burger.html">Burger</a>
<a herf="navigation_bar/sub_menu/rice.html">Rice</a>
<a herf="navigation_bar/sub_menu/noodles.html">Noddles</a>
</div>
</li>
<!--Back to normal-->
<li>Cold Products.</li>
<li>Snacks.</li>
<li>About Us.</li>
<li>Contact Us.</li>
</ul>
Update your #navigation_bar with
#navigation_bar {
display: flex;
align-items: center;
justify-content: center;
padding: 0;
}
Here is the working Demo
#navigation_bar {
list-style-type: none;
margin: 0;
position: fixed;
background-color: #333;
top: 0;
Left: 0;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active), .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
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;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.active {
background-color: #4CAF50;
}
<ul id="navigation_bar">
<li><a class="active" href="navigation_bar/home.html">Home.</a></li>
<li>Promotion.</li>
<!--drop down menu-->
<li class="dropdown">Hot Products.
<div class="dropdown-content">
<a herf="navigation_bar/sub_menu/sandwiches.html">Sandwiches</a>
<a herf="navigation_bar/sub_menu/burger.html">Burger</a>
<a herf="navigation_bar/sub_menu/rice.html">Rice</a>
<a herf="navigation_bar/sub_menu/noodles.html">Noddles</a>
</div>
</li>
<!--Back to normal-->
<li>Cold Products.</li>
<li>Snacks.</li>
<li>About Us.</li>
<li>Contact Us.</li>
</ul>
By changing a bit of your CSS code you can easily do that as:
#navigation_bar {
text-align: center;
}
#navigation_bar li {
float: none;
display: inline-block;
vertical-align: top;
}
li .dropdown-content a:hover:not(.active) {
background: #d0d0d0; /* for altering the hover effect on submenus */
}
I have also created a JSFiddle.
Change navigation_bar css to :
#navigation_bar {
list-style-type: none;
margin: 0 auto;
position: fixed;
background-color: #333;
top: 0;
width: auto;
display: block;
float: left;
}
Try this
#navigation_bar{ text-align: center;} /*add align center */
li {display: inline; /* float:left; */} /* remove float left here and add display inline */