i have a navigation bar that sticks to the top of the page as the user scrolls, however as the user continues to scroll down the page, the navigation menu seems to disappear behind some of the pages elements.
any help with this matter will be greatly appreciated, below is all the code.
* {
margin: 0;
padding: 0;
list-style: none;
font-family: 'Segoe UI';
}
p,
h5 {
margin-bottom: 10px;
line-height: 1.5;
}
h5 {
text-align: center;
border: 1px solid #ccc;
border-width: 1px 0;
}
h5.fixed {
position: fixed;
top: 80px;
left: 0;
background-color: #fff;
right: 0;
}
.nav a {
text-decoration: none;
color: #fff;
display: block;
transition: .3s background-color;
}
.nav a:hover {
background-color: #005f5f;
}
.nav a.active {
background-color: #fff;
color: #444;
cursor: default;
}
/* Option 1 - Display Inline */
.nav li {
display: inline-block;
margin-right: -4px;
}
<h5>
<div class="nav">
<ul>
<li class="home">Home</li>
<li class="tutorials"><a class="active" href="#">Tutorials</a></li>
<li class="about">About</li>
<li class="news">Newsletter</li>
<li class="contact">Contact</li>
</ul>
</div>
</h5>
There are few issues in you code. I could not understand why you using h5 in fixed div. To solve issue try this
h5.fixed {
position: fixed;
top: 80px;
left: 0;
background-color: #fff;
right: 0;
z-index:999;
}
But my suggestion is use some better way to implement fixed top menu. Please check this example https://getbootstrap.com/examples/navbar-fixed-top/
it was ok I guess but color of font was white so it was not visible, I changed it to #000 and its working, please correct if I'm wrong
Plus I added z-index: 9; for your website purpose, if nav is fixed then it will be higher in "z" dimension "above" all elements. Don't forget to use "position: relative/absolute/fixed" etc to make z-index working :)
* {
margin: 0;
padding: 0;
list-style: none;
font-family: 'Segoe UI';
}
p,
h5 {
margin-bottom: 10px;
line-height: 1.5;
}
h5 {
text-align: center;
border: 1px solid #ccc;
border-width: 1px 0;
}
h5.fixed {
position: fixed;
top: 80px;
left: 0;
background-color: #fff;
right: 0;
}
.nav{
position: relative;
z-index: 9;
}
.nav a {
text-decoration: none;
color: #000;
display: block;
transition: .3s background-color;
}
.nav a:hover {
background-color: #005f5f;
}
.nav a.active {
background-color: #fff;
color: #444;
cursor: default;
}
/* Option 1 - Display Inline */
.nav li {
display: inline-block;
margin-right: -4px;
}
<h5>
<div class="nav">
<ul>
<li class="home">Home</li>
<li class="tutorials"><a class="active" href="#">Tutorials</a></li>
<li class="about">About</li>
<li class="news">Newsletter</li>
<li class="contact">Contact</li>
</ul>
</div>
</h5>
Try using the z-index css property. This will allow you to place the element above others on the page.
Try to use this code: z-index property only active when the its elements has relative or absolute value. otherwise it does not work.
elementname {
position: relative;
z-index: 1;
}
please see this link: https://jsfiddle.net/emumsbgp/
Related
I'm having trouble with the dropdown menu of my blog. When I hover over "Plamo Reviews", it doesn't drop down the sub-menus. I read some articles related to this but still cannot find any solution. I think I did something wrong with the CSS.
Could anyone help me?
I'd appreciate any help. Thank you in advance!
nav.fixnavbar {
position: relative;
display: block;
width: 100%;
background: #778595;
z-index: 99;
padding: 0;
margin-bottom: 0;
font-size: 0;
opacity: 1;
}
.fixednav {
position: relative;
margin: 0 auto;
padding: 0;
max-width: 1010px;
}
.fixednav li {
display: inline-block;
position: relative;
}
.fixednav li a {
display: inline-block;
padding: 20px;
color: #fff;
font-size: 14px;
font-weight: 700;
text-transform: uppercase;
}
.fixednav li a:hover {
background: #6c7a89;
color: #fff;
}
.nav-icon {
display: none;
}
nav.fixnavbar.main-nav-scrolled {
position: fixed;
top: 0;
left: 0;
opacity: 0.97;
-webkit-transform: translateZ(0);
transform: translateZ(0);
}
.sub-menu li {
display: none;
}
.fixednav li {
position: relative;
}
.sub-menu {
display: none;
position: absolute;
}
.fixednav li:hover .sub-menu {
display: block;
}
<nav class='fixnavbar'>
<ul class='fixednav' id='togglemenu'>
<li><a href='/'>Home</a></li>
<li><a href='/search/label/list'>List bài viết</a></li>
<li><a href='/search/label/kinh-nghiem'>Kinh nghiệm</a></li>
<li><a href='/search/label/review'>Plamo Reviews</a>
<ul class="sub-menu">
<li>No1</li>
<li>No2</li>
</ul>
</li>
<li class='indzign-box' onclick='document.getElementById('indzignbox').style.top='60px';document.getElementById('count-box').style.display='none';'><i class='fa fa-bars' /></li>
</ul>
</nav>
To achieve the behaviour you want, there are two things you should probably change:
You can get rid of the CSS rule that gives your sub-menus li elements a value of display: none as it's unnecessary. If the parent .sub-menu is set to display: none then the children will inherit that automatically.
You've set your .sub-menu to be position: absolute, but you haven't specified any additional CSS rules to indicate where it should go relative to its parent. So, for example, you can try making your .sub-menu rules look like the following:
.sub-menu {
display: none;
position: absolute;
top: 75px;
background: #ccc;
}
This way, you're saying that your menu should be positioned 75 pixels beneath its parent. The background is just so that your sub-menu is visible against the rest of your app (you can modify this to whatever you like, of course).
I don't think Stackoverflow is a place to get your homework done. Yet, here's a working sample with the minimal fixes to make it work.
You were missing the position (top) for the sub-menu and it was right beneath the fixed-nav.
There was no background-color for the sub-menu items, so the text which is white was not visible.
nav.fixnavbar {
position: relative;
display: block;
width: 100%;
background: #778595;
z-index: 99;
padding: 0;
margin-bottom: 0;
font-size: 0;
opacity: 1;
}
.fixednav {
position: relative;
margin: 0 auto;
padding: 0;
max-width: 1010px;
}
.fixednav li {
display: inline-block;
position: relative;
background-color: #6b7684;
}
.fixednav li a {
display: inline-block;
padding: 20px;
color: #fff;
font-size: 14px;
font-weight: 700;
text-transform: uppercase;
}
.fixednav li a:hover {
background: #6c7a89;
color: #fff;
}
.nav-icon {
display: none;
}
nav.fixnavbar.main-nav-scrolled {
position: fixed;
top: 0;
left: 0;
opacity: 0.97;
-webkit-transform: translateZ(0);
transform: translateZ(0);
}
.fixednav li {
position: relative;
}
.sub-menu {
display: none;
position: absolute;
top: 54px;
left: 0;
}
.fixednav li:hover .sub-menu {
display: block;
}
<nav class='fixnavbar'>
<ul class='fixednav' id='togglemenu'>
<li><a href='/'>Home</a></li>
<li><a href='/search/label/list'>List bài viết</a></li>
<li><a href='/search/label/kinh-nghiem'>Kinh nghiệm</a></li>
<li><a href='/search/label/review'>Plamo Reviews</a>
<ul class="sub-menu">
<li>No1</li>
<li>No2</li>
</ul>
</li>
<li class='indzign-box' onclick='document.getElementById('indzignbox').style.top='60px';document.getElementById('count-box').style.display='none';'><i class='fa fa-bars' /></li>
</ul>
</nav>
I'm trying to do a nav bar with a contact button stuck to the right of the page. I want contact to be aligned with the rest of the elements of the nav bar, however when I add float: right; it just disaligns my nav bar and it doesn't move contact to the right.
Here you can see my code: http://jsfiddle.net/LG2vR/19/
Can someone please tell me the proper way to accomplish this please?
Thanks!
Am not sure if this is what you need exactly, see the updated fiddle
http://jsfiddle.net/ov74xcyg/1/
Basically, i have used position absolute to move your last child of the navigation to the right side and increased width of the navigation till the end of the header.
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100px;
padding: 10px 100px;
z-index: 500;
box-sizing: content-box;
transition: .3s;
background: red;
}
nav.white {
background: white;
height: 35px;
padding: 10px 100px;
transition: .5s;
}
nav ul {
list-style: none;
float: left;
margin: 0;
padding: 0;
display: flex;
width: 90%;
position: relative;
}
nav ul li {
list-style: none;
}
nav ul li:last-child {
display: inline-block;
right: 5%;
position: absolute;
top: 0;
}
nav ul li a {
line-height: 80px;
color: #FFFFFF;
margin: 12px 30px;
text-decoration: none;
text-transform: capitalize;
transition: .5s;
padding: 10px 5px;
font-size: 16px;
}
nav ul li a:hover {
background-color: rgba(255,255,255,0.2);
}
nav.white ul li a {
color: #000;
line-height: 40px;
transition: .5s;
}
nav ul li a:focus {
outline: none;
}
<div class="wrapper">
<nav>
<ul>
<li>LOGO</li>
<li>Home</li>
<li>Page 1</li>
<li>Page 2</li>
<li><a class="contact" href="#">Contact</a></li>
</ul>
</nav>
Just add:
nav ul li a {
float: left;
}
right before:
nav ul li a.contact {
float: right;
}
or use flexbox!
.wrapper li {list-style-type: none;}
.wrapper {
overflow: hidden;
background-color: red;
}
.wrapper .logo{
margin-right : 30px;
}
.wrapper a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.wrapper a:hover {
background-color: #ddd;
color: black;
}
.wrapper a.active {
background-color: #4CAF50;
color: white;
}
.topnav-right {
float: right;
}
<div class="wrapper">
<a class="active logo" href="#">Logo</a>
Home
Page 1
Page 2
<div class="topnav-right">
<li><a class="contact" href="#">Contact</a></li>
</div>
</div>
Here's the code I have problem with my underline hover effect its exppanding extra how to fix it please help me out ************************
HTML
<div class="nav-wrap">
<ul class="group" id="example-one">
<li>Home</li>
<li><a class="dropbtn">Models</a>
<ul class="dropdown-content">
<li></li>
<li>Audi</li>
<li>Bmw</li>
<li>Mercedes</li>
</ul>
</li>
<li>Offers</li>
<li>Group Sales</li>
<li>Reviews</li>
</ul>
</div>
And also i want my dropdown in the middle of his parent And here its*********
CSS
.nav-wrap {
background: white;
width: 100%;
}
#example-one {
text-align: right;
}
#example-one li {
text-align: left;
position: relative;
display: inline-block;
font-family: 'Montserrat', sans-serif;
top:-20px;
}
#example-one a {
color: #000;
font-weight: bold;
font-size: 14px;
padding: 15px 15px;
text-decoration: none;
position: relative;
color: #000;
}
#example-one a:after {
color: #333;
content: '';
position: absolute;
width: 0;
height: 3px;
display: block;
margin-top: 2px;
right: 0;
background: #000;
transition: width all 1s ease;
-webkit-transition: width 1s ease;
}
#example-one a:hover:after {
width: 100%;
left: 0;
background: #000;
}
.dropdown-content {
display: none;
position: absolute;
top: 30px;
left: 0;
background-color:white;
z-index: 1;
}
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
}
#example-one li li {
display: block;
}
#example-one li:hover>ul {
display: block;
}
If I understand your problem correctly, the issue is with the dropdown items not being centered and the dropdown underline being too long. I believe this issue is caused by the padding on the dropdown div.
To resolve this, add some normalised padding to your .dropdown-content class:
.dropdown-content {
padding: 0 20px;
}
let the underline be attached to the text it self not to all the list item so it takes the width of just the text, now it takes the width of the whole cell in the list
Drop down issues
I'm trying to make a purely CSS dropdown menu and I have looked at other questions about this, but none of the solutions seem to work. The problem I have is that when I hover over the drop down button, it does display the menu, however it brings the whole nav-bar down with it.
body {
background-color: #15083E;
margin: 0;
}
.top-buttons {
position: fixed;
width: 100%;
z-index: 99;
}
.top-buttons #logo {
font-family: arial;
text-align: center;
padding: 14px;
background-color: white;
}
.top-buttons>ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #15083E;
border-style: solid;
border-bottom: 1px solid white;
position: relative;
}
.top-buttons>ul>li {
float: left;
}
.top-buttons>ul>li>ul {
display: none;
background: #666;
padding: 0;
position: relative;
top: 100%;
}
.top-buttons>ul>li a {
display: block;
color: white;
font-family: arial, sans-serif;
text-align: center;
padding: 14px 16px;
text-decoration: none;
min-width: 7em;
}
.top-buttons li a:hover {
background-color: #CC3E54;
}
.active {
background-color: #CC3E54;
}
.top-buttons ul li:hover>ul {
display: block;
position: relative;
float: none;
max-width: 7rem;
}
.top-buttons ul ul {
display: none;
position: absolute;
vertical-align: top;
}
.top-buttons>ul ul>li {
background-color: #15083E;
list-style-type: none;
}
.top-buttons>ul ul>li a {
background-color: #15083E;
}
<nav class="top-buttons">
<ul>
<li id="logo">Logo</li>
<li><a class="active" href="index.htm">Home</a></li>
<li>Placeholder</li>
<li><a>DropDown</a>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</li>
<li>Services</li>
</ul>
</nav>
My html probably isn't the best, i'm relatively new to it.
Any suggestions would be greatly appreciated!
Your HTML isn't bad. You were pretty close to a working solution. The main thing you were missing was that your dropdown needs to use absolute positioning inside it's relative positioned parent element. The reason the parent needs relative positioning is so that the absolute positioned child element (the dropdown menu) can be contained within it, otherwise it'll end up where you don't want it.
The other thing that was somewhat important to fix was overflow: hidden; on the top level ul. Once absolute positioning is in place for the dropdown, overflow hidden will hide anything that's outside of the ul. You don't want that. If you remove it then your bottom border doesn't end up where you want it so you need another way to clear the floated li. I used a clearfix to do this, hence the addition of the .cf class on the ul.
body {
background-color: #15083E;
margin: 0;
}
.top-buttons {
position: fixed;
width: 100%;
z-index: 99;
}
.top-buttons #logo {
font-family: arial;
text-align: center;
padding: 14px;
background-color: white;
}
.top-buttons>ul {
list-style-type: none;
margin: 0;
padding: 0;
/* overflow: hidden; */
background-color: #15083E;
border-style: solid;
border-bottom: 1px solid white;
/* position: relative; */
}
.top-buttons>ul>li {
position: relative;
/* added */
float: left;
}
.top-buttons>ul>li>ul {
display: none;
background: #666;
padding: 0;
/* position: relative; */
position: absolute;
/* top: 100%; */
}
.top-buttons>ul>li a {
display: block;
color: white;
font-family: arial, sans-serif;
text-align: center;
padding: 14px 16px;
text-decoration: none;
min-width: 7em;
}
.top-buttons li a:hover {
background-color: #CC3E54;
}
.active {
background-color: #CC3E54;
}
.top-buttons ul li:hover>ul {
display: block;
/* position: relative; */
/* float: none; */
max-width: 7rem;
}
/*
.top-buttons ul ul {
display: none;
position: absolute;
vertical-align: top;
}
*/
.top-buttons>ul ul>li {
background-color: #15083E;
list-style-type: none;
}
.top-buttons>ul ul>li a {
background-color: #15083E;
}
.cf:before,
.cf:after {
content: " ";
/* 1 */
display: table;
/* 2 */
}
.cf:after {
clear: both;
}
<nav class="top-buttons">
<ul class="cf">
<li id="logo">Logo</li>
<li><a class="active" href="index.htm">Home</a></li>
<li>Placeholder</li>
<li><a>DropDown</a>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</li>
<li>Services</li>
</ul>
</nav>
I see a few areas of improvement for the CSS but overall not bad.
I'm trying to remove some white space on the left of a fixed top menu ul but I'm stuck, I already searched and put this:
#nav li {
list-style: none;
display: inline-block;
}
I want to put all the list at the left (like left: 0?), but something is producing some white space on the left of the first list item.
Why is this happening, and how can I remove it?
Here is the code (fiddle):
#contenedormenu {
background-color: #FFFFFF;
width: 100%;
position: fixed;
top: 0px;
box-shadow: 0px 1px 2px #888;
height: 40px;
z-index: 1;
}
#menu {
width: 100%;
height: 40px;
position: fixed;
top: 0px;
}
#nav {
width: 100%;
height: 40px;
top: 0px;
position: fixed;
margin-top: 0px;
}
#nav li {
margin-top: 10px;
list-style: none;
display: inline-block;
}
#nav li a {
color: #5D5D5D;
font-family: Lobster, Arial, sans-serif;
font-size: 16px;
padding: 10px 5px 12px 5px;
text-decoration: none;
text-shadow: rgba(255, 255, 255, 0.6) 1px 1px 0;
}
#nav a:hover {
background-color: #f15a24;
color: white;
text-shadow: none;
}
<div id="contenedormenu">
<div id="menu">
<ul id="nav">
<li id="inicio">
Home
</li>
<li id="item1">
Menu item 2
</li>
<li id="item2">
Menu Item 3
</li>
</ul>
</div>
</div>
DEMO
As you code padding: 0; to the #nav
add padding:0; and margin:0 to the <ul>.
With <ul> tag set:
font-size: 0px;
With <li> tag set:
font-size: 14px; /*or custom*/
Other solution: https://davidwalsh.name/remove-whitespace-inline-block
Add padding: 0; to the <ul>.
By default ul have some margin and padding property due to that it apply. If we need to remove those then we need to apply below condition
ul {
margin:0;
padding: 0;
}