It is not too much of an issue but I wanted to know why there is still a space between the navbar's border and an element that is at the corner. This is my HTML and CSS code along with an image of the problem I am referring to. I have changed the margins but there is still space, I also tried putting the elements on the same line as the element but the space still exists.
IMPORTANT: THE MARGIN DISAPPEARS WHEN THE PAGE IS ZOOMED INTO
a {
display: block;
text-decoration: none;
text-align: center;
padding: 1em;
margin: 0;
}
a:hover {
background-color: green;
}
ul,
li {
display: inline-block;
padding: 0;
margin: 0;
}
ul {
display: flex;
max-width: 50%;
justify-content: space-around;
flex-grow: 1;
}
nav {
position: sticky;
top: 0;
width: 90%;
margin: 0;
padding: 0;
border-left: 5px solid black;
border-right: 5px solid black;
margin: auto;
display: flex;
justify-content: space-between;
align-items: center;
background-color: greenyellow;
}
<nav>
Home
<ul>
<li>
Products
</li>
<li>
Contact
</li>
<li>
About
</li>
</ul>
Sign Up
</nav>
It should not happen now, I have gave margin: 8px 0 which was just margin: 8px
body {
margin: 8px 0;
}
a {
display: block;
text-decoration: none;
text-align: center;
padding: 1em;
margin: 0;
}
a:hover {
background-color: green;
}
ul,
li {
display: inline-block;
padding: 0;
margin: 0;
}
ul {
display: flex;
max-width: 50%;
justify-content: space-around;
flex-grow: 1;
}
nav {
position: sticky;
top: 0;
width: 90%;
margin: 0;
padding: 0;
border-left: 5px solid black;
border-right: 5px solid black;
margin: auto;
display: flex;
justify-content: space-between;
align-items: center;
background-color: greenyellow;
}
<nav>
Home
<ul>
<li>
Products
</li>
<li>
Contact
</li>
<li>
About
</li>
</ul>
Sign Up
</nav>
Related
I made this navigation but have several problems.
header pic comes after the nav but I want navigation to be on the header.
I want navigation to be sticky at the same time. That means when I scroll down, the navigation comes down with the same way and does not change.
how can I make best responsive?
how can I optimize it better in general?
nav {
position: absolute;
margin: 1.5em 30em;
display: flex;
justify-content: space-between;
width: 45%;
height: 10vh;
/* border: 3px dotted red; */
z-index: 999;
}
.navContainer {
position: relative;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
background-color: #d6d6d6;
border-radius: 10px;
opacity: 0.8;
/* border: 3px dotted green; */
}
.navContainer a {
color: #1b1b1b;
}
.logoContaienr img {
width: 150px;
height: auto;
}
.anmeldungsContainer {
position: relative;
top: 25px;
height: 50px;
color: #d6d6d6;
border-radius: 10px;
background-color: #034078;
margin-left: 20px;
/* border: 3px dotted blue; */
}
.anmeldungsContainer li {
padding-top: 0.5em;
}
.anmeldungsContainer a {
color: #d6d6d6;
}
nav ul {
display: flex;
justify-content: space-between;
list-style: none;
}
nav li {
margin: 0 2em;
text-decoration: none;
}
a {
display: block;
text-decoration: none;
font-size: 1.5em;
font-weight: bolder;
transition: 0.3s;
}
<nav>
<div class="navContainer">
<div class="linksMenuContainer">
<ul>
<li>Kurse</li>
<li>Ernährung</li>
</ul>
</div>
<div class="logoContaienr">
<img src="../Img/Logo.png" alt="">
</div>
<div class="rechtsMenuContainer">
<ul>
<li>Abos</li>
<li>Trainer</li>
</ul>
</div>
</div>
<div class="anmeldungsContainer">
<ul>
<li>Anmeldung</li>
</ul>
</div>
</nav>
I have modified the code overall as per my understanding of the requirement.
Please visit: https://codepen.io/emmeiWhite/pen/MWjXqpQ
Some major changes : ( The commented stuff is what we no longer require)
nav {
/* position: absolute; */
/* margin: 1.5em 30em; */
display: flex;
justify-content: center;
/* width: 45%; */
height: 50px;
border: 3px dotted red;
/* z-index: 999; */
position:fixed;
top:0;
left:0;
right:0;
}
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>
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;
}
Layout of what I'mm trying to achieve
I've done the top half of the nav bar and I'm trying to do the second part where the boxes (represent words), which I have circled in the image. I'm trying to directly make that section below the logo sign centered like the image shows but I am unsure on how to do that.
body {
margin: 0;
font-weight: 800;
}
.container {
width: 80%;
height: 100%;
margin: 0 auto;
display: flex;
/* align-items: center; */
justify-content: center;
}
header {
background: #ffe9e3;
height: 100px;
}
.logo {
text-align: center;
margin: 0;
display: block;
}
.business {
position: absolute;
right: 0;
top: 0;
padding: 10px;
}
.menu {}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
}
nav a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
nav a:hover {
color: #000;
}
nav a::before {
content: '';
display: block;
height: 5px;
background-color: #444;
position: absolute;
top: 0;
width: 0%;
transition: all ease-in-out 250ms;
}
nav a:hover::before {
width: 100%;
}
<header>
<div class="container">
<h1 class="logo"><i>LOGO</i></h1>
<nav class=m enu>
<ul>
<li>Hair</li>
<li>Nails</li>
<li>Makeup</li>
<li>Face</li>
</ul>
</nav>
<nav class=b usiness>
<ul>
<li>List Your Business</li>
</ul>
</nav>
<<div class="menu">
<nav>
</nav>
</div>
</div>
</header>
I have done the way you wanted it to look
CSS Part :
* {
padding: 0;
margin: 0;
}
body {
background: #333333;
min-width: 100vw;
min-height: 100vh;
}
.header {
height: 150px;
background: pink;
}
.logo {
padding: 10px;
text-align: center;
}
.nav > ul {
display: flex;
justify-content: space-evenly;
padding: 10px;
margin-top: 20px;
}
.nav > ul > li {
width: 100px;
list-style: none;
border: 2px solid #000;
border-radius: 20px;
}
.nav > ul > li > a {
text-decoration: none;
color: #fff;
font-size: 1.3rem;
padding: 3px 5px;
display: flex;
justify-content: center;
}
check the whole code here: https://codepen.io/the-wrong-guy/pen/GRoyKMa?editors=1100
And you have made a lot of syntax errors like not giving double quotes to the class names
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>