Responsive dropdown - html

I created a dropdown using CSS. The dropdown pops up when the elements of nav-bar are hovered. There is no error in the code. However, I need help with the styling of the nav-bar. I want all the elements in sub-menu to be in the same line, i.e, I want to elements of drop-down to be in same position while changing the information based on the element hovered in nav-bar. Reference can be drop-down on lululemon website.
nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
width: 100%;
margin-top: .7rem;
padding-left: 1.1rem;
}
.nav-list li {
position: relative;
}
.nav-list>li>a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
transition: color 300ms;
}
.sub-menu a {
position: relative;
top: 2rem;
color: #ffffff;
font-size: 1.1rem;
font-weight: 200;
padding: 0 40px 0 40px;
}
.sub-menu {
display: flex;
position: absolute;
box-sizing: border-box;
background-color: black;
visibility: hidden;
box-sizing: border-box;
width: 50rem;
height: 15rem;
}
.nav-list li:hover>.sub-menu {
top: 3.85em;
visibility: visible;
opacity: 1;
}
<nav>
<ul class="nav-list">
<li>
Men
<ul class="sub-menu">
<li>
Mens shirts
</li>
<li>
Mens Shorts
</li>
<li>
Mens Accessories
</li>
</ul>
</li>
<li>
Women
<ul class="sub-menu">
<li>
Womens shirts
</li>
<li>
Womens Shorts
</li>
<li>
Womens Accessories
</li>
</ul>
</nav>

nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
width: 100%;
margin-top: .7rem;
padding-left: 1.1rem;
position: relative;
}
.nav-list li {
position: relative;
}
.nav-list>li>a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
transition: color 300ms;
}
.sub-menu a {
color: #ffffff;
font-size: 1.1rem;
font-weight: 200;
padding: 0 40px 0 40px;
}
.sub-menu {
display: flex;
position: fixed;
box-sizing: border-box;
background-color: black;
visibility: hidden;
width: 50rem;
height: 15rem;
left: 20px;
top: 0;
}
.nav-list li:hover>.sub-menu {
top: 3.85em;
visibility: visible;
opacity: 1;
}
<nav>
<ul class="nav-list">
<li>
Men
<ul class="sub-menu">
<li>
Mens shirts
</li>
<li>
Mens Shorts
</li>
<li>
Mens Accessories
</li>
</ul>
</li>
<li>
Women
<ul class="sub-menu">
<li>
Womens shirts
</li>
<li>
Womens Shorts
</li>
<li>
Womens Accessories
</li>
</ul>
</nav>
I made some changes to your code...basically position: fixed did the job. You can set the left value according to your need.

Is this what you needed?
The problem was that the position:relative was at the wrong place, it must be applied to the .nav-list, since its the parent of .sub-menu and you need it to be aligned according to it.
nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
position:relative;
width: 100%;
margin-top: .7rem;
padding-left: 1.1rem;
}
.nav-list>li>a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
transition: color 300ms;
}
.sub-menu a {
position: relative;
color: #ffffff;
font-size: 1.1rem;
font-weight: 200;
padding: 0 40px 0 40px;
}
.sub-menu {
display: flex;
align-items:center;
position: absolute;
left:0px;
box-sizing: border-box;
background-color: black;
visibility: hidden;
box-sizing: border-box;
width: 50rem;
height: fit-content;
}
.nav-list li:hover >.sub-menu {
top: 3.85em;
visibility: visible;
opacity: 1;
}

Related

Duration for Bottom border using CSS

I'm creating navbar with submenu. The elements of the navbar have a bottom border that becomes visible when hovered on. Further, a submenu is also visible. Currently, the bottom border goes invisible when the cursor is moved to submenu. I want it to be visible as long as the submenu is open.
nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
width: 100%;
margin-top: .7rem;
/*Use this to change the postition of dropdown*/
padding-left: 1.1rem;
/*Use this to move the dropdown left and right*/
}
.nav-list li {
position: relative;
}
.nav-list>li>a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
}
.nav-list>li>a::after {
content: "";
position: absolute;
background-color: #ff2a00;
height: 4px;
width: 0;
left: 0;
bottom: -0.5px;
}
.nav-list>li>a:hover:after {
width: 100%;
}
.sub-menu {
display: flex;
position: absolute;
box-sizing: border-box;
background-color: black;
visibility: hidden;
top: 3.89rem;
/*adjust postion */
left: -4rem;
width: 82.5rem;
height: 35rem;
z-index: 5000;
}
.sub-menu a {
position: relative;
top: 2rem;
color: white;
font-size: 1.1rem;
font-weight: 200;
padding: 3rem 40px 0 40px;
}
.sub-menu a:hover {
color: #7e7978;
}
<div class="main" id="navbar">
<nav>
<ul class="nav-list">
<li>
Men
<ul class="sub-menu">
<li>shirts </li>
</ul>
</li>
</ul>
</nav>
</div>
I have added the following code to the .nav-list li:hover .sub-menu block
.nav-list>li:hover .sub-menu {
visibility: visible;
}
This makes sure that the sub-menu is visible as long as the parent element is hovered.
And also added the following code to the .nav-list>li>a:hover:after
.nav-list>li:hover>a::after {
width: 100%;
}
This makes sure that the bottom border is visible as long as the parent element is hovered.
nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
width: 100%;
margin-top: .7rem;
/*Use this to change the postition of dropdown*/
padding-left: 1.1rem;
/*Use this to move the dropdown left and right*/
}
.nav-list li {
position: relative;
}
.nav-list>li>a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
}
.nav-list>li>a::after {
content: "";
position: absolute;
background-color: #ff2a00;
height: 4px;
width: 0;
left: 0;
bottom: -0.5px;
}
.nav-list>li:hover>a::after {
width: 100%;
}
.nav-list>li:hover .sub-menu {
visibility: visible;
}
.sub-menu {
display: flex;
position: absolute;
box-sizing: border-box;
background-color: black;
visibility: hidden;
top: 3.89rem;
/*adjust postion */
left: -4rem;
width: 82.5rem;
height: 35rem;
z-index: 5000;
}
.sub-menu a {
position: relative;
top: 2rem;
color: white;
font-size: 1.1rem;
font-weight: 200;
padding: 3rem 40px 0 40px;
}
.sub-menu a:hover {
color: #7e7978;
}
.nav-list>li:hover .sub-menu {
visibility: visible;
}
.nav-list>li:hover>a::after {
width: 100%;
}
<div class="main" id="navbar">
<nav>
<ul class="nav-list">
<li>
Men
<ul class="sub-menu">
<li>shirts </li>
</ul>
</li>
</ul>
</nav>
</div>

How to make child <ul> element to take its parent <li> width

I have a dropdown menu consists of ul nested in another ul's list item position absolute.
indeed I want the child ul that represents the dd menu to take its parent li width but it takes its grandparent's ul width instead.
changing the position to relative will disrupt the li style/order don't know why.
nav {
position: relative;
display: flex;
width: 100%;
margin: 0 auto;
justify-content: space-between;
}
nav>ul.nav_list {
position: relative;
display: flex;
justify-content: space-around;
align-items: flex-start;
height: 40px;
}
nav>ul>li {
height: 100%;
margin: 0px 4px;
padding: 0px 8px;
font-size: 18px;
}
nav>ul>li>div {
border-bottom: 2px solid #D88B1D;
padding: 8px 9.5px 2px;
transition: border-bottom .1s;
line-height: 22px;
}
nav>ul>li>div:hover {
border-bottom: 4px solid #D88B1D;
}
.first {
display: flex;
justify-content: center;
width: 242px;
}
li.dropdown {
font-size: 18px;
font-weight: bold;
}
.dropdown:hover {
display: flex;
justify-content: center;
background-color: #42526e;
border-radius: 5px 5px 0px 0px;
}
.dropdown:hover>div.first {
border: none;
}
nav>ul>li>ul.dropdown-content {
position: absolute;
top: 100%;
left: 0;
width: 100%;
display: none;
background-color: gold;
z-index: 99;
}
li.dropdown:hover>ul.dropdown-content {
display: block;
}
.dropdown:hover>div.first>a {
color: white;
}
<nav>
<ul class="nav_list">
<li class="dropdown">
<div class="first">
All Catgories
</div>
<ul class="dropdown-content">
<li> Link1</li>
<li> Link2</li>
<li> Link3</li>
</ul>
</li>
<li>
<div>Shop by brand</div>
</li>
<li>
<div>Online Exclusive</div>
</li>
</ul>
</nav>
When you need to position: absolute an element relatively to its parent, you need the parent to have it's own stacking context. So the parent becomes an offsetParent -- its offset boundaries will be used for positioning.
In your case, you have to set position: relative to li.dropdown.
Then, on ul.dropdown-content, set left: 0; right:0; to stretch it between left and right boundaries so it takes 100% of parent's width:
nav>ul.nav_list {
display: flex;
justify-content: space-around;
align-items: flex-start;
height: 40px;
}
nav>ul>li {
height: 100%;
margin: 0px 4px;
padding: 0px 8px;
font-size: 18px;
}
nav>ul>li>div {
border-bottom: 2px solid #D88B1D;
padding: 8px 9.5px 2px;
transition: border-bottom .1s;
line-height: 22px;
}
nav>ul>li>div:hover {
border-bottom: 4px solid #D88B1D;
}
.first {
text-align: center;
width: 242px;
}
li.dropdown {
font-size: 18px;
font-weight: bold;
position:relative;
}
.dropdown:hover {
background-color: #42526e;
border-radius: 5px 5px 0px 0px;
}
.dropdown:hover>div.first {
border: none;
}
nav>ul>li>ul.dropdown-content {
position: absolute;
top: 100%;
right:0; left: 0;
display: none;
background-color: gold;
}
li.dropdown:hover>ul.dropdown-content {
display: block;
}
.dropdown:hover>div.first>a {
color: white;
}
<nav>
<ul class="nav_list">
<li class="dropdown">
<div class="first">
All Catgories
</div>
<ul class="dropdown-content">
<li> Link1</li>
<li> Link2</li>
<li> Link3</li>
</ul>
</li>
<li>
<div>Shop by brand</div>
</li>
<li>
<div>Online Exclusive</div>
</li>
</ul>
</nav>

How do you make the box extend all the way till the end if when you hover

I am trying to get the box to extend all the to the end on both sides when i hover with things like flex but it wont work.
Ps i know i have the display block in there
#mainNav ul {
display: flex;
padding: 0px 0px 500px 0px;
list-style: none;
}
#mainNav li {
width: 100%;
position: relative;
text-align: center;
}
#mainNav a {
color: black;
text-decoration: none;
background-color: rgb(121, 184, 19);
display: flex;
justify-content: center;
height: 65px;
align-items: center;
font-size: 30px;
letter-spacing: 2px;
text-transform: uppercase;
font-weight: bold;
}
#mainNav a:hover {
background-color: rgb(196, 107, 5);
transition: .3s ease-in;
}
.secondUl {
display: flex;
flex-direction: column;
position: absolute;
width: 100%;
}
.secondUl li {
display: none;
position: absolute;
top: 0px;
}
#mainNav li:hover .secondUl>li {
display: block;
width: 100%;
top: 0px;
}
<nav id="mainNav">
<ul>
<li>Home</li>
<li>Carrot Shops nere me
<ul class="secondUl">
<li>Walmart</li>
<li>Target</li>
<li>Costco</li>
</ul>
</li>
<li>More
<ul class="secondUl">
<li>Biggest Carrot ever</li>
<li>Biggest carrot farm ever</li>
<li>Most carrots ate in 1 serveing</li>
</ul>
</li>
</ul>
</nav>
Hope it helps. Please let me know if you had wanted this.
#mainNav ul {
display: flex;
padding: 0px 0px 500px 0px;
list-style: none;
}
#mainNav li {
width: 100%;
position: relative;
text-align: center;
}
#mainNav a {
color: black;
text-decoration: none;
background-color: rgb(121, 184, 19);
display: flex;
justify-content: center;
height: 65px;
align-items: center;
font-size: 20px;
letter-spacing: 2px;
text-transform: uppercase;
font-weight: bold;
}
#mainNav a:hover {
background-color: rgb(196, 107, 5);
transition: .3s ease-in;
}
.secondUl {
display: flex;
flex-direction: column;
position: absolute;
width: 100%;
}
.secondUl li {
display: none;
position: absolute;
top: 0px;
}
#mainNav li:hover .secondUl>li {
display: block;
width: 100%;
top: 0px;
}
<link rel="stylesheet" type="text/css" href="style.css">
<nav id="mainNav">
<ul>
<li>Home</li>
<li>Carrot Shops nere me
<ul class="secondUl">
<li>Walmart</li>
<li>Target</li>
<li>Costco</li>
</ul>
</li>
<li>More
<ul class="secondUl">
<li>Biggest Carrot ever</li>
<li>Biggest carrot farm ever</li>
<li>Most carrots ate in 1 serveing</li>
</ul>
</li>
</ul>
</nav>

Menu focus or active not responding as expected

Using pure css, I have a menu image that when :hover, the background-color changes as expected. When the user clicks, a menu pop out opens and so now, that initial menu image should be active. However, it is not staying the color needed even with my styling set for :active or :focus.
Image as when hover:
When the cursor moves off that image and to the menu items, that background-color does not stay the intended cover. That image:
My code for the html and css is:
#menu {
position: relative;
}
#menu_img:hover,
#menu_img:active,
#menu_img:focus {
background-color: #008272;
cursor: pointer;
}
#menu_items {
position: absolute;
top: 0%;
opacity: 0;
transition: all 0.5s;
display: none;
background-color: #002F33;
color: #ffffff;
height: 600px;
width: 18%;
z-index: 2;
left: 0px;
}
#menu_items > a {
font-size: 18px;
color: #ffffff;
margin-top: 15px;
margin-bottom: 15px;
padding-left: 20px;
}
#menu_items > a:hover {
text-decoration: none;
}
#menu_items.menu_items_toggle {
opacity: 1;
top: 100%;
display: inline-block;
}
<div id="menu" style="background-color: #002F33; height:50px;">
<span id="span_img_container" class="navIcon" style="width: 50px;"><img id="menu_img" src="assets/images/icon_hamburger.png"></span>
<div id="menu_items">
Moneyball Website
<br>
Moneyball Tool
<br>
Moneyball Stream Channel
<br>
</div>
<span style="color: #ffffff; font-size: 22px; padding-left:15px; padding-top: 26px; position: relative; top: 5px; left: -5px;">Moneyball Tool</span>>
</div>
Any ideas on this? Much appreciated!
I suggest it's because the icon is outside menu_items. You can try something like this:
nav {
background: #333;
color: white;
min-height: 2.75em;
position: relative;
}
nav h1 {
line-height: 2.75rem;
margin: 0 0 0 3.5rem;
}
nav menu {
line-height: 2.75;
left: 0;
margin: 0;
padding: 0;
position: absolute;
top: 0;
width: 2.75em;
}
nav menu:before {
box-shadow: 11px 0 0 -10px rgba(255, 255, 255, 0.6);
content: "☰";
display: block;
line-height: 2.75;
text-align: center;
transition: backround-color 200ms;
width: 2.75em;
}
nav menu li {
background: #333;
list-style: none;
max-height: 0;
overflow: hidden;
padding: 0 1em;
transition: max-height 200ms;
}
nav menu li:hover {
background-color: black;
}
nav menu:hover {
width: auto;
}
nav menu:hover:before {
background-color: green;
box-shadow: none;
}
nav menu:hover li {
max-height: 2.75em;
}
<nav>
<menu>
<li>
<a>First menu link</a>
</li>
<li>
<a>Second menu link</a>
</li>
<li>
<a>Third menu link</a>
</li>
<li>
<a>Fourth menu link</a>
</li>
</menu>
<h1>Page title</h1>
</nav>

Paragraph covers div brought through transition

Why does the paragraph always cover the div that is shown when you hover over link 1 in this example? I have tried changing the z-index of both the paragraph and the list, but nothing works. The thing that really makes me not understand is the fact that during the transition, the list appears over the paragraph. Then, after the transition, the paragraph seems to change z-index. Any help would be great.
div {
background-color: #BFBFBF;
height: 50px;
width: 100%;
z-index: 1;
}
.NavList {
list-style: none;
margin: 0;
padding: 0;
}
.NavListItem {
display: inline-block;
text-align: center;
float: left;
}
.NavListItem:Hover .NavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #7F7F7F;
transition: background-color .5s;
}
.NavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #404040;
transition: background-color .5s;
}
.NavListItem:Hover .SubNavList {
display: block;
list-style: none;
margin: 0;
padding: 0;
opacity: 1;
z-index: 100;
transition: opacity 5s;
}
.NavListItem:Hover .SubNavListItem {
display: block;
text-align: center;
}
.NavListItem:Hover .SubNavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #7F7F7F;
transition: background-color .5s;
}
.NavListItem:Hover .SubNavLink:Hover {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #404040;
transition: background-color .5s;
}
.SubNavList {
display: block;
width: 0px;
height: 0px;
opacity: 0;
transition: opacity 5s;
z-index: 100;
}
.SubNavListItem {
display: none;
}
p {
z-index: -1;
}
<body>
<div>
<ul class="NavList">
<li class="NavListItem">
Home
</li>
<li class="NavListItem">
Link 1
<ul class="SubNavList">
<li class="SubNavListItem">
SubLink 1
</li>
<li class="SubNavListItem">
SubLink 2
</li>
</ul>
</li>
<li class="NavListItem">
Link 2
<ul class="SubNavList">
<li class="SubNavListItem">
SubLink 3
</li>
<li class="SubNavListItem">
SubLink 4
</li>
</ul>
</li>
<li class="NavListItem">
Link3
</li>
</ul>
</div>
<p>Lorem ipsum dolor sit amet</p>
</body>
Z-index depends on the element having "positioning." Give your div and you p position like so:
div,p {
position: relative;
}
Fiddle example: http://jsfiddle.net/9dp7p5LL/
Just do this:
p {
display: block;
position: absolute;
z-index: -1;
}
See the updated fiddle here
z-index applies to positioned elements.
source
In your example you don't need negative z-index to p elements. You can just apply position: relative with a high z-index to .NavListItem:Hover .SubNavLink:hover like:
div {
background-color: #BFBFBF;
height: 50px;
width: 100%;
z-index: 1;
}
.NavList {
list-style: none;
margin: 0;
padding: 0;
}
.NavListItem {
display: inline-block;
text-align: center;
float: left;
}
.NavListItem:Hover .NavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #7F7F7F;
transition: background-color .5s;
}
.NavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #404040;
transition: background-color .5s;
}
.NavListItem:Hover .SubNavList {
display: block;
list-style: none;
margin: 0;
padding: 0;
opacity: 1;
z-index: 100;
transition: opacity 5s;
}
.NavListItem:Hover .SubNavListItem {
display: block;
text-align: center;
}
.NavListItem:Hover .SubNavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #7F7F7F;
transition: background-color .5s;
}
.NavListItem:Hover .SubNavLink:Hover {
display: block;
position: relative;
z-index: 10;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #404040;
transition: background-color .5s;
}
.SubNavList {
display: block;
width: 0px;
height: 0px;
opacity: 0;
transition: opacity 5s;
z-index: 100;
}
.SubNavListItem {
display: none;
}
<body>
<div>
<ul class="NavList">
<li class="NavListItem">
Home
</li>
<li class="NavListItem">
Link 1
<ul class="SubNavList">
<li class="SubNavListItem">
SubLink 1
</li>
<li class="SubNavListItem">
SubLink 2
</li>
</ul>
</li>
<li class="NavListItem">
Link 2
<ul class="SubNavList">
<li class="SubNavListItem">
SubLink 3
</li>
<li class="SubNavListItem">
SubLink 4
</li>
</ul>
</li>
<li class="NavListItem">
Link3
</li>
</ul>
</div>
<p>Lorem ipsum dolor sit amet</p>
</body>