I have this dropdown menu:
<nav>
<ul>
<li><a>Home</a></li>
<li><a>Who are we</a></li>
<li><a>Habitat</a></li>
<li id="menu">
<a>Raising in Captivity</a>
<ul id="dropdown">
<li><a>Temperature and Humidity</a></li>
<li><a>UVB</a></li>
<li><a>Diet</a></li>
</ul>
</li>
</ul>
</nav>
and the CSS for it:
nav {
border-top: solid 1px #DADADA;
border-bottom: solid 1px #DADADA;
margin-top: 60px;
text-align: center;
}
li {
display: inline-block;
}
#dropdown {
display: none;
}
#dropdown.active {
position: absolute;
display: flex;
flex-direction: column;
position: relative;
line-height:15px;
}
a {
padding: 15px;
margin: 10px;
text-decoration: none;
text-transform: uppercase;
color: black;
font-size: 15px;
font-weight: bold;
}
and the JavaScript:
document.getElementById("menu").addEventListener("click", openMenu);
function openMenu() {
document.getElementById("dropdown").classList.toggle("active");
}
When you click on the Raising in Captivity link it opens the three dropdown options and it works, but they are positioned inside the navigation, I want them to show up just below the Raising in Captivity element , outside of the bottom border, how can I do that with css?
Here is a demo:
https://jsfiddle.net/philip9/farc0xmz/1/
#dropdown.active {
margin-top: 15px;
display: flex;
flex-direction: column;
position: absolute;
line-height:20px;
padding: 10px;
margin-left: -40px;
}
position: absolute. Then I think it looks nice with padding and margin-left added as well. Centers it under
You need to update the style for #dropdown.active. There are 2 position styles, remove position:releative and add some margin to move the drop-down below the lower border.
document.getElementById("menu").addEventListener("click", openMenu);
function openMenu() {
document.getElementById("dropdown").classList.toggle("active");
}
nav {
border-top: solid 1px #DADADA;
border-bottom: solid 1px #DADADA;
margin-top: 60px;
text-align: center;
}
li {
display: inline-block;
}
#dropdown {
display: none;
}
#dropdown.active {
position: absolute;
display: flex;
flex-direction: column;
line-height: 15px;
margin-top: 15px;
}
a {
padding: 15px;
margin: 10px;
text-decoration: none;
text-transform: uppercase;
color: black;
font-size: 15px;
font-weight: bold;
}
<nav>
<ul>
<li><a>Home</a></li>
<li><a>Who are we</a></li>
<li><a>Habitat</a></li>
<li id="menu">
<a>Raising in Captivity</a>
<ul id="dropdown">
<li><a>Temperature and Humidity</a></li>
<li><a>UVB</a></li>
<li><a>Diet</a></li>
</ul>
</li>
</ul>
</nav>
remove position: relative from #dropdown.active selector styles:
#dropdown.active {
position: absolute;
display: flex;
flex-direction: column;
line-height:15px;
}
Probably your problem is because you have both position: absolute; and position: relative; in the #dropdown.active CSS. Remove the position: relative; and adjust the margins.
Added/Updated these styles
#dropdown a {
padding: 0;
margin: 0;
}
#menu {
position: relative;
}
#dropdown.active {
position: absolute;
display: flex;
flex-direction: column;
line-height: 15px;
left: 0;
text-align: left;
}
nav {
border-top: solid 1px #DADADA;
border-bottom: solid 1px #DADADA;
margin-top: 60px;
text-align: center;
}
li {
display: inline-block;
}
#dropdown {
display: none;
}
#menu {
position: relative;
}
#dropdown.active {
position: absolute;
display: flex;
flex-direction: column;
line-height: 15px;
left: 0;
text-align: left;
padding-top: 10px;
}
a {
padding: 15px;
margin: 10px;
text-decoration: none;
text-transform: uppercase;
color: black;
font-size: 15px;
font-weight: bold;
}
#dropdown a {
padding: 0;
margin: 0;
}
<nav>
<ul>
<li><a>Home</a></li>
<li><a>Who are we</a></li>
<li><a>Habitat</a></li>
<li id="menu">
<a>Raising in Captivity</a>
<ul id="dropdown">
<li><a>Temperature and Humidity</a></li>
<li><a>UVB</a></li>
<li><a>Diet</a></li>
</ul>
</li>
</ul>
</nav>
<script>
document.getElementById("menu").addEventListener("click", openMenu);
function openMenu() {
document.getElementById("dropdown").classList.toggle("active");
}
</script>
Your CSS code contains a duplicate entry for position property. So remove 'position: relative' from it. I added a margin-top of 25px to bring it below the bottom border.
You don't need to change the HTML and Javascript of your code. Change the CSS file as follows:
nav {
border-top: solid 1px #DADADA;
border-bottom: solid 1px #DADADA;
margin-top: 60px;
text-align: center;
}
li {
display: inline-block;
}
#dropdown {
display: none;
}
#dropdown.active {
position: absolute;
display: flex;
flex-direction: column;
margin-top: 25px;
line-height:15px;
}
a {
padding: 15px;
margin: 10px;
text-decoration: none;
text-transform: uppercase;
color: black;
font-size: 15px;
font-weight: bold;
}
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'm trying to make a Navbar with a dropdown menu. I want 'Clips' to have a dropdown menu with 3 links. But I can't seem to get it to work.
If i set the position to absolute i get them stacked on top of eachother en when i change it to relative they go inside the link. Can someone help? Do I have to change the selectors?
Thanks in advance!
.navbar{
display: flex;
justify-content: space-around;
align-items: center;
font-size: 18px;
background-color: darkred;
}
.nav-list{
list-style-type: none;
}
.dropdown{
position: relative;
display: inline-block;
list-style-type: none;
padding: 10px 10px;
border: solid black 1px;
border-radius: 10px;
margin: 2px;
}
.dropdown ul li{
position: relative;
}
.dropdown-content {
display: none;
position: absolute;
background-color: darkred;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 10;
overflow: visible;
}
.dropdown-content a {
color: black;
padding: 12px 18px;
text-decoration: none;
display: block;
}
.dropdown:hover .dropdown-content {
display: block;
}
.nav-list .list-item {
display: inline-block;
padding: 10px 10px;
border: solid black 1px;
border-radius: 10px;
margin: 2px;
}
.navbar a{
text-decoration: none;
color: white;
font-family: 'arvo', italic;
}
.logo{
height: 60px;
position: relative;
margin: 5px 0px 5px 17px;
}
.logo_text {
position: absolute;
top: 50px;
font-size: 12pt;
color: white;
}
<body>
<header>
<nav class="navbar">
<div>
<img class="logo" src="img/FrankLogo.svg"> <h3 class="logo_text"><a href=index.html>FRANKIE</a></h3>
</div>
<ul class="nav-list">
<li class="dropdown">Clips
<ul>
<li class="dropdown-content">Frankie Anthem</li>
<li class="dropdown-content">Vrouwtjes</li>
</ul></li>
<li class="list-item">Shows</li>
<li class="list-item">Vieze Victor</li>
<li class="list-item">Contact</li>
</ul>
</nav>
</header>
Set UL position inside top menu
li.dropdown > ul {
position: absolute;
margin-left: 0 !important;
padding-left: 0;
top: 40px;
}
I have this navbar where the listed items do not span the complete height of the navbar. Most of the posts I have seen have talked about padding and margin, but none of what I have should effect what is going on. Any tips would be great, thanks.
#navbar {
display: flex;
justify-content: flex-end;
width: 100%;
top: 0;
left: 0;
padding: 0;
position: fixed;
background-color: slategray;
}
#nav-list {
display: flex;
margin-right: 5rem;
list-style: none;
}
#nav-list a {
display: block;
padding: 1rem;
color: white;
font-size: 1.5rem;
text-decoration: none;
border: 1px solid black;
}
#nav-list a:hover {
background: grey;
}
<nav id="navbar">
<ul id="nav-list">
<li><a>item1</a></li>
<li><a>item2</a></li>
<li><a>item3</a></li>
</ul>
</nav>
EDIT: Thank you all for the fast answer, the margin was the problem and I swear I played with that and it changed nothing. I appreciate all the possible solutions that resolved this and teaching me the default behavior of ul margins!
Replace the margin-right rule in the #nav-list ruleset with a complete margin rule: margin: 0 5rem 0 0;. This will reset the browser's default styling and make it work as you wanted.
#navbar {
display: flex;
justify-content: flex-end;
width: 100%;
top: 0;
left: 0;
padding: 0;
position: fixed;
background-color: slategray;
}
#nav-list {
display: flex;
/*margin-right: 5rem; instead of this*/
margin: 0 5rem 0 0; /*use this*/
list-style: none;
}
#nav-list a {
display: block;
padding: 1rem;
color: white;
font-size: 1.5rem;
text-decoration: none;
border: 1px solid black;
}
#nav-list a:hover {
background: grey;
}
<nav id="navbar">
<ul id="nav-list">
<li><a>item1</a></li>
<li><a>item2</a></li>
<li><a>item3</a></li>
</ul>
</nav>
Now this will work...
#navbar {
display: flex;
justify-content: flex-end;
width: 100%;
top: 0;
left: 0;
padding: 0;
position: fixed;
background-color: slategray;
}
#nav-list {
margin: 0; /*added*/
display: flex;
margin-right: 5rem;
list-style: none;
}
#nav-list a {
display: block;
padding: 1rem;
color: white;
font-size: 1.5rem;
text-decoration: none;
border: 1px solid black;
}
#nav-list a:hover {
background: grey;
}
<nav id="navbar">
<ul id="nav-list">
<li><a>item1</a></li>
<li><a>item2</a></li>
<li><a>item3</a></li>
</ul>
</nav>
ul by default has margin-block-start: 1em and margin-block-end: 1em.
To fix this add margin: 0 to #nav-list.
#nav-list {
display:flex;
margin:0;
margin-right: 5rem;
list-style: none
}
If you are only concerned with height, just remove margin-top and margin-bottom from #nav-list.
Try this:
#nav-list {
display: flex;
list-style: none;
margin: 0 80px 0 16px;
}
The navbar I created overlaps when I switch my site to a mobile view.
I tried making it fit-content, but it still overlaps. The part that overlaps is the listed items
#navBar {
overflow: hidden;
text-align: center;
border-top: double black 2px;
}
#navBar ul {
padding: 0;
list-style: none;
width: fit-content;
}
#navBar li {
display: inline;
padding-top: 23px;
position: relative;
}
#navBar a {
font-size: 20px;
padding-right: 20px;
text-decoration: none;
text-align: center;
border: 4px solid lime;
padding-bottom: 10px;
padding-top: 20px;
padding-left: 10px;
font-family: American Typewriter, serif;
color: #262626;
text-transform: uppercase;
border-radius: 0px 0px 25px 25px;
}
#navBar a:hover {
color: lime;
}
#navBar a::before {
content: '';
display: block;
height: 5px;
width: 100%;
background-color: lime;
position: absolute;
top: 0;
width: 0%;
transition: all ease-in-out 250ms;
}
#navBar::hover::before {
width: 100%;
}
<nav id="navBar">
<div class="bar">
<ul>
<li>Tab1</li>
<li>Tab2 </li>
<li> Tab3</li>
</ul>
</div>
</nav>
I expect the navbar items to be side by side but the actual result in them overlapping.
You can use inline-flex for that
change
#navBar ul {
padding: 0;
margin: 0; /* reset margin */
list-style: none;
width: fit-content;
}
#navBar li {
display: inline-flex; /* change to inline-flex and remove the padding */
position: relative;
}
#navBar {
overflow: hidden;
text-align: center;
border-top: double black 2px;
}
#navBar ul {
padding: 0;
margin: 0;
list-style: none;
width: fit-content;
}
#navBar li {
display: inline-flex;
position: relative;
}
#navBar a {
font-size: 20px;
padding-right: 20px;
text-decoration: none;
text-align: center;
border: 4px solid lime;
padding-bottom: 10px;
padding-top: 20px;
padding-left: 10px;
font-family: American Typewriter, serif;
color: #262626;
text-transform: uppercase;
border-radius: 0px 0px 25px 25px;
}
#navBar a:hover {
color: lime;
}
#navBar a::before {
content: '';
display: block;
height: 5px;
width: 100%;
background-color: lime;
position: absolute;
top: 0;
width: 0%;
transition: all ease-in-out 250ms;
}
#navBar::hover::before {
width: 100%;
}
<nav id="navBar">
<div class="bar">
<ul>
<li>Tab1</li>
<li>Tab2 </li>
<li> Tab3</li>
</ul>
</div>
</nav>
So I try to change li's vertical position but when I do the nav bar height is affected as well. What is the way to actually do that without affecting nav bar's height?
Here's the code:
body {
margin: 0px;
}
ul {
margin: 0px;
padding: 0px;
list-style: none;
padding-left: 5px;
padding-top: 10px;
background-color: #333;
overflow: hidden;
}
li {
float: left;
height: 30px;
width: 100px;
background-color: grey;
color: white;
margin-right: 4px;
text-align: center;
line-height: 30px;
font-weight: bold;
font-size: 15px;
}
li a {
text-decoration: none;
color: white;
display: block;
}
ul li:hover {
opacity: 0.8;
}
<ul>
<li>Home</li>
<li>My Profile</li>
<li>Settings</li>
</ul>
What is your goal here? To have the content centered or...?
It may be better to use flexbox here rather than set the padding-left: 850px; on your ul (also on your ul you could've used display: block; margin: 0 auto; to center it.) If you'd like, you can give your ul a defined height and use align-items to specify how it should be aligned vertically.
body {
margin: 0;
}
nav {
background-color: #333;
display: flex;
justify-content: center;
}
nav ul {
display: flex;
justify-content: center;
list-style: none;
margin: 0;
padding: 0;
height: 50px;
align-items: center;
}
li {
height: 30px;
width: 100px;
background-color: grey;
color: white;
margin-right: 4px;
text-align: center;
line-height: 30px;
font-weight: bold;
font-size: 15px;
}
li a {
text-decoration: none;
color: white;
display: block;
}
ul li:hover {
opacity: 0.8;
}
<body>
<nav>
<ul>
<li>Home</li>
<li>My Profile</li>
<li>Settings</li>
</ul>
</nav>
</body>
You can add position: relative; and a value for bottom or top- in my snippet bottom: 4px;:
This reserves the space originally taken by the element (without position: relative;), but moves the element according to the top/bottom/left/right settings, without affecting other elements.
body {
margin: 0px;
}
ul {
margin: 0px;
padding: 0px;
list-style: none;
padding-left: 5px;
padding-top: 10px;
background-color: #333;
overflow: hidden;
}
li {
float: left;
height: 30px;
width: 100px;
background-color: grey;
color: white;
margin-right: 4px;
text-align: center;
line-height: 30px;
font-weight: bold;
font-size: 15px;
position: relative;
bottom: 4px;
}
li a {
text-decoration: none;
color: white;
display: block;
}
ul li:hover {
opacity: 0.8;
}
<ul>
<li>Home</li>
<li>My Profile</li>
<li>Settings</li>
</ul>