<ul> dropdown pushing up parent <li> - html

I want to add a CSS dropdown menu to my header. It's works in part...
but when you mouse over it, this element escapes up. How to set it correctly?
It should stay in place and dropdown should be under the <li> element.
* {
margin: 0px;
padding: 0px;
font-family: 'Advent Pro', sans-serif;
}
body {
display: flex;
-ms-flex-direction: column;
flex-direction: column;
min-height: 100vh;
}
.wrapper {
display: flex;
flex-direction: column;
}
.navbar-list,
.navbar-list a,
.navbar,
.logo {
display: flex;
align-items: center;
}
.navbar {
display: flex;
background: #008cf4;
padding: 0 20px;
flex-wrap: wrap;
border-bottom: 1px solid #d6d7dd;
font-size: 14px;
}
.navbar .navbar-list {
height: 80px;
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
margin-left: auto;
flex-wrap: wrap;
flex-grow: 1;
justify-content: flex-end;
}
.navbar .navbar-list a {
color: #e9e9e9;
text-decoration: none;
margin: 0 10px;
padding: 0 10px;
text-transform: uppercase;
}
.navbar .navbar-list a:hover {
color: #fff;
}
.navbar .navbar-list i {
font-size: 14px;
color: inherit;
padding-bottom: 1px;
}
.navbar .navbar-list ul {
display: none;
flex-direction: column;
list-style-type: none;
}
.navbar .navbar-list ul li {
list-style-type: none;
justify-content: center;
align-items: flex-end;
align-content: flex-start;
height: auto !important;
opacity: 0.8;
background: black;
text-align: center;
}
.navbar .navbar-list ul li a {
height: 10px !important;
}
.navbar .navbar-list ul li a:hover {
background-color: #a4a4a4;
height: 10px;
}
.navbar .navbar-list li:hover ul {
display: block;
height: auto !important;
}
.navbar .logo {
margin-right: 15px;
}
.navbar .logo h1 {
font-family: 'Alegreya', sans-serif;
font-size: 35px;
}
<link href="//fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href='//fonts.googleapis.com/css?family=Alegreya&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Advent+Pro&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<script src="//code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous">
</script>
<div class="wrapper">
<nav class="navbar">
<div class="logo">
<h1>Your logo</h1>
</div>
<ul class="navbar-list">
<li>
<a href="#">
<i class="material-icons">home</i> Dashboard
</a>
</li>
<li>
<a href="#">
<i class="material-icons">build</i> Account & settings
</a>
<ul>
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
</ul>
</li>
</ul>
<div>avatar</div>
</nav>
</div>
View on Codepen with SCSS

You need to set position:relative to the parent item, then position:absolute on the dropdown.
Without touching the HTML, that'd be
.navbar-list > li{
position:relative;
}
.navbar-list ul{
position:absolute;
width:100%;
}
The second rule sets any <ul> that's a descendant from the .navbar-list as absolute positioned, removing it from the flow so they won't "push" others, while the first makes the any <li> that's a direct child of the .navbar-list the point from which their child <ul> will be positioned.

Add position: relative to .navbar-list li
Add position: absolute top: 100% left: 0 and width: 100% to li ul
Updated codepen

try this
* {
margin: 0px;
padding: 0px;
font-family: 'Advent Pro', sans-serif;
}
body {
display: flex;
-ms-flex-direction: column;
flex-direction: column;
min-height: 100vh;
}
.wrapper {
display: flex;
flex-direction: column;
}
.navbar-list,
.navbar-list a,
.navbar,
.logo {
display: flex;
align-items: center;
}
.navbar {
display: flex;
background: #008cf4;
padding: 0 20px;
flex-wrap: wrap;
border-bottom: 1px solid #d6d7dd;
font-size: 14px;
}
.navbar .navbar-list {
height: 80px;
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
margin-left: auto;
flex-wrap: wrap;
flex-grow: 1;
justify-content: flex-end;
}
.navbar .navbar-list a {
color: #e9e9e9;
text-decoration: none;
margin: 0 10px;
padding: 0 10px;
text-transform: uppercase;
}
.navbar .navbar-list a:hover {
color: #fff;
}
.navbar .navbar-list i {
font-size: 14px;
color: inherit;
padding-bottom: 1px;
}
.navbar .navbar-list ul {
display: none;
flex-direction: column;
list-style-type: none;
position: absolute;
width: auto;
}
.navbar .navbar-list ul li {
list-style-type: none;
justify-content: center;
align-items: flex-end;
align-content: flex-start;
height: auto !important;
opacity: 0.8;
background: black;
text-align: center;
position: relative;
}
.navbar .navbar-list ul li a {
height: 10px !important;
}
.navbar .navbar-list ul li a:hover {
background-color: #a4a4a4;
height: 10px;
}
.navbar .navbar-list li:hover ul {
display: block;
height: auto !important;
}
.navbar .logo {
margin-right: 15px;
}
.navbar .logo h1 {
font-family: 'Alegreya', sans-serif;
font-size: 35px;
}
<link href="//fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href='//fonts.googleapis.com/css?family=Alegreya&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Advent+Pro&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<script src="//code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous">
</script>
<div class="wrapper">
<nav class="navbar">
<div class="logo">
<h1>Your logo</h1>
</div>
<ul class="navbar-list">
<li>
<a href="#">
<i class="material-icons">home</i> Dashboard
</a>
</li>
<li>
<a href="#">
<i class="material-icons">build</i> Account & settings
</a>
<ul>
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
</ul>
</li>
</ul>
<div>avatar</div>
</nav>
</div>

Related

how to make horizontal line between ::before and ::after element on hover?

My goal is to make horizontal line between The Anchor element, with ::before and ::after Pseudo Elements on hover.
The problem in my code is that horizontal line appears only on about element, when goal is to make it on every element. in developer tool it shows that properties are on every element before and after but it only appears on one (about) element as I said.
Can you help me, how to fix this code ?
nav,
nav ul {
height: 100vh;
margin: 0;
padding: 0;
}
nav ul {
display: flex;
flex-direction: column;
/* justify-content: stretch; */
list-style: none;
}
.nav-item {
height: 8%;
}
.home-icon {
padding-right: 0.5rem;
}
nav li {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
gap: 10px;
height: 20%;
/* overflow: hidden; */
}
nav li a {
font-family: 'Nunito', sans-serif;
font-weight: 1000;
font-size: 1.2rem;
color: whitesmoke;
letter-spacing: 2px;
text-decoration: none;
text-align: center;
}
nav li a:hover {
letter-spacing: 8px;
}
nav li a::before {
content: '';
width: 35vw;
height: 3px;
background-color: whitesmoke;
position: absolute;
top: 35%;
left: 0;
opacity: 0;
}
nav li a::after {
content: '';
width: 35vw;
height: 3px;
background-color: whitesmoke;
position: absolute;
top: 35%;
right: 0;
opacity: 0;
}
nav li a:hover::before {
opacity: 1;
}
nav li a:hover::after {
opacity: 1;
}
<nav>
<ul>
<li>
<img class="home-icon" src="./img/home.svg" alt="home" />
Home
</li>
<li>
<img src="./img/disk.svg" alt="about" />
About
</li>
<li>
<img src="./img/skills.svg" alt="skills" />
Skills
</li>
<li>
<img src="./img/briefcase.svg" alt="briefcase" />
Projects
</li>
<li>
<img src="./img/contacts.svg" alt="contacts" />
Contact
</li>
</ul>
</nav>
There were multiple problems with your CSS.
First off your were positioning your before and after absolutely, so they were always appearing in the same place. Second off, ::after and ::before are inline items by default so you need to set them to display: inline-block or the width won't take place.
I also took the liberty of reducing with width of your before and after since Projects is too long for 35vw on both sizes and would make the ::after appear below.
Code;
nav,
nav ul {
height: 100vh;
margin: 0;
padding: 0;
}
nav ul {
display: flex;
flex-direction: column;
/* justify-content: stretch; */
list-style: none;
}
.nav-item {
height: 8%;
}
.home-icon {
padding-right: 0.5rem;
}
nav li {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
gap: 10px;
height: 20%;
/* overflow: hidden; */
}
nav li a {
font-family: 'Nunito', sans-serif;
font-weight: 1000;
font-size: 1.2rem;
color: whitesmoke;
letter-spacing: 2px;
text-decoration: none;
text-align: center;
}
nav li a:hover {
letter-spacing: 8px;
}
nav li a::before {
content: '';
margin-right: 10px;
width: 25vw;
height: 3px;
background-color: whitesmoke;
opacity: 0;
display: inline-block;
}
nav li a::after {
content: '';
width: 25vw;
margin-left: 10px;
height: 3px;
background-color: whitesmoke;
opacity: 0;
display: inline-block;
}
nav li a:hover::before {
opacity: 1;
}
nav li a:hover::after {
opacity: 1;
}
<nav>
<ul>
<li>
<img class="home-icon" src="./img/home.svg" alt="home" />
Home
</li>
<li>
<img src="./img/disk.svg" alt="about" />
About
</li>
<li>
<img src="./img/skills.svg" alt="skills" />
Skills
</li>
<li>
<img src="./img/briefcase.svg" alt="briefcase" />
Projects
</li>
<li>
<img src="./img/contacts.svg" alt="contacts" />
Contact
</li>
</ul>
</nav>

One member of a list higher than the other

I am trying to create a navbar in my site, but every time I try to align the logo to the left the page selection is put higher, anyone have a solution?
Screenshot:
This is the html code:
<div class="NavBar">
<ul class="Items">
<li class="Logo">
<img src="images/logo1.png" alt="IM2B - Play your brand">
</li>
<li class="Links">
<a class="active" href="#home">Home</a>
News
Contact
About
</li>
</ul>
</div>
This is the css code:
.NavBar {
list-style-type: none;
overflow: hidden;
background-color: #333;
padding: 20px;
}
.NavBar .Items {
margin: auto;
width: 60%;
text-align: center;
list-style: none;
}
.NavBar .Logo {
float: left;
}
.NavBar .Links {
display: inline-block;
padding: 0px 16px;
}
.NavBar .Links a {
color: #f2f2f2;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.NavBar .Links a:hover {
background-color: #ddd;
color: black;
}
.NavBar .Links a.active {
background-color: #04AA6D;
color: white;
}
Add flex property to your outside navitems' containers:
.NavBar .Items {
margin: auto;
display: flex;
justify-content: space-around;
align-items: center;
/* width: 60%; */
text-align: center;
list-style: none;
}
.NavBar .Links {
border: 2px solid white;
display: flex;
justify-content: space-around;
align-items: center;
}
Is this you were looking for? Look, there are 2-levels of display:flex for alignments.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.NavBar {
list-style-type: none;
background-color: #333;
padding: 10px;
}
ul li {
list-style-type: none;
}
.NavBar .Items {
display: flex;
justify-content: space-around;
}
.NavBar .Links {
display: flex;
}
.NavBar .Links a {
color: #f2f2f2;
text-decoration: none;
font-size: 17px;
padding: 10px 20px;
display: inline-block;
height: 40px;
align-self: center;
}
.NavBar .Links a:hover {
background-color: #ddd;
color: black;
}
.NavBar .Links a.active {
background-color: #04AA6D;
color: white;
}
img {
height: 60px;
width: 100px;
}
<div class="NavBar">
<ul class="Items">
<li class="Logo">
<img src="https://picsum.photos/200/300" alt="IM2B - Play your brand">
</li>
<li class="Links">
<a class="active" href="#home">Home</a>
News
Contact
About
</li>
</ul>
</div>

top navbar items background does not cover whole space, it covers only the text

I am trying to create a simple web page and I want the links' background covers the whole space in navbar but it covers only the text around it.
My code is here:
.navbar {
width: 105%;
height: 5vw;
display: flex;
flex-direction: row;
background-color: #008083;
z-index: 10;
}
.navbar ul {
list-style: none;
margin: 0;
padding: 0px;
overflow: hidden;
}
.navbar li {
float: left;
}
.navbar li a {
text-decoration: none;
color: black;
padding: 1px;
text-align: center;
}
.tab {
width: 100%;
display: flex;
background-color: grey;
justify-content: center;
align-items: center;
margin: 1px;
}
<nav class="navbar">
<ul>
<li>Biography</li>
<li> Novels </li>
<li> Films </li>
</ul>
</nav>
Any suggestions?
You can remove the height from the navbar class so, it'll take the available height. Also, you can add height to .navbar li (Optional, if you want to use some custom height).
.navbar {
width: 105%;
/*height: 5vw;*/
display: flex;
flex-direction: row;
background-color: #008083;
z-index: 10;
}
.navbar ul {
list-style: none;
margin: 0;
padding: 0px;
overflow: hidden;
}
.navbar li {
float: left;
/*height: 100%;(Optional)*/
}
.navbar li a {
text-decoration: none;
color: black;
padding: 1px;
text-align: center;
}
.tab {
width: 100%;
display: flex;
background-color: grey;
justify-content: center;
align-items: center;
margin: 1px;
}
<nav class="navbar">
<ul>
<li>Biography</li>
<li> Novels </li>
<li> Films </li>
</ul>
</nav>
i don't understand exact your problem but if your problem with the default margin and padding its a solution
* {
padding: 0;
margin: 0;
}
.navbar {
width: 105%;
height: 5vw;
display: flex;
flex-direction: row;
background-color: #008083;
z-index: 10;
}
.navbar ul {
list-style: none;
margin: 0;
padding: 0px;
overflow: hidden;
}
.navbar li {
float: left;
/*height: 100%;(Optional)*/
}
.navbar li a {
text-decoration: none;
color: black;
padding: 1px;
text-align: center;
}
.tab {
width: 100%;
display: flex;
background-color: grey;
justify-content: center;
align-items: center;
margin: 1px;
}
<nav class="navbar">
<ul>
<li>Biography</li>
<li> Novels </li>
<li> Films </li>
</ul>
</nav>
I have removed some unnecessary style from your code & build this using CSS display: flex; instead of float. Now .tab is covering full height with background.
Here is the working example:
.navbar {
background-color: #008083;
z-index: 10;
}
.navbar ul {
list-style: none;
margin: 0;
padding: 0;
overflow: hidden;
display: flex;
align-items: center;
}
.navbar li {
margin: 0;
padding: 0;
}
.navbar li a {
text-decoration: none;
color: black;
text-align: center;
padding: 10px;
display: block;
}
.tab {
background-color: grey;
}
<nav class="navbar">
<ul>
<li>Biography</li>
<li> Novels </li>
<li> Films </li>
</ul>
</nav>

Trouble with alignment in html and css

I'm trying to write the code for a fitness supplement webshop, and as I am making the navigation bar, this weird problem has come up. The unordered list is neither aligned vertically nor horizontally.
HTML:
<div class="menus">
<ul menuList>
<li>SUPPLEMENTER</li>
<li>ACCESSORIES</li>
<li>TØJ</li>
<li>ANDET</li>
</ul>
</div>
CSS:
.menus {
background-color: #00C9FF;
height: 42px;
width: 100%;
padding: 0;
margin: 0;
display: flex;
flex-direction: row;
justify-content: center;
bottom: 0%;
}
.menus ul li {
list-style: none;
padding-left: 50px;
padding-right: 50px;
}
.menus ul {
display: flex;
flex-direction: row;
margin-top: auto;
}
.menus ul li a {
font-weight: 800;
font-style: italic;
font-size: 30pt;
text-decoration: none;
color: white;
}
If you needed this:
body {
margin: 0;
}
.menus {
background-color: #00C9FF;
height: 50px;
width: 100%;
padding: 0;
margin: 0;
display: flex;
flex-direction: row;
justify-content: center;
bottom: 0%;
}
.menus ul li {
list-style: none;
padding-left: 50px;
padding-right: 50px;
}
.menus ul {
display: flex;
flex-direction: row;
margin-top: auto;
}
.menus ul li a {
font-weight: 800;
font-style: italic;
font-size: 30pt;
text-decoration: none;
color: white;
}
<div class="menus">
<ul menuList>
<li>SUPPLEMENTER</li>
<li>ACCESSORIES</li>
<li>TØJ</li>
<li>ANDET</li>
</ul>
</div>
Then the body margin was not set to 0 and the navbar needed a little bit more of height.
And yes... that code is not responsive.

Need my hover state for my nav bar to be bigger

right now the purple just covers the text but it should be a nice block of colour like the dropdown is. Also, I have a bar under my nav img that should not be there when I hover. I know it is a width/height thing, but no matter where I put the code it does not work.
https://codepen.io/Smoki248/pen/NWxrOWK
li {
list-style: none;
}
a {
color: #f2f2f2;
text-decoration: none;
}
a:hover {
background-color: #8781bd;
}
.container {
max-width: 100%;
width: 100%;
margin: 0 auto;
text-align: center;
}
.btn {
padding: 0 20px;
height: 40px;
font-size: 1em;
font-weight: 400;
font-family: "Amatic SC", Roboto, sans-serif;
border: 1px #8781bd solid;
border-radius: 2px;
background: #8781bd;
color: #f2f2f2;
cursor: pointer;
}
.grid {
display: flex;
}
header {
position: fixed;
top: 0;
min-height: 75px;
padding: 0px 0px;
display: flex;
align-items: center;
background-color: #2f2f2f;
}
#media (max-width: 600px) {
header {
flex-wrap: wrap;
}
}
.logo {
width: 60vw;
}
#media (max-width:650px) {
.logo {
margin-top: 15px;
width: 100%;
position: relative;
}
}
.logo > img {
width: 100%;
height: 100%;
max-width: 100px;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
margin-left: 20px;
}
#media (max-width: 650px) {
.logo > img {
margin: 0 auto;
}
}
nav {
font-weight: 400;
}
#media (max-width: 650px) {
nav {
margin-top: 10px;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
padding: 0 50px;
}
}
h1 {
font-family: "Amatic SC", Raleway, Roboto, sans-serif;
font-size: 35pt;
width: 100%;
text-align: center;
}
h2 {
font-family: "Amatic SC", Raleway, Roboto, sans-serif;
font-size: 24pt;
width: 100%;
text-align: center;
}
nav li {
padding-bottom: 30px 0px;
}
nav > ul {
width: 30vw;
display: flex;
flex-direction: row;
justify-content: space-around;
}
#media (max-width: 650px) {
nav > ul {
flex-direction: column;
}
}
.dropdown > li{
float: right;
overflow: hidden;
}
.dropdown > li a {
font-size: 16px;
border: none;
outline: none;
color: #f4f4f4;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
nav > li a:hover, .dropdown:hover a {
background-color: #8781bd;
color:#f4f4f4;
}
.dropdown-content {
display: none;
position: absolute;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
color: #f4f4f4;
z-index: 1;
margin-top: 20px;
min-width: 100px;
}
.dropdown-content li a {
float: none;
color: #f4f4f4;
padding: 10px 14px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content li a:hover {
background-color: #625aa9;
color: #f4f4f4;
}
.dropdown:hover .dropdown-content {
display: block;
}
<header id="page-wrapper">
<header id="header">
<div class="logo">
<nav>
<a href="http://www.wrecklessdevelopment.com"><img id="header-img"
src="images/wreckless-development-logo.gif" alt="Wreckless Development Logo"/></a>
</nav>
</div>
<h1>Wreckless Development</h1>
<nav id="navbar">
<ul>
<li>About</li>
<li>Services</li>
<div class="dropdown">
<li><i class="fa fa-caret-down"></i> Portfolio<li>
<div class="dropdown-content">
<ul>
<li>Photography</li>
<li>Composite</li>
<li>Logos</li>
<li>Branding</li>
<li>Advertising</li>
</ul>
</div>
</div>
<li>Contact</li>
<li>Blog</li>
</ul>
</nav>
</header>
</header>
The problem is tag a's default display is inline, so if you want to adjust height of a tag, you have to change it's default display to display: inline-block like this, and then you may be able to do whatever you want with that a tag, you can refer my code below for more details:
#header a {
display: inline-block; // change display style
height: 75px;
line-height: 75px; // center the text
padding-left: 12px;
padding-right: 12px;
}
.dropdown > li > a {
padding: 0 16px; // no need to padding top and bottom because we already had line-height and height
}
.dropdown-content{
margin-top: 75px; // push the .dropdown-content further to fit new css
}
#header .dropdown-content li a{
display: block; // set an <a> tag to full with of the dropdown
height: auto;
line-height: 16px; // center the text with current font-size
}
you can take a look in my codepen.io for more details here. Hope it will help