Z-index doesn't work in Sub Navigation - html

I have a problem where my z-indexes just won't kick in and I don't really get why.
I am not able to reposition the elements in the DOM, I need a purely CSS-based solution without any greater hacks
I have the following navigation:
<nav>
<div class="wrapper">
<ul>
<li>Test</li>
<li>Test
<ul>
<li>Sub test</li>
<li>Sub test</li>
</ul>
</li>
</ul>
</div>
</nav>
http://jsfiddle.net/S3XBD/1/
I packed it in a fiddle with the CSS to demonstrate my problem
my target is to get the blue box (Sub-Navigation) under the green box (Top level Navigation) in the exact same manner as demonstrated in the fiddle
Is there any way to fix my z-indexes or can someone point me to the mistake?
Thanks for your help

Just remove the z-index form the .wrapper element and change the z-index of the second ul to -1
nav .wrapper {
width: 100%;
max-width: 400px;
background: green;
margin: 0px auto;
position: relative;
}
...
nav ul ul {
display: none;
position: absolute;
top: 50%;
left: 0;
width: 100%;
background: blue;
z-index: -1;
}
http://jsfiddle.net/S3XBD/2/

I have updated your csss and removed hover as this is easy to fix: but the issue I believe you had was only in your positioning
nav {
width: 100%;
background: red;
}
nav .wrapper {
width: 100%;
max-width: 400px;
background: green;
margin: 0px auto;
position: relative;
z-index: 50 ;
}
nav ul {
list-style: none;
}
nav ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
width: 100%;
background: blue;
z-index: 100 !important;
}
nav ul li {
display: inline-block;
}
nav ul li ul {
display: block;
}
Just one thing more: User firebug to find out what it current style and you can modify it there on runtime. It also supports hover

Related

the anchorjump crops off the top of the page

I'm having a problem with my website, at the top of my page i made a list/menu with links, when you press the link the page jumps down to the topic you want, but the problem is when you press the link it crops off the top of the page, so you can't see the top 200px. How do I fix this? And why does it do that in the first place? I can see when you press the link the URL changes to "/#jump1" at the end.
This is the HTML:
<ul id="js-menu"> <li class="portfolio-menu" id="menu-li-1">Tema 2</li> <li class="portfolio-menu" id="menu-li-2">Tema 3</li> <li class="portfolio-menu" id="menu-li-3">Tema 4</li> <li class="portfolio-menu" id="menu-li-4">Tema 5</li> <li class="portfolio-menu" id="menu-li-5">Tema 6</li> </ul>
and this is how the #jump is applied:
<h2 id="jump-3">Tema 3</h2>
I tried searching on the web to find answers, but i have not found it... Please help.
Just take the time and find the solution yourself. There are countless options from CSS to JS, but here are some that you can give a try.
Easiest solution:
#jump-3 {
padding-top: 50px; /*height of your navbar*/
margin-top: -50px;
}
Another solution, taken from here, #LGT:
html {
height: calc(100vh - 84px); /* Fix the height. The 84px here is the height of your nav. */
margin-top: 84px;
overflow: hidden; /* Don't scroll. */
}
body {
height: 100%; /* Set the height to that of html. */
overflow-y: auto; /* Draw a vertical scrollbar when needed. */
}
Another solution:
#jump-3:target {
padding-top: 50px; /*height of your navbar*/
}
/*:taget pseudo class is used when user accesses the selected tag using href*/
This happens when your nav has a sticky or fixed position. You can fix it with scroll-padding-top or scroll-margin-top property in css.
The scroll-padding-top property is applied to the parent container and acts just like a CSS top padding, defining offsets from the top of the scrolling area. The scroll-margin-top property should be applied to each anchor section. Like:
nav {
position: sticky;
top: 0;
width: 100vw;
height: 60px; <--- Value for scroll-margin-top property
background: #F4F2F3;
}
section {
width: 100vw;
height: 100vh;
scroll-margin-top: 60px; <--- Value from nav height
padding: 1rem;
}
Here:
body {
margin: 0;
padding: 0;
}
nav {
position: sticky;
top: 0;
z-index: 999;
width: 100vw;
height: 60px;
background: #F4F2F3;
}
ul {
width: 100vw;
height: 60px;
margin: 0;
display: flex;
justify-content: space-around;
align-items: center;
list-style: none;
}
li {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
a {
color: #656256;
text-decoration: none;
}
section {
position: relative;
width: 100vw;
height: 100vh;
scroll-margin-top: 60px;
padding: 1rem;
color: #FFFFFF;
}
#anchor-1 {
background: #D3B88C;
}
#anchor-2 {
background: #9EBC9F;
}
#anchor-3 {
background: #656256;
}
<nav>
<ul>
<li> Menu link with anchor 1 </li>
<li> Menu link with anchor 2 </li>
<li> Menu link with anchor 3 </li>
</ul>
</nav>
<section id="anchor-1"> Section 1 </section>
<section id="anchor-2"> Section 2 </section>
<section id="anchor-3"> Section 3 </section>

Anchor Tags Not Visible

This is very weird. So I'm making a simple navigation menu. I added anchor tags inside my list items but the thing I don't understand is that the more I move them to the right, they're not showing up as links but just list items. I know in this demo it shows them working but in my code they don't show up as links. What is wrong? Do I need to add a pointing cursor? I thought links automatically gave you them.
.nav {
display: flex;
position: relative;
align-items: center;
height: 50px;
margin: 50px 200px 0 200px;
width: calc(100% - 400px); }
.nav a {
display: flex;
align-items: center; }
.nav a img {
height: 35px;
position: absolute;
left: 0px; }
.nav .desktop {
position: absolute;
right: 0; }
.nav .desktop ul li {
display: inline-block; }
<div class="nav">
<img src="/images/ff_logo_black.png" alt="" />
<div class="desktop">
<ul>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</div>
</div>
Turns out it was my h1 element covering them since it was positioned rather large. Silly me.

Make a nav bar stick

Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick
/* HEADER */
<div class="headercss">
<div class="headerlogo">
</div>
</div>
/* BODY */
body {
margin: 0px;
height: 2000px;
}
.headercss {
width: auto;
height: 320px;
position: relative;
}
.headerlogo {
width: auto;
height: 250px;
position: relative;
}
.nav {
width: auto;
height: 70px;
position: relative;
overflow: hidden;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
float:left;
width:100%;
overflow: hidden;
}
li {
float: left;
width:25%;
min-width: 243px;
overflow: hidden;
}
a:link, a:visited {
display: block;
height: 68px;
min-width: 243px;
overflow: hidden;
}
a:hover, a:active {
}
$(document).ready(function() {
$(window).scroll(function () {
//if you hard code, then use console
//.log to determine when you want the
//nav bar to stick.
console.log($(window).scrollTop())
if ($(window).scrollTop() > 280) {
$('#nav_bar').addClass('navbar-fixed');
}
if ($(window).scrollTop() < 281) {
$('#nav_bar').removeClass('navbar-fixed');
}
});
});
html, body {
height: 4000px;
}
.navbar-fixed {
top: 0;
z-index: 100;
position: fixed;
width: 100%;
}
#body_div {
top: 0;
position: relative;
height: 200px;
background-color: green;
}
#banner {
width: 100%;
height: 273px;
background-color: gray;
overflow: hidden;
}
#nav_bar {
border: 0;
background-color: #202020;
border-radius: 0px;
margin-bottom: 0;
height: 30px;
}
.nav_links {
margin: 0;
}
.nav_links li {
display: inline-block;
margin-top: 4px;
}
.nav_links li a {
padding: 0 15.5px;
color: #3498db;
text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner">
<h2>put what you want here</h2>
<p>just adjust javascript size to match this window</p>
</div>
<nav id='nav_bar'>
<ul class='nav_links'>
<li>Nav Bar</li>
<li>Sign In</li>
<li>Blog</li>
<li>About</li>
</ul>
</nav>
<div id='body_div'>
<p style='margin: 0; padding-top: 50px;'>and more stuff to continue scrolling here</p>
</div>
add to your .nav css block the
position: fixed
and it will work
I hope this can help someone. Determine the nav offset through js and then apply sticky position css to nav:
But first, we will define the styles in the stylesheet, like so.
.sticky {
position: fixed;
width: 100%;
left: 0;
top: 0;
z-index: 100;
border-top: 0;
}
Then, we will apply that class to the navigation conditionally with jQuery.
$(document).ready(function() {
var stickyNavTop = $('.nav').offset().top;
var stickyNav = function(){
var scrollTop = $(window).scrollTop();
if (scrollTop > stickyNavTop) {
$('.nav').addClass('sticky');
} else {
$('.nav').removeClass('sticky');
}
};
stickyNav();
$(window).scroll(function() {
stickyNav();
});
});
Just use z-index CSS property as described in the highest liked answer and the nav bar will stick to the top.
Example:
<div class="navigation">
<nav>
<ul>
<li>Home</li>
<li>Contact</li>
</ul>
</nav>
.navigation {
/* fixed keyword is fine too */
position: sticky;
top: 0;
z-index: 100;
/* z-index works pretty much like a layer:
the higher the z-index value, the greater
it will allow the navigation tag to stay on top
of other tags */
}
CSS:
.headercss {
width: 100%;
height: 320px;
background-color: #000000;
position: fixed;
}
Attribute position: fixed will keep it stuck, while other content will be scrollable. Don't forget to set width:100% to make it fill fully to the right.
Example
Give headercss position fixed.
.headercss {
width: 100%;
height: 320px;
background-color: #000000;
position: fixed;
top:0
}
Then give the content container a 320px padding-top, so it doesn't get behind the header.
You can do it with CSS only by creating your menu twice. It's not ideal but it gives you the opportunity have a different design for the menu once it's on top and you'll have nothing else than CSS, no jquery.
Here is an example with DIV (you can of course change it to NAV if you prefer):
<div id="hiddenmenu">
THIS IS MY HIDDEN MENU
</div>
<div id="header">
Here is my header with a lot of text and my main menu
</div>
<div id="body">
MY BODY
</div>
And then have the following CSS:
#hiddenmenu {
position: fixed;
top: 0;
z-index:1;
}
#header {
top: 0;
position:absolute;
z-index:2;
}
#body {
padding-top: 80px;
position:absolute;
z-index: auto;
}
Here is a fiddle for you to see: https://jsfiddle.net/brghtk4z/1/
/* Add css in your style */
.sticky-header {
position: fixed;
width: 100%;
left: 0;
top: 0;
z-index: 100;
border-top: 0;
transition: 0.3s;
}
/* and use this javascript code: */
$(document).ready(function() {
$(window).scroll(function () {
if ($(window).scrollTop() > ) {
$('.headercss').addClass('sticky-header');
} else{
$('.headercss').removeClass('sticky-header');
}
});
});
I would recommend to use Bootstrap. http://getbootstrap.com/. This approach is very straight-forward and light weight.
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-fixed-top">
<li> <br>BLINK</li>
<li><br>ADVERTISING WITH BLINK</li>
<li><br>EDUCATING WITH BLINK</li>
<li><br>ABOUT US</li>
</ul>
</div>
</div>
</div>
You need to include the Bootstrap into your project, which will include the necessary scripts and styles. Then just call the class 'navbar-fixed-top'. This will do the trick. See above example
Just Call this code and call it to your nave bar for sticky navbar
.sticky {
/*css for stickey navbar*/
position: sticky;
top: 0;
z-index: 100;
}
To make header sticky, first you have to give position: fixed; for header in css. Then you can adjust width and height etc. I would highly recommand to follow this article. How to create a sticky website header
Here is code as well to work around on header to make it sticky.
header {
position: fixed;
right: 0;
left: 0;
z-index: 999;
}
This code above will go inside your styles.css file.

CSS conflict with others div

I have tried to make a site but when I create a nav its conflict with body. body .main css showing marigin top in nav menu. but I set this for body content
You can check here jsfiddle
Here is the css code
*{margin: 0;
padding: 0;}
nav .navigation {margin: 0;
padding: 0;
position: fixed;
background: #333;
z-index: 999;
width: 100%}
nav .navigation li{display: inline-block;
padding: 5px 10px;}
nav .navigation li a{text-decoration: none;
color: #e1e1e1;}
nav .navigation li a:hover{color: #EDEDED}
.main { margin-top: 30px; }
.slide{background-attachment: fixed;
width: 100%;
height: 100%;
position: relative;
padding: 30px;
}
Here is the html code
<nav>
<ul class="navigation">
<li data-slide='1'>slide1</li>
<li data-slide='2'>slide2</li>
<li data-slide='3'>slide3</li>
<li data-slide='4'>slide4</li>
</ul>
</nav>
<div class="main">
<div class='slide' id='slide1' data-slide='1' data-stellar-background-ratio='0.5'>
<div class="container">
<div class="row">
<div class="col-md-6">
<h1>welcome</h1>
</div>
</div>
</div>
</div>
</div>
It's because of the 'position: fixed' on the navbar. Change that margin-top: 30px to padding-top: 30px;
Generally when using position: fixed, you should specify the position instead of leaving it up to the browser to figure out where to place the element. It's quite unlikely that you'll want the browsers default position when using position: fixed, as you're forcing it to come out of the flow anyway.
For example, on your nav .navigation selector, add something like top: 0.
nav .navigation {
top: 0;
margin: 0;
padding: 0;
position: fixed;
background: #333;
z-index: 999;
width: 100%
}
Just replace the code :
nav .navigation {margin : 0;
padding: 0;
position: fixed;
background: #333;
z-index: 999;
width: 100%;
top:0;
}
I have update your js fiddle : **http://jsfiddle.net/JLjT2/6/.** Please check
it because u set the position:fix in ( nav .navigation)
delete fix property, and if you want nev's position fix, then add top:0px; in same class
see here
nav .navigation
{
margin: 0;
padding: 0;
position: fixed;
background: #333;
z-index: 999;
width: 100%;
top:0px;}

Image spilling outside div

I have an image animating from right to left... The image is 6000px wide to give a longer duration to the animation. I put the image (clouds-layer-2.png) in a div (layer-2-container) and used the following CSS hoping that the image wouldn't spill over the div creating a horizontal scroll. But unfortunately it's not working and I have 6000px worth of horizontal scroll.
.layer-2-container {
width: auto;
overflow-y: scroll;
overflow-x: hidden;
z-index: 2;
}
This can be viewed at http://www.mike-griffin.com/testing. If you use CMD/CTRL+A to select all you'll be able to see the clouds better, if that helps.
I'll also paste all the code below.
-HTML-
<section class="home-section">
<div class="navigation">
<nav>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</nav>
</div><!--navigation-->
<div class="creative-designer">
<h2>| Heading one |</h2>
</div>
<div class="home-section-layers">
<img class="layer-1" src="img/white-M-layer-3.jpg" width="" height="">
<div class="layer-2-container">
<img class="layer-2" src="img/clouds-layer-2.png" width="" height="">
</div><!--layer-2-container-->
</div><!--home-section-layers-->
</section><!--home-section-->
</div><!--container-->
-CSS-
.container {
width: 100%;
height: auto;
max-width: 100%;
}
.home-section {
width: 100%;
height: auto;
position: absolute;
}
.layer-1 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: auto;
/*margin-top: -10%;*/
z-index: 1;
}
.layer-2-container {
width: auto;
overflow-y: scroll;
overflow-x: hidden;
z-index: 2;
}
.layer-2 {
position: absolute;
z-index: 2;
width: auto;
height: auto;
top: 0;
}
.creative-designer {
text-align: center;
position: relative;
z-index: 5;
color: #666;
margin-top: 40%;
}
.navigation {
color: #0C98D6;
position: relative;
z-index: 10;
top: 0px;
}
.navigation ul {
list-style: none;
width: 100%;
padding-right: 5%;
float: right;
}
.navigation ul li {
float: right;
padding: 10px;
}
Thank you for any help at all.
In the spirit of Stack Overflow, I'm answering your question exactly.
.home-section {
overflow-x: hidden;
}
I want to stress, though, that this site is really heavy, and you should code it such that there aren't going to be load issues on mobile. You can split the clouds in half, and/or use an SVG (the M should also be SVG). Additionally, your clouds are going to be overlapping content should you decide to add stuff on the sides, so you might want to set a border-radius to make the circle crop out the clouds.
Just suggestions, but hope they help!