I have a nav list that i centered, but there is some default spacing that i don't want. Also in the fiddle example you can't see it, but on my end the nav list isn't really centered with the same css etc. Maybe someone can look at the code to see if it really centers the nav list.
--> Fiddle
<header>
<nav>
<ul>
<li><span>Home</span></li>
<li><span>Work</span></li>
<li><span>Information</span></li>
</ul>
</nav>
</header>
You should remove the whitespace between the li elements. As example jsFiddle
<ul>
<li><span>Home</span></li><li>
<span>Work</span></li><li>
<span>Information</span></li>
</ul>
You can set font-size: 0 to ul element and on li the one you want. For example:
/************************************************
Site Name:
Author:
************************************************/
html {
margin: 0;
padding: 0;
position: relative;
min-height: 100%;
overflow-y: scroll;
}
body {
font-family: regular, helvetica, arial, sans-serif;
font-weight: normal;
font-size: 18px;
line-height: 1.4;
text-transform: none;
letter-spacing: 0;
color: #111;
}
body,
input,
textarea,
select,
button {
text-rendering: optimizeLegibility;
-webkit-font-smoothing: subpixel-antialiased;
-moz-osx-font-smoothing: grayscale;
}
:hover {
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
-ms-transition: all 0.3s;
-o-transition: all 0.3s;
transition: all 0.3s;
}
b,
strong {
font-weight: normal;
}
a,
a:visited {
color: #111;
text-decoration: underline;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
-webkit-tap-highlight-color: transparent;
}
h1,
nav {
font-size: 18px;
font-weight: normal;
text-transform: uppercase;
text-decoration: none;
text-align: center;
letter-spacing: 2px;
background-color: transparent;
}
h1 {
margin: 0 0 26px 0;
}
p {
margin: 0 0 16px 0;
background-color: transparent;
}
p a:hover,
a:focus,
a:active {
color: #111;
text-decoration: none;
}
h1 a,
a:visited {
color: #111;
text-decoration: none;
}
h1 a:hover,
a:focus,
a:active {
color: #111;
text-decoration: underline;
}
.center {
width: 500px;
height: 500px;
margin: 0 auto;
background-color: green;
}
/************************************************
Header - Footer - Navigation
************************************************/
header {
position: fixed;
width: 100%;
line-height: 55px;
top: 0;
left: 0;
margin: 0;
padding: 0;
z-index: 9999;
background-color: rgba(0, 0, 0, 0.70);
}
/* Navigation */
nav {
display: block;
text-align: center;
}
nav ul {
list-style-type: none;
padding: 0;
margin: 0;
font-size: 0;
/*set font size 0*/
}
nav li {
display: inline-block;
padding: 0 0;
font-size: 16px;
}
nav a {
position: relative;
display: block;
color: white;
text-decoration: none;
background-color: red;
}
nav a:visited,
a:active {
color: white;
text-decoration: none;
}
nav a:hover span {
transition: none;
text-decoration: none;
border-bottom: 2px solid white;
}
/* Dropdown */
/*nav li:hover a {
background-color: #444;
}
nav li ul {
position: absolute;
float: left;
z-index: 1;
display: none;
opacity: 0;
visibility: hidden;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
-ms-transition: all 0.3s;
-o-transition: all 0.3s;
transition: all 0.3s;
}
nav li:hover ul {
opacity: 1;
visibility: visible;
}
nav li ul li {
float: none;
width: 100%;
}
nav li ul a:hover {
color: #999;
}*/
<header>
<nav>
<ul>
<li><span>Home</span>
</li>
<li><span>Work</span>
</li>
<li><span>Information</span>
</li>
</ul>
</nav>
</header>
<div class="center"></div>
You can also set a border to li elements to seperate from each other.
Another way is to use the comment tags between the lines.
<ul>
<li><span>Home</span></li><!--
--><li><span>Work</span></li><!--
--><li><span>Information</span></li>
</ul>
Related
I'm working on a side navigation menu for my website using a bootstrap template. I'm a designer, not a developer, but I'm trying to learn by doing. I got everything working right, mostly. But I can't figure out why when I hover over the first or second menu items, the third one is expanded.
It's a parent for a submenu, but I've tried removing CSS rules one by one to see if that changes or fixes anything, but it doesn't seem to be working. Here is a link to the codepen that has bootstrap already loaded.
https://codepen.io/steve-mullen/pen/WNgNBjE
<html lang="en">
<body>
<header id="header" class="d-flex flex-column justify-content-center">
<nav id="navbar" class="navbar nav-menu">
<ul class="main-nav">
<li class="no-sub-nav">1<span>Menu 1</span></li>
<li class="no-sub-nav">2<span>Menu 2</span></li>
<li class="has-sub-nav">3<span>Menu 3</span>
<ul class="sub-nav">
<li><span>Sub 1</span></li>
<li><span>Sub 2</span></li>
<li><span>Sub 3</span></li>
<li><span>Sub 4</span></li>
</ul>
</li>
</ul>
</nav>
</header>
</body>
body {
font-family: "Open Sans", sans-serif;
color: #272829;
}
a {
color: #0563bb;
text-decoration: none;
}
a:hover {
color: #067ded;
text-decoration: none;
}
#header {
position: fixed;
top: 0;
left: 0;
bottom: 0;
z-index: 9997;
transition: all 0.5s;
padding: 15px;
overflow-y: auto;
}
#media (max-width: 991px) {
#header {
width: 300px;
background: #fff;
border-right: 1px solid #e6e9ec;
left: -300px;
}
}
/* Main Nav */
.nav-menu {
padding: 0;
display: block;
position: relative;
}
.nav-menu * {
margin: 0;
padding: 0;
list-style: none;
}
.nav-menu ul {
position: relative;
white-space: nowrap;
}
.no-sub-nav a,
.no-sub-nav a:focus {
display: flex;
align-items: center;
color: rgba(26,26,26,1.00);
padding: 10px 18px;
transition: 0.3s;
font-size: 15px;
border-radius: 50px;
background: #f2f3f5;
height: 56px;
width: 100%;
overflow: hidden;
}
.nav-menu a {
border: 4px #1a1a1a solid;
}
.nav-menu a i,
.nav-menu a:focus i {
font-size: 20px;
}
.nav-menu a span,
.nav-menu a:focus span {
padding: 0 5px 0 5px;
color: #1a1a1a;
}
#media (min-width: 992px) {
.nav-menu a,
.nav-menu a:focus {
width: 56px;
}
.nav-menu a span,
.nav-menu a:focus span {
display: none;
color: #fff;
}
.nav-menu li {
padding: 0;
}
.sub-nav {
display:none;
position: absolute;
}
.has-sub-nav:hover .sub-nav {
display: block;
transition: 0.3 sec;
position: absolute;
margin: 0;
width:100%;
padding-top: 8px;
padding-left:15px;
}
}
.nav-menu a:hover,
.nav-menu .active,
.nav-menu .active:focus,
.nav-menu:hover>a {
color: #fff;
background: #1a1a1a;
border: 4px white solid;
padding: 0 18px;
}
.nav-menu a:hover span,
.nav-menu .active span,
.nav-menu .active:focus span,
.nav-menu:hover>a span {
color: #fff;
}
.no-sub-nav a:hover,
.no-sub-nav:hover>a {
width: 100%;
color: #fff;
}
.no-sub-nav a:hover span,
.no-sub-nav:hover>a span{
display: block;
}
/* Sub Nav */
.has-sub-nav {
padding: 0;
display: block;
position: relative;
}
.has-sub-nav * {
margin: 0;
padding: 0;
list-style: none;
}
.has-sub-nav ul {
position: relative;
white-space: nowrap;
}
.no-sub-nav {
margin-bottom: 8px;
}
.has-sub-nav li {
padding-bottom:8px;
}
.has-sub-nav a,
.has-sub-nav a:focus {
display: flex;
align-items: center;
color: rgba(26,26,26,1.00);
padding: 10px 18px;
transition: 0.3s;
font-size: 15px;
border-radius: 50px;
background: #f2f3f5;
height: 56px;
width: 100%;
overflow: hidden;
}
.has-sub-nav a {
border: 4px #1a1a1a solid;
}
.has-sub-nav a span,
.has-sub-nav a:focus span {
padding: 0 5px 0 5px;
color: #1a1a1a;
}
.has-sub-nav a:hover,
.has-sub-nav .active,
.has-sub-nav .active:focus,
.has-sub-nav:hover>a {
color: #fff;
background: #1a1a1a;
border: 4px white solid;
padding: 0 18px;
}
.has-sub-nav a:hover span,
.has-sub-nav .active span,
.has-sub-nav .active:focus span,
.has-sub-nav:hover>a span {
color: #fff;
}
.has-sub-nav a:hover,
.has-sub-nav:hover>a {
width: 100%;
color: #fff;
}
.has-sub-nav a:hover span,
.has-sub-nav:hover>a span{
display: block;
}
I found the issue in your CSS, you just need to remove the width: 100% in
.has-sub-nav a,
.has-sub-nav a:focus
and it will works as normal.
This is because the width:100% make the 3rd menu to fit to its container's(ul) width, so whenenver 1st or 2nd menu is expanding, ul is expanding and cause 3rd to following its parent width then cause the issue.
https://codepen.io/zeikman/pen/dyqPZKW
I'm using a bootstrap template and trying to create a submenu for the third menu item that will display four sub-items. For one, I can't get them to display properly, and I can't get them to stay open to hover over them. I'm a designer, not a developer, but I'm trying to learn a little. Can anyone tell me what I'm doing wrong?
I've looked through numerous Q&A's on here, but none seem to work for me. Ideally, I would like the submenu items to be dots that expand when you hover over the parent. I tried that at first, and almost got it, but they would close before I could hover over them, and they wouldn't open when you hovered over the parent. I feel like I'm close, but I've been working at this for the entire day.
<html>
<body>
<header id="header" class="d-flex flex-column justify-content-center">
<nav id="navbar" class="navbar nav-menu">
<ul class="main-nav">
<li>H<span>Home</span></li>
<li>A<span>About</span></li>
<li>P<span>Portfolio</span>
<ul class="nav-sub">
<li><span>Sub 1</span></li>
<li><span>Sub 2</span></li>
<li><span>Sub 3</span></li>
<li><span>Sub 4</span></li>
</ul>
</li>
</ul>
</nav>
</header>
</body>
</html>
body {
font-family: "Open Sans", sans-serif;
color: #272829;
}
a {
color: #0563bb;
text-decoration: none;
}
a:hover {
color: #067ded;
text-decoration: none;
}
#header {
position: fixed;
top: 0;
left: 0;
bottom: 0;
z-index: 9997;
transition: all 0.5s;
padding: 15px;
overflow-y: auto;
}
.nav-menu {
padding: 0;
display: block;
position: relative;
}
.nav-menu * {
margin: 0;
padding: 0;
list-style: none;
}
.nav-menu ul {
position: relative;
white-space: nowrap;
}
.nav-menu a,
.nav-menu a:focus {
display: flex;
align-items: center;
color: rgba(26, 26, 26, 1);
padding: 10px 18px;
margin-bottom: 8px;
transition: 0.3s;
font-size: 15px;
border-radius: 50px;
background: #f2f3f5;
height: 56px;
width: 100%;
overflow: hidden;
}
.nav-menu a {
border: 4px #1a1a1a solid;
padding-left: 15px !important;
}
.nav-menu a span,
.nav-menu a:focus span {
padding: 0 5px 0 5px;
color: #1a1a1a;
display: none;
}
.nav-sub li,
.nav-sub li:focus {
display: none;
}
.nav-menu a:hover,
.nav-menu .active,
.nav-menu .active:focus,
.nav-menu:hover > a {
color: #fff;
background: #1a1a1a;
border: 4px white solid;
padding: 0 20px;
}
.nav-menu a:hover span,
.nav-menu .active span,
.nav-menu .active:focus span,
.nav-menu:hover > a span {
color: #fff;
}
.nav-menu a:hover,
.nav-menu:hover > a {
width: 100%;
color: #fff;
}
.nav-menu a:hover span,
.nav-menu:hover > a span {
display: block;
}
.main-nav > li:hover .nav-sub {
position: absolute;
width: 100%;
opacity: 1;
margin-bottom: 8px;
display: block;
}
.main-nav > li:hover .nav-sub li span {
width: 100%;
}
.nav-sub {
margin-bottom: 8px;
padding: 0;
position: absolute;
display: none;
width: 100%;
height: auto;
text-align: center;
opacity: 0;
transition: all 0.5s;
}
.nav-sub li {
display: flex;
margin: 0;
width: 100%;
}
The main reason was a margin-bottom: 8px on the last a tag, so there is a gap between the menu item and sub-menu. I made a quick fix for this issue. But there is still a lot of work to do.
body {
font-family: "Open Sans", sans-serif;
color: #272829;
}
a {
color: #0563bb;
text-decoration: none;
}
a:hover {
color: #067ded;
text-decoration: none;
}
#header {
position: fixed;
top: 0;
left: 0;
bottom: 0;
z-index: 9997;
transition: all 0.5s;
padding: 15px;
overflow-y: auto;
}
.nav-menu {
padding: 0;
display: block;
position: relative;
}
.nav-menu * {
margin: 0;
padding: 0;
list-style: none;
}
.nav-menu ul {
position: relative;
white-space: nowrap;
}
.nav-menu a,
.nav-menu a:focus {
display: flex;
align-items: center;
color: rgba(26, 26, 26, 1);
padding: 10px 18px;
margin-bottom: 8px;
transition: 0.3s;
font-size: 15px;
border-radius: 50px;
background: #f2f3f5;
height: 56px;
width: 100%;
overflow: hidden;
}
.nav-menu .has-nav-sub a,
.nav-menu .has-nav-sub a:focus {
margin-bottom: 0px;
}
.nav-menu a {
border: 4px #1a1a1a solid;
padding-left: 15px !important;
}
.nav-menu a span,
.nav-menu a:focus span {
padding: 0 5px 0 5px;
color: #1a1a1a;
display: none;
}
.nav-sub li,
.nav-sub li:focus {
display: none;
}
.nav-menu a:hover,
.nav-menu .active,
.nav-menu .active:focus,
.nav-menu:hover>a {
color: #fff;
background: #1a1a1a;
border: 4px white solid;
padding: 0 20px;
}
.nav-menu a:hover span,
.nav-menu .active span,
.nav-menu .active:focus span,
.nav-menu:hover>a span {
color: #fff;
}
.nav-menu a:hover,
.nav-menu:hover>a {
width: 100%;
color: #fff;
}
.nav-menu a:hover span,
.nav-menu:hover>a span {
display: block;
}
.main-nav>li:hover .nav-sub {
position: absolute;
width: 100%;
opacity: 1;
display: block;
}
.main-nav>li:hover .nav-sub li span {
width: 100%;
}
.nav-sub {
padding: 0;
position: absolute;
display: none;
width: 100%;
height: auto;
text-align: center;
opacity: 0;
transition: all 0.5s;
}
.nav-sub li {
display: flex;
margin: 0;
width: 100%;
}
<html>
<body>
<header id="header" class="d-flex flex-column justify-content-center">
<nav id="navbar" class="navbar nav-menu">
<ul class="main-nav">
<li>H<span>Home</span></li>
<li>A<span>About</span></li>
<li class="has-nav-sub">P<span>Portfolio</span>
<ul class="nav-sub">
<li><span>Sub 1</span></li>
<li><span>Sub 2</span></li>
<li><span>Sub 3</span></li>
<li><span>Sub 4</span></li>
</ul>
</li>
</ul>
</nav>
</header>
</body>
</html>
I am writing a new web page, and I am stuck In the navbar specify the responsive part...
when I zoom out the web page, the dropdown menu does not align itself with the parent link
Is that normal? I tried to use rem and percentage units in the left CSS property for the dropdown ul but that didn't work,, Is there a way to fix it or I have to put the navbar into a container and center it?
my code:
* {
box-sizing: border-box
}
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
}
nav {
display: grid;
grid-template-columns: 5rem 1fr;
align-items: center;
background-color: #000000;
}
.logo, .menu {
margin-left: 2rem;
}
.logo h1 {
color: #FFF;
text-transform: uppercase;
font-size: 1.2rem;
font-weight: 600;
letter-spacing: 5px;
}
.menu {
display: block;
position: relative;
}
.menu li {
display: inline-block;
color: #808080;
text-transform: uppercase;
list-style: none;
font-size: .9rem;
font-weight: 400;
letter-spacing: 1px;
padding: .4rem .4rem;
}
.menu .active {
color: #FFF;
}
.menu ul {
width: 20%;
height: 0;
opacity: 0;
position: absolute;
left: 30%;
top: 25px;
border-radius: 4px;
z-index: 1;
padding: 10px 0;
background: #FFF;
box-shadow: 0 2px 30px 0px rgba(0, 0, 0, 0.2);
transition: all .5s ease;
}
.menu > li:nth-of-type(4) > a {
display: block;
text-align: center;
text-decoration: none;
color: #808080;
}
.menu > li:nth-of-type(4) > a::after, .menu ul:before {
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
font-family: "Font Awesome 5 Free";
}
.menu ul:before {
font-weight: 900;
position: absolute;
top: -15px;
left: 25px;
content: "\f0d8";
color: #fff;
font-size: 25px;
}
.menu > li:nth-of-type(4) > a::after {
font-weight: 900;
padding-left: 5px;
content: "\f0d7";
}
.menu ul li {
display: none;
padding: 1rem 0;
text-indent: 20px;
cursor: pointer;
transition: .5s all ease-out;
}
.menu ul li a {
color: #000;
text-decoration: none;
letter-spacing: normal;
text-transform: capitalize;
}
.menu ul li:hover {
background-color: #e74208;
}
.menu ul li:hover a {
color: #fff;
}
.menu li:nth-of-type(4):hover ul {
height: auto;
opacity: 1;
}
.menu li:nth-of-type(4):hover ul li {
display: block;
}
<nav>
<div class="logo"><!-- Logo -->
<h1>Hexa</h1>
</div>
<ul class="menu"><!-- Nav Menu -->
<li class="active">Home</li>
<li>About</li>
<li>Projects</li>
<li>
Parent Link
<ul>
<li>Architectural Design</li>
<li>Interior</li>
<li>Building</li>
</ul>
</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</nav>
To position your dropdown menu you should add a position relative to is parent li. You are positioning the dropdown absolutely in relation to your .menu
.menu > li:nth-of-type(4) {
position: relative;
}
I created a navigation menu for a re-design of a website that's a Printing Company. Everything is working perfectly, but I can't find out why the Navigation shrinks when you change the browser size. I've created these menus before, and I've never had a problem with shrinking.
Here is the Demo https://jsfiddle.net/dshojaei/vbcztkdy/3/embedded/result/
#nav_wrap {
text-align: center;
background-color:#343232;
border-top:1px solid black;
height:54px;
}
/* Reset */
.nav,
.nav a,
.nav ul,
.nav li,
.nav div,
.nav form,
.nav input {
margin: 0;
padding: 0;
border: none;
outline: none;
}
.nav a { text-decoration: none; }
.nav li { list-style: none; }
/* Menu Container */
.nav {
display: inline-block;
position: relative;
cursor: default;
z-index: 500;
text-align:left;
}
/* Menu List */
.nav > li {
display:block;
float: left;
}
/* Menu Links */
.nav > li > a {
position: relative;
display: block;
z-index: 510;
height: 54px;
padding: 0 20px;
line-height: 54px;
font-family: Helvetica, Arial, sans-serif;
font-weight: bold;
font-size: 13px;
color: #fcfcfc;
text-shadow: 0 0 1px rgba(0,0,0,.35);
background: #343232;
border-left:none;
border-right:none;
-webkit-transition: all .3s ease;
-moz-transition: all .3s ease;
-o-transition: all .3s ease;
-ms-transition: all .3s ease;
transition: all .3s ease;
}
.businesscards:hover > a {
background:#009ad6;
}
.flyers:hover > a{
background:#c60077;
}
.cards:hover > a {
background:#cec41e;
}
.banner:hover > a {
background:#000000;
}
.dvd:hover > a {
background:#3c3c3c;
}
.stationary:hover > a {
background:#7b7b7b;
}
.labels:hover > a {
background:#afafaf;
}
.catalogs:hover > a {
background:#d7d7d7;
}
.nav > li:first-child > a {
border-radius: 0px 0 0 0px;
border-left: none;
}
/* Menu Dropdown */
.nav > li > div {
position: absolute;
display: block;
width: 100%;
top: 55px;
left: 0;
opacity: 0;
visibility: hidden;
overflow: hidden;
border-right:1px solid black;
border-left:1px solid black;
border-bottom:1px solid black;
background: #ffffff;
border-radius: 0 0 3px 3px;
-webkit-transition: all .3s ease .15s;
-moz-transition: all .3s ease .15s;
-o-transition: all .3s ease .15s;
-ms-transition: all .3s ease .15s;
transition: all .3s ease .15s;
}
.nav > li:hover > div {
opacity: 1;
visibility: visible;
overflow: visible;
}
/* Menu Content Styles */
.nav .nav-column {
float: left;
width: 20%;
padding: 2.5%;
}
.nav .nav-column h3 {
margin: 20px 0 10px 0;
line-height: 18px;
display:inline;
font-family: Helvetica, Arial, sans-serif;
font-weight: bold;
font-size: 14px;
color: #372f2b;
text-transform: uppercase;
}
/*.nav .nav-column img {
display:inline;
width:20%;
height: 20%;
}
*/
.nav img:nth-of-type(1) {
margin-top:30px;
margin-left:20px;
float:left;
width:5%;
height:5%;
}
.nav img:nth-of-type(2) {
margin-top:30px;
margin-left:20px;
float:left;
width:5%;
height:5%;
}
#brochure {
margin-top:30px;
margin-left:20px;
float:left;
width:3%;
height:3%
}
.nav .nav-column h3.orange { color: #ff722b; }
.nav .nav-column li a {
display: block;
line-height: 26px;
font-family: Helvetica, Arial, sans-serif;
font-weight: bold;
font-size: 13px;
color: #888888;
}
.nav .nav-column li a:hover { color: #666666; }
fix the width.
.nav {
display: inline-block;
position: relative;
cursor: default;
z-index: 500;
text-align: left;
width: 1190px;
}
You can change the code for the list items to the following:
.nav > li {
display: inline-block;
}
And, add the following declaration to the list rule set:
.nav {
display: inline-block;
position: relative;
cursor: default;
z-index: 500;
text-align:left;
white-space: nowrap;
}
In order, for this to display properly, the list items, in HTML, should be next to each other, otherwise you'll see spaces:
<ul><li></li><li></li></ul>
I'm trying to center the Menu Links but I can't. (Link to my website). As you can notice, the 'Home' and 'Staff' links are aligned at the left side of the page, but I want them to be centered. (With percentage, if possible)
Here you have the CSS Code
/*Navigation*/
#menu {
width: 100%;
float: left;
margin: 0 0 3em 0;
padding: 0;
list-style: none;
background-color: #1C1C1C;
margin-top:85px;
}
#menu li {
float: left; }
#menu li a {
display: block;
padding: 30px 30px;
text-decoration: none;
font-weight: bold;
color: #fff;
transition: background-color 0.5s ease;
transition: color 0.5s ease;
}
#menu li a:hover {
color: #69bfe7;
background-color: #2E2E2E;
transition: background-color 0.5s ease;
transition: color 0.5s ease;
}
Here the HTML Code
<!-- Here's all it takes to make this navigation bar. -->
<ul id='menu'>
<li><a href='#'>Home</a></li>
<li><a href='#'>Staff</a></li>
</ul>
<!-- That's it! -->
Try this:
#menu {
background-color: #1c1c1c;
float: left;
list-style: outside none none;
margin: 85px 0 3em;
padding: 0;
text-align: center;
width: 100%;
}
#menu li {
display: inline-block;
/* float: left; */
}
This should do the trick:
#menu {
width: 100%;
margin: 0 0 3em 0;
padding: 0;
list-style: none;
background-color: #1C1C1C;
margin-top:85px;
text-align: center;
}
#menu li {
display: inline-block;
}