I have been trying to center this dropdown navigation, but I just cant get it right. Does anyone here have an idea about why I can't do it? I have a feeling it has something to do with the floating of the first li elements, but I am not sure.
JSFiddle: https://jsfiddle.net/21e7p8Lx/
ul#dropdown {
list-style: none;
}
ul#dropdown li {
float: left;
background: darkgrey;
}
ul#dropdown li a {
display: inline-block;
padding: 20px 40px;
color: black;
text-decoration: none;
text-transform: uppercase;
font-size: 20px;
}
ul#dropdown li:hover {
background: grey;
}
ul#dropdown > li:not(:last-child) {
border-right: 1px solid white;
}
ul#dropdown li ul {
list-style: none;
display: none;
position: absolute;
margin-top: 0px;
}
ul#dropdown li:hover ul {
display: block;
}
ul#dropdown li ul li {
float: none;
border: none;
border-top: 1px solid white;
}
<nav>
<ul id="dropdown">
<li>Test1</li>
<li>Test2</li>
<li>
Test3
<ul>
<li>DropdownTest1</li>
<li>DropdownTest2</li>
<li>DropdownTest3</li>
</ul>
</li>
<li>Test4</li>
<li>
Test5
<ul>
<li>DropdownTest1</li>
<li>DropdownTeasdadst2</li>
<li>DropdownTest3</li>
</ul>
</li>
</ul>
</nav>
Add this CSS:
ul#dropdown {
position: relative;
left: 50%;
transform: translateX(-50%);
display: inline-block; //cause #dropdown to "shrink-to-fit"
}
The first three styles are based on a popular method for vertical centering, found at http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/. In this case, I've changed so we can horizontally center.
The last style is needed, because block elements take up 100% by default, but inline-block elements will shrink-to-fit their contents.
Updated Fiddle
Related
I am a beginner to web development, and I am trying to make a dropdown menu.
The problem is when I hover on particular element, it consumes more than the expected space.
I want it to appear below the "shop" element. I do not understand where I am going wrong.
.nav {
width: 100%;
float: right;
}
.nav ul {
/* it edits the list, list-style: none; removes the discs from the list items */
float: right;
list-style-type: none;
display: block;
text-align: right;
}
.nav ul li {
display: inline-block;
margin: 20px 40px;
padding: 0 10px 0 10px;
text-align: center;
position: relative;
border: 2px solid gold;
}
.nav ul li a {
/* edits the links- text-decoration: none; removes the underline others are obvious*/
color: #000000;
text-decoration: none;
display: block;
}
.nav ul li ul li {
/* navigation sub-options disappear when not hovered */
display: none;
margin: 0;
padding: 0;
border: 2px solid greenyellow;
}
.nav ul li:hover ul li {
/* navigation options appear when hover on elements */
display: block;
}
<div class="nav">
<ul>
<li>Home</li>
<li>Shop
<ul>
<li>Products</li>
<li>Membership</li>
</ul>
</li>
<li>Blog</li>
<li>News</li>
<li>Activity</li>
<li>Contact Us</li>
</ul>
</div>
Set position: relative on shop-link and position: absolute on dropdown. Then align dropdown with top, left, bottom, transform what would you like.
With transform it would look like this:
.link {
position: relative;
}
.dropdown {
position: absolute;
bottom: 0;
left: 0;
transform: translateY(100%)
}
I think the issue is with the way you organized these elements. Personally, when I make drop down menus, I use <button> for each root of the drop down menu. It makes styling everything much easier.
Then, what I do is I put the main text in an <h2> or <h3>, and style that how I want the main part of the drop down to look. Everything inside of the drop down can be styled using the <button> class' settings. Here's how I modified your code to get what I assumed your looking for.
CSS Styling:
.nav2 a {
color: #000000;
text-decoration: none;
display: block;
}
.nav2 button {
margin: 20px 40px;
padding: 0 10px 0 10px;
border: 0px;
/* change this to the color you want the background of your website to be */
background-color: white;
border: 2px solid gold;
font-size: 0px;
}
.nav2 button:hover {
display: inline-block;
margin: 20px 40px;
padding: 0 10px 0 10px;
text-align: center;
position: relative;
background-color: white;
border: 2px solid greenyellow;
/* change this to the color you want the background of your website to be */
background-color: white;
font-size: 16px;
}
h2 {
color: #000000;
text-decoration: none;
font-size: 16px;
font-weight: normal;
}
And then the HTML body:
<div class="nav2">
<button>
<h2>Home</h2>
</button>
<button>
<h2>Shop</h2>
<br>Products
<br>Membership
</button>
<button>
<h2>Blog</h2>
</button>
<button>
<h2>News</h2>
</button>
<button>
<h2>Activity</h2>
</button>
<button>
<h2>Contact Us</h2>
</button>
</div>
The end result looked like this
I hope my response was helpful!!
Your CSS is a bit messy, but to get it working add the following:
/* sub-nav option list */
.nav > ul > li > ul {
position: absolute;
margin-top: 1px; /* removes border intersection, can't be too large otherwise a gap will remove hover */
left: -55px;
}
position: absolute "removes" the element from the container so it is not contained in your parent's border. This will allow us to use the left, right, bottom, top CSS properties to position the sub-nav.
margin-top is used here to remove the intersection of shop and the sub-nav. However, you should be careful increasing this value greater than 1-2px since it will create empty space and hovering on the elements is required for your sub-nav to show.
Here is the working snippet:
.nav {
width: 100%;
float: right;
}
.nav ul {
/* it edits the list, list-style: none; removes the discs from the list items */
float: right;
list-style-type: none;
display: block;
text-align: right;
}
.nav ul li {
display: inline-block;
margin: 20px 40px;
padding: 0 10px 0 10px;
text-align: center;
position: relative;
border: 2px solid gold;
}
.nav ul li a {
/* edits the links- text-decoration: none; removes the underline others are obvious*/
color: #000000;
text-decoration: none;
display: block;
}
/* sub-nav option list */
.nav > ul > li > ul {
position: absolute;
margin-top: 1px; /* removes border intersection, can't be too large otherwise a gap will remove hover */
left: -55px;
}
.nav ul li ul li {
/* navigation sub-options disappear when not hovered */
display: none;
margin: 0;
padding: 0;
border: 2px solid greenyellow;
}
.nav ul li:hover ul li {
/* navigation options appear when hover on elements */
display: block;
}
<div class="nav">
<ul>
<li>Home</li>
<li>Shop
<ul>
<li>Products</li>
<li>Membership</li>
</ul>
</li>
<li>Blog</li>
<li>News</li>
<li>Activity</li>
<li>Contact Us</li>
</ul>
</div>
Position docs for a better explanation of absolute: https://developer.mozilla.org/en-US/docs/Web/CSS/position
Here You have:
.nav{
position: relative;
display: flex;
justify-content: flex-end;
}
.nav ul{
display: flex;
list-style-type: none;
margin: 0;
padding: 0;
}
.nav ul li{
background-color: gold;
border: 1px solid gold;
color: #FFF;
}
.nav ul li:hover{
background-color: #FFF;
color: gold;
}
.nav ul li a{
padding: 1rem 2rem;
color: inherit;
text-decoration: none;
font-family: Verdana;
}
.nav ul li ul {
/* navigation sub-options disappear when not hovered */
display: none;
opacity: 0;
visibility: hidden;
position: absolute;
margin: 0;
padding: 0;
border: 2px solid greenyellow;
}
.nav ul li:hover ul {
/* navigation options appear when hover on elements */
display: flex;
opacity: 1;
visibility: visible;
}
<div class="nav">
<ul>
<li>Home</li>
<li>Shop
<ul>
<li>Products</li>
<li>Membership</li>
</ul>
</li>
<li>Blog</li>
<li>News</li>
<li>Activity</li>
<li>Contact US</li>
</ul>
</div>
I have a code like this, I want hover work when I move mouse to <ul> <li> tag will drop down <ul> tag:
body {
background: hotpink;
}
.menu>ul>li {
display: inline-block;
position: relative;
}
.menu>ul>li>a {
display: inline-block;
text-decoration: none;
font-variant: small-caps;
font-size: larger;
color: white;
padding: 0 10px;
line-height: 40px;
}
.menu>li>a:hover {
color: yellow;
}
.menu>ul ul {
position: absolute;
display: none;
padding: 0px;
margin: 0px;
list-style: none;
border-radius: 3px;
width: 200px;
background-color: lightgray;
box-shadow: 0 0 2px green;
}
.menu>ul li:hover>ul {
display: block;
}
.menu>ul li:hover>ul {
display: block;
}
<nav class="menu">
<ul>
<li>Trang chủ</li>
<li>Giới thiệu</li>
<ul>
<li>Giới thiệu chung</li>
<li>Cơ cấu tổ chức</li>
</ul>
<li>Tin tức</li>
<li>Liên hệ</li>
<li>Hỏi đáp</li>
</ul>
</nav>
I want when I move to gioithieu.html, it will show the ul below.
I had this but it's not working.
Please help me. Thanks in advance.
Change the HTML to this:
<li>Giới thiệu
<ul>
<li>Giới thiệu chung</li>
<li>Cơ cấu tổ chức</li>
</ul>
</li>
Your selector implies that the ul (dropdown menu) is a direct child of the li due to using >, but instead it was a sibling, so it won't work due to that. With this snippet, the ul will now be its child.
Besides, it isn't valid HTML to put an ul inside an ul, and this way it is valid.
I have recently been using tabindex="1" and :focus with divs to make drop down lists in my menu.
But when these drop down lists are clicked they make my links below collapse on themselves, does anyone know why?
Here is the example https://jsfiddle.net/ugjgng5u/4/
When List 1 is clicked all links below shrink and collapse.
<li class=collapse tabindex="1"><a class=red> List 1 </a>
<div class="inside">Content 1....<br>
hi<br>
hi<br>
hi<br>
hi</div></li>
I thought it was to do with clearing the floats after the div? But didn't seem to help.
Thanks!
If I had to guess, it's because the li is inside the menu and you can't detach it. A work around is to make the div absolute.
https://jsfiddle.net/ugjgng5u/7/
HTML
<div id=container>
<div id=top-bar>
<div class=top-links>
<toplinks>
<ul id=menu>
<li><a>A </a></li>
<li class=collapse tabindex="1">
<a class=red> List 1 </a>
<div class="inside">Content 1....
<br> hi
<br> hi
<br> hi
<br> hi
</div>
</li>
<li> <a> C</a></li>
<li><a>B </a></li>
</ul>
</toplinks>
</div>
</div>
</div>
CSS
#container {
background-color: #fff;
max-width: 350px;
z-index: 1;
position: relative;
}
#top-bar {
display: block;
position: relative;
height: auto;
line-height: 1.7;
font-size: 16px;
font: Arial, Helvetica, sans-serif;
}
.top-links li a:hover {
color: #808080;
}
.top-links li ul {
display: none;
}
.top-links li ul li {
display: block;
float: none;
}
.top-links ul li a:hover + .hidden,
.hidden:hover {
display: block;
}
.top-links li > a {
display: block;
font-size: 12px;
font-weight: 600;
height: 44px;
color: #999;
text-decoration: none;
}
li.collapse > a {
cursor: pointer;
display: block;
}
li.collapse:focus {
outline: none;
}
li.collapse > div.inside {
display: none;
}
li.collapse:focus div.inside {
display: block;
}
.inside {
z-index: 10;
position: absolute;
top: 40%;
left: 11%;
background: white;
width: 300px;
padding-left: 20px;
border: 1px solid black;
}
You got some odd choices going on in your JSFiddle.
None-the-less, don't float .indside. Not sure why it's being floated to begin with. When you float and item you take it out of the normal document flow an it no longer takes up space like it did prior to floating. This means the parent element will treat it as if wasn't there.
If you're looking to do a fly-out menu then you should use position: absolute; on the dropdown menu and position: relative; on it's containing element.
Basic fly-out menu below.
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
ul {
width: 300px;
background-color: #f1f1f1;
}
li {
position: relative;
line-height: 1.5;
}
li:hover {
background-color: #ccc;
cursor: pointer;
}
li:hover > ul {
display: block;
}
li > ul {
display: none;
position: absolute;
top: 0;
left: 100%;
background-color: #eee;
}
<ul>
<li>One</li>
<li>Two
<ul>
<li>Two A</li>
<li>Two B</li>
</ul>
</li>
<li>Three</li>
</ul>
As a HTML/CSS newbie, I am trying to create a centered horizontal main menu with vertical drop down submenus.
The submenus are supposed to perfectly align with the parent main menu element.
Cannot attach picture due to lack of reputation points, but have a visualization in case that could help anyone.
In my current set-up, the submenu items (Directions and Google Maps) are aligned completely to the left and I cannot get them nicely under the main menu item (Location). I believe the solution lies with the absolute/relative positioning of elements, but I cannot figure out how to implement it without destroying the general layout.
Finally, the sub-menu boxes should all have the same width, while the main menu items can vary according to their normal length.
The end result would be similar to this example, unfortunately the code for it gives a 404 error.
This is my HTML:
#nav {
color: orange;
list-style: none;
font-weight: bold;
text-align: center;
border: 1px solid orange;
border-width: 1px 0;
}
#nav li {
display: inline;
}
#nav a {
display: inline-block;
padding: 1ex;
text-decoration: none;
color: orange;
}
#nav a:hover {
text-decoration: none;
color: white;
background-color: orange;
}
#nav li ul {
display: none;
list-style: none;
position: absolute;
}
#nav li ul li {
display: block;
border-bottom: 1px solid orange;
border-width: 1px 0;
text-align: left;
}
#nav li:hover ul {
display: block;
}
<ul id="nav">
<li>Home
</li>
<li>
Location
<ul>
<li>Directions
</li>
<li>Google Maps
</li>
</ul>
</li>
<li>Pictures
</li>
<li>Prices & Availability
</li>
<li>General Info
</li>
<li>Reservations & Contact
</li>
</ul>
Apologies for the newbie requests & cheers to all for helping!
Parent(#nav li) must be positioned relatively.
Initially ul renders with some padding, you should add padding: 0 to get proper alignment.
#nav {
color: orange;
list-style: none;
font-weight: bold;
text-align: center;
border: 1px solid orange;
border-width: 1px 0;
padding: 0;
}
#nav li {
display: inline;
position: relative;
}
#nav a {
display: inline-block;
padding: 1ex;
text-decoration: none;
color: orange;
}
#nav a:hover {
text-decoration: none;
color: white;
background-color: orange;
}
#nav li ul {
display: none;
list-style: none;
position: absolute;
padding: 0;
left: 0;
}
#nav li ul li {
display: block;
border-bottom: 1px solid orange;
border-width: 1px 0;
text-align: left;
}
#nav li:hover ul {
display: block;
}
<ul id="nav">
<li>Home
</li>
<li>
Location
<ul>
<li>Directions
</li>
<li>Google Maps
</li>
</ul>
</li>
<li>Pictures
</li>
<li>Prices & Availability
</li>
<li>General Info
</li>
<li>Reservations & Contact
</li>
</ul>
I have this menu:
#navbar {
text-align: center;
margin: auto;
padding: 0;
height: 1em;
}
#navbar li {
list-style: none;
float:left; }
#navbar li a:hover{
background-color: #CCC;
}
#navbar li a {
border: 1px solid #000;
display: block;
margin-right: 18px;
margin-left: 18px;
padding: 3px 8px;
background-color: #FFF;
color: #000;
text-decoration: none; }
#navbar li ul {
display: none;
width: 10em; /* Width to help Opera out */
}
#navbar li:hover ul {
display: block;
position: absolute;
margin: 0;
padding: 0; }
#navbar li:hover li {
float: none; }
#navbar li:hover li a {
background-color: #FFF;
border-bottom: 1px solid #000;
color: #000; }
#navbar li li a:hover {
background-color: #CCC; }
<ul id="navbar">
<li>Start</li>
<li>Vad?</li>
<li>Kom igång!</li>
<li>Läringsartikler<ul>
<li>Subitem One</li>
<li>Second Subitem</li>
<li>Numero Tres</li></ul>
</li>
<li>Läringsfilmer<ul>
<li>Subitem One</li>
<li>Second Subitem</li>
<li>Numero Tres</li></ul>
</li>
</ul>
as you can see in navbar { i tried to use text-align: center or margin:auto but it still wont center the whole menu..
why?
when i change the navbar li to float center instead of float left then it make the whole menu stupid big
You need to specify a width on your navbar ul.
#navbar {
text-align: center;
margin: auto;
padding: 0;
height: 1em;
width: 400px;
}
There is NO center value for 'float' style attribute
-- Oops dint see that comment
As mentioned, there is no Float:center. In order to center using margin-left and margin-right auto, you either need to set a width (as mentioned above) or change it to display:block.
If you don't want to set a width or can't, there's a CSS hack called Shrink Wrapping that is easy to setup.