Make z-index child higher than parent - javascript

I've seen several posts like this and I haven't found anything specifically targeting the problem I am having (maybe I didn't search enough, forgive me for that!).
Anyway, I basically need my child div to position itself over the parent div.
Here's the catch though, the child has to be nested inside the parent div, example:
<div id="parent">
<div id="child"></div>
</div>
My parent is basically a moving block (that you can move with arrow keys) and the child is basically an attribute to the parent and should move with it but over it as well.
Is this possible?
I also want to do this without having to write a separate movement code for the child.

You can use this CSS to peg the child to be the full size of the parent (which will cover the parent background):
#parent {position: relative; height: 200px; width: 200px;}
#child {position: absolute; height: 100%; width: 100%;}
Working demo that shows you can move the parent and the child stays covering it: http://jsfiddle.net/jfriend00/XGU4w/
Or, if you only have the one single child, then it's simply a matter of setting the child height and width to be 100% and then setting a size on the parent.
#parent {height: 200px; width: 200px;}
#child {height: 100%; width: 100%;}
Working demo: http://jsfiddle.net/jfriend00/s89a3/

Try this fiddle: http://jsfiddle.net/sNW8W/
You simply have to give the child element a higher z-index. The code from the previous answer would not work, providing no z-index. Remember: only elements that carry the position attribute can make use of the z-index property.
#parent {
position: relative;
background:red;
height:100px;
width: 100px;
z-index:0;
}
#child {
position: absolute;
top: -10px;
left: 25px;
background: green;
height: 50px;
width: 50px;
z-index: 10;
}

Related

Position a fixed element below another fixed element

I have two fixed elements, one of which can either have
display: block or display: none. The other fixed element is always going to be visible. I want the elements to stick to the top of the website, while having them not overlay each other.
The only proposed solution I found is in these questions:
How to position a fixed div under another fixed div?
Fixed element below fixed element without JS
Put your two divs inside another container div and set that one as
fixed.
I can't do that however, as both of these elements are on different positions in the code, which I am not able to change.
Here's a code snippet demonstrating my problem:
nav,
.secondmenu {
position: fixed;
height: 120px;
opacity: 1;
top: 0;
width: 100%;
background: lightgrey;
}
.secondmenu {
height: 50px;
background: grey;
opacity: 1;
z-index: 2;
}
body {
height: 1000px;
}
<div class="secondmenu">Might be there or not and overlays the other navigation</div>
<div>Some other stuff separating the two from each other with relative position</div>
<nav></nav>
What I want and things to keep in mind:
If both elements are visible, they should be fixed on top of the page, while one is below the other
If only the second element is visible, I want the second one to be fixed at the top of the page
The first element can change its visibility using inline styles (display:none <-> display:block, even without reloading the website)
Javascript/Jquery solutions are fine
this could bo done adding a 'top' with the height of the first nav to the second, like i did here.
Note: This is not the complete solution: If you want to show the second nav only you could do this using js by setting the 'top' back to 0.
nav,
.secondmenu {
position: fixed;
height: 120px;
opacity: 1;
top: 0;
width: 100%;
background: lightgrey;
}
.secondmenu {
height: 50px;
background: grey;
opacity: 1;
z-index: 2;
top: 120px;
}
body {
height: 1000px;
}
<div class="secondmenu">Might be there or not and overlays the other navigation</div>
<div>Some other stuff separating the two from each other with relative position</div>
<nav></nav>
Creating a holder for both of them is the proper approach.
But in your case you can position both of them fixed.
and when the other one is hidden or shown you can handled both elements style(i.e. top and left properties) via Javascript
Try like below
Have you tried Sticky Kit? http://leafo.net/sticky-kit/
Sticky-kit provides an easy way to attach elements to the page when the user scrolls such that the element is always visible.

Input field resize

I'm having trouble with re-sizing my search input bar. I've managed to make it re-size when the browser window gets smaller using width:100%;. But I can't get the starting width to be 300px without making it always 300px and then it's goes out of the parent div on resize. For some reason it's 185px;
Here's the jsfiddle of my set-up.
You are embeding your input into a span by calling $('.search2').typeahead. This span has the css-class twitter-typeahead wich is not set to width: 100%;
Add this to your CSS:
.twitter-typeahead {width: 100%;}
Then you need to change your div container css to this (width: 100%; max-width: 300px;):
.div2 {
float: left;
width: 100%;
max-width: 300px;
padding-left: 20px;
}
Leaving width: 100%; on your input is fine!
See the working code: http://jsfiddle.net/utg4mh6z/1/
Just remove all the float: left; and it works. It seems they are not required anyway. At least you don't state it anywhere and floating for 100% width elements makes no sense from my point of view.

Using JS CSS to set position causes different outcome than the same setting using pure CSS

Here is a fiddle to demonstrate the problem:
http://jsfiddle.net/6e1vg58L/
The javascript adds the "position:fixed" to the nav-content. Everything works how I want, the nav content stays in place while scrolling down the page. Now, if you go and put "position: fixed" under "#nav-content" in the CSS, and delete the JS, it should have the same outcome, correct?
For some reason setting the position in CSS or HTML causes the entire cell to dissapear, while setting it using Javascript or any browser inspector gives it the desired output?
$(document).on("scroll", function(){
if($(window).scrollTop() > 0)
{
$("#nav-content").css("position","fixed");
}
else
{
$("#nav-content").css("position","relative");
$("#nav-content").css("top",0);
}
});
vs
#nav-content {
position: fixed;
}
At first I thought it could be something with the listener causing it to work (but why?), but after opening it up in a live browser and adding the "position: fixed" through the inspector, it works exactly how it should. This is the problem, two out of four ways give the same, desired result, but the other two give the same, undesired result.
Although I am not 100% on the exact whys I think the reason is because by declaring it fixed has the following effect.
fixed
Do not leave space for the element. Instead, position it at a
specified position
so it means content being 100% is allowed to take the whole screen when the page is first rendered. Navigation (although not the one being fixed which is the confusing bit) is on the screen but hidden by the content at 100%. the interesting thing is if you use chrome to disable the fixed property the navigation appears and then because it is now on screen reapplying the position fixed does not hide it which is why the JS route behaves differently.
the changes to fix could defining the initial widths in % relative to each other.
#content {
position: relative;
background-color: #eee;
width: 70%;
max-width: 1300px;
min-width: 450px;
height: auto;
}
and then the same for navigation
#navigation {
width: 30%;
background-color: #000;
height: 100%;
position: relative;
top: 0;
bottom: 0;
}
http://jsfiddle.net/vemtyyox/
another way to keep the navigation at 300px could be to use calc to define the width of the content
#content {
position: relative;
background-color: #eee;
width: calc(100% - 300px);
max-width: 1300px;
min-width: 450px;
height: auto;
}
#navigation {
width: 300px;
background-color: #000;
height: 100%;
position: relative;
top: 0;
bottom: 0;
}
http://jsfiddle.net/9db77jvp/
Looking closer i think there is something odd about the way display:table-cell and the fixed properties are working, maybe.

How to reveal element by scrolling?

I'm trying to make an effect similar as used on http://www.t-mobile.com/ , when the user scrolls down to the bottom of the page they reveal the "footer" more and more as the user keeps on scrolling.
I've tried to search both here and on google but haven't been able to find anything that's really useful. Most examples only shows/hide the footer once the user scrolls to the bottom.
So my question is, what's the effect called to reveal an element by scrolling? Are there any good tutorials / blog posts about this? All help I can get is much appreciated!
As I commented, you need to make your element fixed, so as explanation goes, I have two elements here, one is a normal position: relative; element, so nothing fancy about that, I assigned relative so that I can make the z-index work
Second element is positioned fixed and also, make sure you use margin-bottom which should be equal to the height of your footer, no need to assign any negative z-index whatsoever to this element.
Demo
Not much HTML ...
<div></div>
<div>Reveal Me</div>
CSS
/* These are for your main site wrapper */
div:first-child {
height: 800px; /* Even auto is fine, I
used fixed height because I don't have any content here */
background: #eee;
margin-bottom: 200px; /* Equals footer wrappers height */
z-index: 1;
position: relative;
}
/* These are for footer wrapper */
div:last-child {
background: #aaa;
height: 200px;
position: fixed;
bottom: 0;
width: 100%;
}
For Dynamic Sizes
Note that am using a fixed height for the fixed positioned element, if you have variable height in footer element, than you need to use JS or jQuery to calculate the height using
$('#wrapperElement').css('margin-bottom', $('#footer').height());
Here, the selectors of #wrapperElement and #footer are my assumed ones, you can replace those with the your own selectors.
Something about fixed element - Horizontal Centering (I think it will be helpful to some users)
When you will make your element fixed, it will get out of the document flow, so if you are assigning fixed to the wrapper of footer element and want to center some content in there, than nest another element inside that wrapper and use width and margin: auto; for that...
Demo 2
HTML
<div></div>
<div>
<div>Reveal Me</div>
</div>
CSS
body > div:first-child {
height: 800px;
background: #eee;
margin-bottom: 200px;
z-index: 1;
position: relative;
}
body > div:last-child {
background: #aaa;
height: 200px;
position: fixed;
bottom: 0;
width: 100%;
}
body > div:last-child div {
width: 80%;
margin: auto;
outline: 1px solid red; /* To show that element is horizontally centered */
}
Note: Selectors used in this answer are too general and are good for
quick demonstration purposes, in real projects, make sure you use
specific selectors

Element Position in CSS

I'm still newbie in using CSS..
I really need help for doing our team's small project for mobile app :D
Here's my question :D
Is it possible for CSS to put "one element" above "another element" ??
For example I have "a wooden board" element and I have another element "sticks" ..
I would like the "stick" element behind "the wooden board" element.. So it's like a billboard style :D
(If in Photoshop, we can just drag the layer "sticks" below the layer "board")
If it is possible, then how to do it ?
I try to look for some sources but didn't get any result..
Any help would be appreciated :D (code/reference/etc)
Thanks a lot
NB : can't post the image :( since I just start to use stackoverflow, sorry :(
Yes you can, there are multiple ways of achieving that..
1st using position: relative; and z-index(optional)
Demo
<div class="one"></div>
<div class="two"></div>
.one {
height: 100px;
width: 100px;
background: #f00;
}
.two {
height: 100px;
width: 100px;
background: #0f0;
position: relative;
top: -70px;
left: 30px;
}
2nd you can use position: absolute; wrapped inside position: relative; container
Demo 2
<div class="one">
<div class="two"></div>
</div>
.one {
height: 100px;
width: 100px;
background: #f00;
position: relative;
}
.two {
height: 100px;
width: 100px;
background: #0f0;
position: absolute;
bottom: -30px;
left: 30px;
}
The first example is useful if you have 2 separate elements, you can use position: relative; and position the element accordingly using top, right, bottom and left properties.
The other example uses position: absolute; but did you see the change in markup? I've positioned absolute the child element, which is nested inside position: relative; element. I would prefer this solution.
z-index is optional, say if you want the bring up the 1st element over second
Demo
Make sure you use position property which is set to relative absolute or fixed as z-index doesn't work on static which is default position.
Also I would like to add up here, that I didn't provided z-index solution for Demo 2 as the elements are nested, so you cannot position a child element behind parent, it inherits parent elements z-index

Categories