So I want a dropdown menu but instead of it pop up in left, it will pop up down without overlay other subtopics and so these other subtopics will float to bot.
I made this montage on paint so you can see what I want:
enter image description here
nav ul {
box-shadow: 0px 0px 5px whitesmoke;
list-style-type: none;
background-color: #FB030F;
margin: 0;
padding: 0;
width:calc(100% - 3px);
}
nav > ul > li ul {
display: none;
}
nav ul > li {
display: block;
position: relative;
}
nav #m {
display: block;
padding: 5px 5px;
text-decoration: none;
white-space: nowrap;
color: white;
}
nav #m:hover {
background-color: #ff7373;
}
nav > ul li:hover > ul {
display: block;
position: absolute;
top: 0;
top: 100%;
}
ul li:hover > #n {
display: block;
position: absolute;
top: 0;
left: 100%;
}
nav a {
text-align: center;
}
<nav>
<ul>
<li>Builds
<ul id="n">
<li>Build 1</li>
<li>Build 2</li >
<li>Build 3</li>
<li>Build 4</li>
<li>Build 5</li>
<li>Build 6</li>
<li>Build 7</li>
<li>Build 8</li>
</ul>
</li>
<li>Configurador</li>
<li>Componentes</li>
<li>Guia</li>
</ul></li>
</nav>
Let me just write something here so I can post this question, because stack overflow is telling me that I cannot post this because I have too much code and so I have to add more details. But I guess my doubt is simple and I do not need to add more info, if I am wrong tell me please.
If I understand you right, then try this:
nav ul {
box-shadow: 0px 0px 5px whitesmoke;
list-style-type: none;
background-color: #FB030F;
margin: 0;
padding: 0;
width:calc(100% - 3px);
}
nav > ul > li ul {
display: none;
}
nav ul > li {
display: block;
position: relative;
}
nav #m {
display: block;
padding: 5px 5px;
text-decoration: none;
white-space: nowrap;
color: white;
}
nav #m:hover {
background-color: #ff7373;
}
nav > ul > li:hover ul {
display: block;
}
nav a {
text-align: center;
}
I just deleted some classes which positioned blocks absolute, that's why menu appeared not where you wanted.
Hope it'll help.
Related
I'm training my CSS to pass to JS, but I'm worried because I'm having some problems doing a drop-down menu.
The concept I already get, but when I put it to work, it's kind of bugging, and moving the nav.
header {
overflow: auto;
}
header img {
float: left;
margin-left: 300px;
margin-top: 10px;
}
header img:hover {
opacity: 0.8;
}
nav {
float: right;
margin-right: 250px;
margin-top: 40px;
}
nav ul {
padding: 0px;
}
nav ul li {
display: inline;
margin-right: 50px;
border-right: 1px solid black;
}
nav ul li:last-child {
border-right: none;
}
nav ul li a {
color: black;
text-decoration: none;
font-weight: bold;
font-family: roboto;
margin-right: 50px
}
nav ul li a:hover {}
nav li ul {
display: none;
}
nav li:hover ul {
display: block;
position: relative;
}
nav li:hover li {
float: none;
}
<header>
<img src="logo.png">
<nav>
<ul>
<li>
Menu 01
<ul>
<li>Submenu 01</li>
</ul>
</li>
<li>Menu 02</li>
<li>Menu 03</li>
<li>Menu 04</li>
</ul>
</nav>
</header>
I sent the code with the img tag just to make clear the position of the stuff. I'm also learning the stuff, so it'll probably be very bad in others monitors.
nav li ul{
display: none;
position:absolute;
}
nav li:hover ul{
display: block;
}
thats all
I have a menu with margin-top and margin-left of 12 pixels for the elements that are inside another , but when the cursor is between that space (margin), the menu is hidden. I can solve that if I remove position:absolute, but I need it. How can I solve this?
HTML
<ul>
<li>Home</li>
<li>Food
<ul>
<li>Chilean food</li>
<li>Chinese food</li>
<li>Mexican food
<ul>
<li>Nachos</li>
<li>Quesadillas</li>
<li>Tacos</li>
</ul>
</li>
</ul>
CSS
ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
}
ul li {
position: relative;
}
ul li ul {
display: none;
position: absolute;
margin-top: 4px;
}
ul li:hover > ul {
display: block;
}
ul li a {
text-decoration: none;
padding: 12px;
display: block;
background: black;
color: white;
white-space: nowrap;
}
ul li ul li ul {
top: 0;
left: 100%;
margin-top: 0;
margin-left: 4px;
}
JSFiddle: https://jsfiddle.net/v3ebd6hy/3/
Thanks!
Change the margin-top to padding-top in ul li ul.
Fiddle for Reference
Considering the following menu code JSFiddle here:
<div class="menu">
<nav>
<ul>
<li>Logo</li>
<li>
Services
<div class="menu-submenu">
<ul>
<li>Very big text here in this option</li>
<li>Option</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</div>
</li>
<li>Support</li>
<li>Contact</li>
</ul>
</nav>
</div>
And CSS:
.menu {
width: 100%;
background-color: white;
margin-bottom: 5px;
}
.menu nav ul {
display: flex;
flex-direction: row;
align-items: center;
padding: 0px;
margin: 0px;
list-style-type: none;
}
.menu nav ul li:first-child {
padding-left: 10%;
padding-top: 5px;
padding-right: 30px;
}
.menu nav ul li:not(:first-child) {
line-height: 30px;
padding-top: 10px;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 10px;
}
.menu nav ul li:first-child {
height: 30px;
}
.menu nav ul li:last-child {
margin-left: auto;
margin-right: 10%;
align-self: flex-end;
}
.menu nav ul li:hover:not(:first-child) {
background-color: blue;
color: white;
}
.menu nav ul li {
position: relative;
}
.menu-submenu {
display: none;
}
.menu nav ul li:hover .menu-submenu {
display: flex;
}
.menu-submenu ul {
position: absolute;
top: 30px;
display: flex;
flex-direction: column;
}
.menu-submenu ul li {
flex: 1;
background-color: red;
z-index: 10;
}
a. How do I make the submenu to open on vertical, not horizontal ?
b. How do I allow the submenu text not to brake (open with greater width that its parent) ?
a) You correctly set flex-direction: column on .menu-submenu ul. The problem is you also set flex-direction: row on .menu nav ul with equal (but applied) specificity. To correct this, simply give your .menu-submenu ul selector more specificity (by changing it to .menu-submenu > ul for example).
b) Your .menu nav ul li:first-child gets applied to both the navbar and the submenu. Due to the height and padding restrictions, this causes the display of the submenu to get messed up. I believe you only want to apply it to the main navbar. As such, simply change this rule to make use of the child combinator (>), as .menu nav > ul > li:first-child.
You'll want to apply > to either side of ul to target your navbar. To target your submenu, you should use .menu .menu-submenu > ul and .menu .menu-submenu > ul > li so that you don't get confused about which menu gets targetted.
Here's an example showcasing the submenu vertical, along with removing all other rules that were (likely) incorrectly applied to it:
.menu {
width: 100%;
background-color: white;
margin-bottom: 5px;
}
.menu nav ul {
display: flex;
flex-direction: row;
align-items: center;
padding: 0px;
margin: 0px;
list-style-type: none;
}
.menu nav > ul > li:first-child {
padding-left: 10%;
padding-top: 5px;
padding-right: 30px;
}
.menu nav > ul > li:not(:first-child) {
line-height: 30px;
padding-top: 10px;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 10px;
}
.menu nav > ul > li:first-child {
height: 30px;
}
.menu nav > ul > li:last-child {
margin-left: auto;
margin-right: 10%;
align-self: flex-end;
}
.menu nav > ul > li:hover:not(:first-child) {
background-color: blue;
color: white;
}
.menu nav ul li {
position: relative;
}
.menu-submenu {
display: none;
}
.menu nav ul li:hover .menu-submenu {
display: flex;
}
.menu .menu-submenu > ul {
position: absolute;
top: 30px;
display: flex;
flex-direction: column;
}
.menu .menu-submenu > ul > li {
flex: 1;
background-color: red;
z-index: 10;
}
<div class="menu">
<nav>
<ul>
<li>Logo</li>
<li>
Services
<div class="menu-submenu">
<ul>
<li>Very big text here in this option</li>
<li>Option</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</div>
</li>
<li>Support</li>
<li>Contact</li>
</ul>
</nav>
</div>
You can make it nicer with less code:
.menu, .menu * {margin: 0; padding: 0; box-sizing: border-box}
.menu {
background: white;
margin-bottom: 5px;
}
.menu ul {
list-style: none;
display: flex;
align-items: center;
}
.menu ul li {
margin: 0 5px; /* adjust */
padding: 5px; /* adjust */
}
.menu ul li:last-child {
margin-left: auto;
}
.menu ul li:hover:not(:first-child) {
background: blue;
color: white;
}
.menu ul li:hover .menu-submenu {
display: flex;
}
.menu-submenu {
display: none;
position: relative; /* added */
}
.menu-submenu ul {
position: absolute;
top: 5px; /* modified; adjust; needs to match the padding of the ".menu ul li" */
left: -5px; /* added; adjust; needs to match the padding of the ".menu ul li" (negative) */
display: flex;
flex-direction: column;
}
.menu-submenu ul li {
width: 100%; /* added */
padding: 5px; /* adjust */
white-space: nowrap; /* added */
background: red;
z-index: 10;
}
<nav class="menu">
<ul>
<li>Logo</li>
<li>
Services
<div class="menu-submenu">
<ul>
<li>Very big text here in this option</li>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</div>
</li>
<li>Support</li>
<li>Contact</li>
</ul>
</nav>
Otherwise the point is in using the white-space: nowrap to prevent line breaking and width: 100% to make them even in width. Since the .menu-submenu ul has position: absolute, position: relative needs to be on its parent element.
I have a problem with the navigation bar. When I hover over About or Text on the nav bar it shows a spacing on the left side of the button, I want it the hover colour to contain the full width of the button.
https://jsfiddle.net/jdd3h0sf/3/
HTML:
<div id="nav">
<ul>
<li class="home">Home</li>
<li>About</li>
<li>Text ⌄
<ul class="submenu">
<li>One</li>
<li>Two</li>
<li>Three</li></li>
</ul>
<li>Work</li>
<li>Contact ⌄
<ul class="submenutwo">
<li>One</li>
<li>Two</li>
<li>Three</li></li>
</ul>
</ul>
CSS:
#nav {
background-color: #333;
height: 52px;
text-align: center;
margin: 0 auto;
letter-spacing: 1px;
text-transform: uppercase;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
}
#nav li {
border-right: 1.8px solid #191919;
height: auto;
width: 156.5px;
padding: 0;
margin: 0;
}
.home {
border-left: 1.8px solid #191919;
}
#nav ul li {
display: inline-block;
}
#nav ul li:hover {
background-color: #444;
}
#nav ul li a, visted {
color: #ccc;
display: block;
padding: 15px;
margin: 0;
text-decoration: none;
}
#nav ul li a:hover {
color: #ccc;
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
}
#nav ul ul {
display: none;
position: absolute;
background-color: #444;
border: 1px solid #333;
border-top: 0;
max-width: 169px;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:visited {
color: #ccc;
}
#nav ul ul li a:hover {
color: #2980B9;
}
This is a part of display:inline-block;. If you want to keep them displayed inline-block, there are several different solutions (Read a css-Tricks article about it):
1 - Change your HTML format:
Change your <li>'s html like this:
<ul>
<li>
one</li><li>
two</li><li>
three</li>
</ul>
Or this:
<ul>
<li>one</li
><li>two</li
><li>three</li>
</ul>
Or even with comments, like this:
<ul>
<li>one</li><!--
--><li>two</li><!--
--><li>three</li>
</ul>
Or, just place all the li's on a single line:
<ul><li>one</li><li>two</li><li>three</li></li>
It is messy, yet effective.
2 - Negative margins:
Pretty straightforward:
li{
display: inline-block;
margin-right: -4px;
}
3 - Skip the closing tag:
This is actually perfectly fine in HTML5, li's do not have to have a closing tag.
<ul>
<li>one
<li>two
<li>three
</ul>
4 - Set the <ul>'s font size to 0:
ul {
font-size: 0;
}
ul li {
font-size: 16px;
}
5 - Or, just float the <li>'s:
Whatever floats your boat.
You are experiencing the dreaded inline-block spacing issue. In your fiddle, if you condense all of your li elements to be on the same line, the hover works as expected. The linked article outlines a few other options.
You can also just float the elements and that would resolve the issue.
#nav ul li {
float: left;
}
This is what I'm trying to do:
If you noticed there is space between the menu and the submenu.
The problem is that the submenu doesn't work this way, because when the mouse pointer leaves the menu the submenu disappears.
It only works if it looks like this:
How can I leave the space between the menu and the submenu and get it to work?
My Code:
JSFIDDLE CODE
HTML:
<body>
<nav>
<ul>
<li>One
<ul>
<li>1.1</li>
<li>1.2
</ul>
</li>
<li>Two
<ul>
<li>2.1</li>
<li>2.2</li>
<li>2.3</li>
</ul>
</li>
<li>Three
<ul>
<li>3.1</li>
<li>3.2</li>
</ul>
</li>
<li>Four</li>
</ul>
</nav>
</body>
CSS:
body {
background-color: #cac3bc
}
nav {
float: left;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
background-color: #fff;
margin-top: 10px;
padding: 0 20px;
list-style: none;
position: relative;
display: inline-table;
margin-right: -80px;
}
nav ul li {
float: left;
}
nav ul li:hover {
border-bottom: 5px solid #f5aa65;
color: #fff;
}
nav ul li a:hover {
color: #000;
}
nav ul li a {
display: block;
padding: 15px 15px;
font-family: 'PT Sans', sans-serif;
color: #000;
text-decoration: none;
}
nav ul ul {
background-color:#fff;
border-radius: 0px;
padding: 0;
position: absolute;
top: 100%;
box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #000;
}
You could make use of :before to extend the "hoverable" area:
nav ul ul:before {
content: "";
display: block;
height: 20px;
position: absolute;
top: -20px;
width: 100%;
}
See this demo.
The accepted answer is beautifully simple and perfect. However, I want to add an alternative for others like myself who had to use a variation of the answer above. In my situation my sub menu is full width so to do that I do an absolute position on my sub menu to start just below the main menu - I introduce the :before element to bring in a gap of 100px. Therefore my :before code is
// Define the 100px gap between menu and submenu.
&:hover ul.sub-menu:before {
content: "";
display: block;
//Note: This height starts at the top:100% of the position absolute for the ul.sub-menu below,
//pushing the sub-menu down by the height defined here.
height: 100px;
width: 100%;
background-color: transparent;
}
The code to place the sub-menu at an absolute position below the main menu and full width is
&:hover ul.sub-menu {
background-color: transparent;
display: block;
position: absolute;
border-top: 10px solid red;
top: 100%;
left: 0;
width: 100%;
// Sub-menu appears on top of main menu.
z-index: 1;
enter code here