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>
Related
I am trying to make a dropdown menu, it's my first time doing it and I'm experimenting with it. The problem that I'm facing is this:
As you can see, the principal menus are showing in a list. I have tried displaying them as flex and other attempts, but it seems like the header is making a limitation to them and I don't know how to put them beside each other. (Clarification: 'Notificaciones' and 'Usuario' are main menus and 'Mi Perfil' is a submenu that comes from 'Usuario' (parent))
Here is my code:
* {
text-decoration: none;
list-style: none;
}
body {
margin: 0px;
padding: 0px;
font-family: roboto;
}
.header-text {
position: relative;
margin: 2px 6px 2px 4px;
cursor: pointer;
}
.header-icons {
width: 32px;
margin: 2px 0px 2px 0px;
cursor: pointer;
}
header {
display: flex;
justify-content: space-between;
background-color: rgb(20, 33, 61);
color: white;
height: 30px;
align-items: center;
padding: 6px;
position: static;
}
.nav li a {
padding: 4px;
background-color: red;
list-style: none;
display: inline-block;
float: right;
}
<header>
<div class="header-text">
<h1 class="titulo-logo">Lorem</h1>
</div>
<nav class="nav">
<ul>
<li>Notificaciones</li>
<li>
Usuario
<ul>
<li>Mi Perfil</li>
</ul>
</li>
</ul>
</nav>
</header>
Thank you so much in beforehand!
First the <li> should have display: inline-block for being arranged in a row. It has nothing to do with the header.
Second, the position of the sub menu (ul ul) needs to be absolute within a li with position: relative.
white-space: nowrap will make the element not wrap when the width is larger than the parent element's width.
* {
text-decoration: none;
list-style: none;
}
body {
margin: 0px;
padding: 0px;
font-family: roboto;
}
.header-text {
position: relative;
margin: 2px 6px 2px 4px;
cursor: pointer;
}
.header-icons {
width: 32px;
margin: 2px 0px 2px 0px;
cursor: pointer;
}
header {
display: flex;
justify-content: space-between;
background-color: rgb(20, 33, 61);
color: white;
height: 30px;
align-items: center;
padding: 6px;
position: static;
}
.nav li a {
padding: 4px;
background-color: red;
list-style: none;
display: inline-block;
float: right;
}
/* added css */
ul li{
display: inline-block;
position: relative;
white-space: nowrap
}
ul ul{
position: absolute;
right:0;
top:100%
}
<header>
<div class="header-text">
<h1 class="titulo-logo">Lorem</h1>
</div>
<nav class="nav">
<ul>
<li>Notificaciones</li>
<li>
Usuario
<ul>
<li>Mi Perfil</li>
</ul>
</li>
</ul>
</nav>
</header>
i need the navbar to display the dropdown div when i hover on the .nav-right.I have tried using position:absolute for the .nav-dropdown, but it just keeps going inside the nav itself and i want it underneath. Also, the .nav-right does not even also display the dropdown, what am i doing wrong?
``html
<div class="nav-left">
<span class="logo"></span>
</div><!--nav left-->
<div class="nav-middle">
<ul class="nav-list">
<li class="nav-item"><a class="nav-link" href="#">About Project</a></li>
<li class="nav-item"><a class="nav-link" href="#">Portfolio</a></li>
<li class="nav-item"><a class="nav-link" href="#">Shop</a></li>
<li class="nav-item"><a class="nav-link" href="#">Collections</a></li>
</ul>
</div><!--nav middle-->
<div class="nav-right">
<div class="user-image">
<img src="" />
</div><!--user image-->
<div class="lines">
<span class="line line-1"></span>
<span class="line line-2"></span>
</div><!--lines-->
</div><!--nav right-->
<div class="nav-dropdown">
dropdown nav bar
</div><!--nav dropdown-->
</nav><!--nav---->
``
``css
.nav {
height: auto;
width: 100%;
position: fixed;
top: 0;
align-items: center;
z-index: 1;
box-sizing: border-box;
display: flex;
align-items: center;
padding: 0 80px;
box-sizing: border-box;
border: solid 1px red;
}
.nav .nav-left {
display: flex;
flex-basis: 15%;
justify-content: left;
align-items: center;
height: 70px;
border: solid 1px green;
}
.nav .nav-left span {
height: 30px;
width: 30px;
background-color: #000;
}
.nav .nav-middle {
height: 70px;
flex-basis: 65%;
display: flex;
align-items: center;
border: solid 1px blue;
}
.nav .nav-middle ul {
display: flex;
list-style: none;
padding: 0;
margin: 0;
}
.nav .nav-middle ul li {
margin-right: 70px;
border: Solid 1px red;
}
.nav .nav-right {
display: flex;
height: 70px;
border: solid 1px purple;
flex-basis: 20%;
align-items: center;
justify-content: right;
}
.nav .nav-right .user-image {
height: 40px;
width: 40px;
background: #000;
border-radius: 50%;
margin-right: 40px;
}
.nav .nav-right .lines .line {
background-color: #000;
width: 30px;
height: 2px;
display: block;
margin-bottom: 5px;
}
.nav .nav-dropdown {
width: 100%;
display: none;
position: absolute;
top: 0;
height: 100px;
background-color: #000;
color: #fff;
border: solid 3px green;
box-sizing: border-box;
}
.nav .nav-right:hover + .nav-dropdown {
display: block;
}
.nav-link {
color: #000;
text-decoration: none;
font-family: selectric, helvetica, arial;
transition: linear 0.3s;
}
.nav-link:hover {
color: #484848;
``
Set top css value in pixels for the nav-dropdown, to position it where you like to position it.
.nav {
height: auto;
width: 100%;
position: fixed;
top: 0;
align-items: center;
z-index: 1;
box-sizing: border-box;
display: flex;
align-items: center;
padding: 0 80px;
box-sizing: border-box;
border: solid 1px red;
}
.nav .nav-left {
display: flex;
flex-basis: 15%;
justify-content: left;
align-items: center;
height: 70px;
border: solid 1px green;
}
.nav .nav-left span {
height: 30px;
width: 30px;
background-color: #000;
}
.nav .nav-middle {
height: 70px;
flex-basis: 65%;
display: flex;
align-items: center;
border: solid 1px blue;
}
.nav .nav-middle ul {
display: flex;
list-style: none;
padding: 0;
margin: 0;
}
.nav .nav-middle ul li {
margin-right: 70px;
border: Solid 1px red;
}
.nav .nav-right {
display: flex;
height: 70px;
border: solid 1px purple;
flex-basis: 20%;
align-items: center;
justify-content: right;
}
.nav .nav-right .user-image {
height: 40px;
width: 40px;
background: #000;
border-radius: 50%;
margin-right: 40px;
}
.nav .nav-right .lines .line {
background-color: #000;
width: 30px;
height: 2px;
display: block;
margin-bottom: 5px;
}
.nav .nav-dropdown {
width: 100%;
display: none;
position: absolute;
top: 100px;
height: 100px;
background-color: #000;
color: #fff;
border: solid 3px green;
box-sizing: border-box;
}
.nav .nav-right:hover+.nav-dropdown {
display: block;
}
.nav-link {
color: #000;
text-decoration: none;
font-family: selectric, helvetica, arial;
transition: linear 0.3s;
}
.nav-link:hover {
color: #484848;
}
<nav class='nav'>
<div class="nav-left">
<span class="logo"></span>
</div>
<!--nav left-->
<div class="nav-middle">
<ul class="nav-list">
<li class="nav-item"><a class="nav-link" href="#">About Project</a></li>
<li class="nav-item"><a class="nav-link" href="#">Portfolio</a></li>
<li class="nav-item"><a class="nav-link" href="#">Shop</a></li>
<li class="nav-item"><a class="nav-link" href="#">Collections</a></li>
</ul>
</div>
<!--nav middle-->
<div class="nav-right">
<div class="user-image">
<img src="" />
</div>
<!--user image-->
<div class="lines">
<span class="line line-1"></span>
<span class="line line-2"></span>
</div>
<!--lines-->
</div>
<!--nav right-->
<div class="nav-dropdown">
dropdown nav bar
</div>
<!--nav dropdown-->
</nav>
Move nav-dropdown to inside nav-right. This locates the dropdown as a child to nav-right
<div class="nav-right">
<div class="user-image">
<img src="" />
</div><!--user image-->
<div class="lines">
<span class="line line-1"></span>
<span class="line line-2"></span>
</div><!--lines-->
<div class="nav-dropdown">
dropdown nav bar
</div><!--nav dropdown-->
</div><!--nav right-->
Add position:relative to nav-right. This means we can position nav-dropdown using position:absolute.
.nav-right {
display: flex;
height: 70px;
border: solid 1px purple;
flex-basis: 20%;
align-items: center;
justify-content: right;
position:relative;
}
Because nav-dropdown is positioned:absolute, we can use the top: property to move the top down by however we want, so set this to, say: 4rem
.nav-dropdown {
width: 100%;
display: none;
position: absolute;
top: 4rem;
height: 100px;
background-color: #000;
color: #fff;
border: solid 3px green;
box-sizing: border-box;
}
Change the .nav-right:hover selector to select .nav-dropdown like this which displays the dropdown when we hover over .nav-right.
.nav-right:hover > .nav-dropdown {
display: block;
}
Basically what we're doing is putting a child elemnt in your navbar-right that we hide on document load. We position nav-right as position:relative which does nothing for .nav-right but allows us to positon the dropdown relative to this. We position nav-dropdown as absolute which doesn't interrupt your navbar rendering and we can then use the css properties left, top, bottom and right (using only the ones we need) to position the dropdown where we like. In this case we use 'top' to push the thing down and make it look like a menu.
There are some good examples here: https://www.w3schools.com/css/css_dropdowns.asp and here https://css-tricks.com/solved-with-css-dropdown-menus/
Good luck!
I used the position: fixed style to make the <nav> not moving. However, if there is a <header>, there is a margin as much as the <header> area. After moving upward as much as the <header> area, I want to fix it at that position.
I'm using the React library, I'm studying on my own so it's hard to find a way.
#hd a {
font-family: "array";
font-size: 30px;
}
#hd {
margin-left: 0.5%;
width: 99%;
height: 45px;
display: flex;
justify-content: space-between;
align-items: center;
text-align: center;
}
#btn1 {
width: 100px;
height: 30px;
border-radius: 30px;
background-color: white;
border-color: #71bbff;
outline: 0;
}
#btn2 {
width: 100px;
height: 30px;
border-radius: 30px;
background-color: #71bbff;
outline: 0;
border-color: #71bbff;
margin-left: 25px;
}
/* header */
/* main */
#main {
width: 100%;
height: 600px;
}
nav a {
text-decoration: none;
color: white;
font-size: 25px;
}
nav {
width: 100%;
height: 60px;
position: absolute;
}
nav:hover {
background-color: black;
}
/*
#nav.sticky {
position: fixed;
margin-top: -45px;
background: white;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
}
*/
ul {
height: 60px;
display: flex;
justify-content: space-around;
align-items: center;
list-style: none;
}
ul li {
width: 130px;
text-align: center;
}
ul li:hover {
transition:all .3s ease-out;
border-bottom: 1px solid orange;
}
#fmenu ul {
position: absolute;
}
#smenu {
border-top: 1px solid orange;
width: 130px;
height: 240px;
display: none;
background: black;
}
#smenu li {
margin-top: 13px;
width: 100%;
}
#smenu li:hover {
background-color: peru;
}
#fmenu:hover #smenu {
display: block;
}
<header id="hd">
<img src="http://picsum.photos/45/45.jpg" width='45' />
<a>Array</a>
<div id="btn">
<button id="btn1"href="#">Login</button>
<button id="btn2"href="#">회원가입</button>
</div>
</header>
<div id="main">
<nav id="nav">
<ul>
<li>Array</li>
<li id="fmenu">커뮤니티
<ul id="smenu">
<li>자유게시판</li>
<li>질문게시판</li>
<li>정보게시판</li>
<li>프로젝트</li>
<li>일기장</li>
</ul></li>
<li>구성원</li>
<li>연합팀</li>
</ul>
</nav>
</div>
You need to move your nav tag one level up in the DOM tree and instead of fixed position which makes the position of the element constant, use sticky position which is relative positioned until it crosses a specified threshold, at which point it is treated as fixed positioned. Here is relevant part:
<header>....</header>
<nav id='nav'>...</nav>
<div id='main>...</div>
In the css,
nav{
width: 100%;
position: sticky;
top:0;
background-color: #000;
}
Make these changes to get your desired effect.
I begin with the following code below:
#square {
position: fixed;
width: 350px;
height: 100%;
top: 0px;
left: 0px;
background-color: rgb(230, 255, 230);
}
ul {
position: relative;
bottom: 30px;
display: flex;
flex-direction: column;
align-items: center;
list-style-type: none;
padding-left: 0;
}
li {
margin-top: 40px;
text-align: center;
}
.navlink {
text-decoration: none;
color: white;
border-color: white;
border-width: 2px;
border-style: solid;
padding: 5px 20px 5px 20px;
background-color: green;
border-radius: 10px;
}
a:hover {
background-color: #66DE66;
}
<div id = "square">
<ul>
<li><a class = "navlink" href = "#">Introduction</a></li>
<li><a class = "navlink" href = "#">Middle</a></li>
<li><a class = "navlink" href = "#">End</a></li>
</ul>
</div>
I need the li elements (Introduction, Middle, and End) to be the same width, while remaining centered, and while maintaining anchor href functionality and complete hover functionality. I've tried altering the width of the li element and .navlink class to no avail. I've also tried defining the green rectangular elements under li instead of .navlink, but that just presented new problems.
I suspect the padding I have defined for my .navlink class may present a problem, but I'm not sure how.
The issue with the code the column flexbox does not have a defined height and its stacking up to auto height of the flex items (see after removing margin-top on li and the relative translation on the ul):
#square {
position: fixed;
width: 350px;
height: 100%;
top: 0px;
left: 0px;
background-color: rgb(230, 255, 230);
}
ul {
/*position: relative;
bottom: 30px;*/
display: flex;
flex-direction: column;
align-items: center;
list-style-type: none;
padding-left: 0;
}
li {
/*margin-top: 40px;*/
text-align: center;
}
.navlink {
text-decoration: none;
color: white;
border-color: white;
border-width: 2px;
border-style: solid;
padding: 5px 20px 5px 20px;
background-color: green;
border-radius: 10px;
}
a:hover {
background-color: #66DE66;
}
<div id = "square">
<ul>
<li><a class = "navlink" href = "#">Introduction</a></li>
<li><a class = "navlink" href = "#">Middle</a></li>
<li><a class = "navlink" href = "#">End</a></li>
</ul>
</div>
Now you can see the li taking up as much space as it requires when the navlink anchor element is made a block element by adding display: block:
#square {
position: fixed;
width: 350px;
height: 100%;
top: 0px;
left: 0px;
background-color: rgb(230, 255, 230);
}
ul {
/*position: relative;
bottom: 30px;*/
display: flex;
flex-direction: column;
align-items: center;
list-style-type: none;
padding-left: 0;
}
li {
/*margin-top: 40px;*/
text-align: center;
}
.navlink {
text-decoration: none;
color: white;
border-color: white;
border-width: 2px;
border-style: solid;
padding: 5px 20px 5px 20px;
background-color: green;
border-radius: 10px;
display: block; /* added */
}
a:hover {
background-color: #66DE66;
}
<div id = "square">
<ul>
<li><a class = "navlink" href = "#">Introduction</a></li>
<li><a class = "navlink" href = "#">Middle</a></li>
<li><a class = "navlink" href = "#">End</a></li>
</ul>
</div>
Now you can make your ul and inline-flex element so that it takes only as much space as it needs and remove align-items: center. Also horizontally center the square by making it a flexbox too and using justify-content: center. Adjust the margin between the li and there you go:
#square {
position: fixed;
width: 350px;
height: 100%;
top: 0px;
left: 0px;
background-color: rgb(230, 255, 230);
display: flex; /* made this a flexbox */
justify-content: center; /* align horizontally */
}
ul {
/*position: relative;
bottom: 30px;*/
display: inline-flex; /* changed to inline flex */
flex-direction: column;
/*align-items: center;*/
list-style-type: none;
padding-left: 0;
}
li {
margin-top: 10px; /* changed */
text-align: center;
}
.navlink {
text-decoration: none;
color: white;
border-color: white;
border-width: 2px;
border-style: solid;
padding: 5px 20px 5px 20px;
background-color: green;
border-radius: 10px;
display: block; /* added */
}
a:hover {
background-color: #66DE66;
}
<div id="square">
<ul>
<li><a class="navlink" href="#">Introduction</a></li>
<li><a class="navlink" href="#">Middle</a></li>
<li><a class="navlink" href="#">End</a></li>
</ul>
</div>
How to make properly such navigation bar, especially this two intersecting lines. I know that I can use transform: skewX(10deg); for rotating this vertical line, but not sure am I ok with whole html&css
HTML:
<main>
<div class="rounded-rectangle">
<ul class="top">
<li>
<button>text</button>
</li>
<li>
<button>text</button>
</li>
</ul>
<ul class="bottom">
<li>
<button>text</button>
</li>
<li>
<button>text</button>
</li>
</ul>
</div>
</main>
Fiddle
You could use pseudo elements of the container to draw the intersecting lines.
html,
body {
height: 100%;
}
body {
display: flex;
flex-direction: row;
font-family: 'Raleway', sans-serif;
background-color: #e24d3d;
}
main {
flex-grow: 1;
}
ul li {
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
list-style: none;
text-transform: uppercase;
font-size: 30px;
}
button {
background-color: transparent;
border: none;
color: white;
padding: 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 2.5em;
font-family: 'Raleway', sans-serif;
text-transform: uppercase;
}
button:focus {
outline: -webkit-focus-ring-color auto 0px;
}
ul.top {
display: flex;
justify-content: space-around;
height: 50%;
}
ul.top div:nth-last-child(1) {
border-right: 1px solid #FFF;
}
ul.bottom {
display: flex;
justify-content: space-around;
height: 50%;
}
.rounded-rectangle {
background-color: rgba(128, 213, 226, 0.4);
height: 320px;
max-width: 520px;
border-radius: 10px;
margin: auto;
position: relative;
}
.rounded-rectangle:before, .rounded-rectangle:after {
content: '';
position: absolute;
background: #fff;
}
.rounded-rectangle:before {
top: 50%;
height: 1px;
left: 0; right: 0;
}
.rounded-rectangle:after {
left: 50%;
width: 1px;
top: 0; bottom: 0;
transform: rotate(10deg);
}
ul {
padding: 0;
margin: 0;
}
<main>
<div class="rounded-rectangle">
<ul class="top">
<li>
<button>text</button>
</li>
<li>
<button>text</button>
</li>
</ul>
<ul class="bottom">
<li>
<button>text</button>
</li>
<li>
<button>text</button>
</li>
</ul>
</div>
</main>