Z-index not working as expected - html

I am developing a webpage and I am having problems with z-index not working as expected. My structure looks like this:
<div class="mainWrapper">
<div class="overlay"></div>
<div class="main">
<div class="content1">
content goes here
</div>
</div>
</div>
In overlay, I have a picture that should be above the main-class, but under the content-class. I've tried simply adding different z-indexes, but it does not seems to work. How can I solve this problem? Does this happen because the overlay-div is outside the other divs?

<style>
.mainWrapper{ position:relative; }
.overlay{ position:absolute; }
.main{ position:absolute; }
.content1{ position:absolute; }
</style>
<div class="mainWrapper">
<div class="main"></div>
<div class="overlay"></div>
<div class="content1">
content goes here
</div>
</div>
With absolute positioning, Items will get layered in the order they added to the page. So here, main will be the bottom layer, then overlay on top of that, then content1 above that.

Hard to tell what is happening without seeing the CSS, but...
In order for z-index to work properly, all of the elements you are working with need to be given an explicit position.
Some info:
http://reference.sitepoint.com/css/z-index
http://reference.sitepoint.com/css/stacking

It works with right css like this:
.overlay {
height: 300px;
width: 200px;
background-color: blue;
position: absolute;
top: 0px;
left: 0px;
z-index: 3;}
.main{
height: 250px;
width: 150px;
background-color: red;
position: absolute;
top: 0px;
left: 0px;}
.content1{
height: 200px;
width: 100px;
background-color: yellow;
position: absolute;
top: 0px;
left: 0px;}
.mainWrapper{
position: absolute;
}
example HERE

Related

Using sticky position with absolute together

I have this code
#wrapper {position: relative;}
#content {padding-top: 100px;}
#search {position: absolute; /*position: sticky; position: -webkit-sticky;*/ top: 0; left: 40%; height: 100px; background: red; width: 20%;}
<div id="wrapper">
<div id="content">
content<br>content<br>content<br>content<br>content<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>content
</div>
<div id="sibebar">...</div>
<div id="footer">...</div>
<div id="search"></div>
</div>
I need to have #search element in the code after #content element, positioned absolutely to the top. After scrolling I need to fix search element at the top of the page.
Any ideas?
I've tried to add #search_wrap with absolute position and #search with sticky position, it didn't work.
Thanks.
grid (again) can rescue you to avoid but simulate position:absolute; and use position:sticky;
Set both element in the same grid cell.
#wrapper {
position: relative;
display: grid;
}
#content,
#search {
grid-row: 1;
grid-column: 1;
}
#content {
padding-top: 100px;
}
#search {
position: sticky;
top: 0;
margin: 0 auto;
height: 100px;
background: red;
width: 20%;
}
<div id="wrapper">
<div id="content">
content<br>content<br>content<br>content<br>content<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>content
</div>
<div id="sibebar">...</div>
<div id="footer">...</div>
<div id="search"></div>
</div>
Have you considered doing it all in javascript instead of css only? that will give you more control about what you are trying to achieve:
you can attach / detach css elements to make sure your custom behaviour is achieved
if you are trying to mix some positions then you should absolutely investigate the javascript path.
I have written an article that wraps the javascript implementation in a hook that you might find useful. I could rewrite everything but it's easier that you check it our here.
hope my answer serves as a guidance :)

fixed div on top displaying layer problem

I am trying to make a div fixed on the top but looks like the layer overlaps.
CSS:
#fsancy {
background-color:#ddd;
position: fixed;
display: block;
width: 200px;
height: 100px;
left: 50%;
top: 0%;
margin-left: -100px; /*half the width*/
}
HTML:
<div class="container" id="fsancy">
<div class="row">
<div class="col-lg-12 text-center fluid fixme" id=""
style="background-color: #ff0033; max-width: 100%; color: #ffffff; font-size: xx-large">Share £200 With A
Friend
</div>
</div>
Picture example
Fixed position elements are not part of the regular document flow, so in your particular case you have to add some margin-top to the first regular element which is high enough to avoid the overlap / fit under the fixed header.
#Michelbach Alin, use position absolute and z-index properties for fix as a layer.
{
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
Reference.
https://www.w3schools.com/cssref/pr_pos_z-index.asp

Using HTML Divs to Make Navigation Bar

I'm just starting web development, and I'm trying to construct a page-navigation bar with a bunch of 'Div' elements, but I cant seem to get my second button ("AboutButton") to appear on the screen. Here is my HTML body code:
<body>
<div id="navbar" id="top">
<div id="pageselection">
<div id="HomeButton"> HOME </div>
<div id="AboutButton"> <a href="#About> ABOUT </a> </div>
</div>
</div>
</body>
and here is my CSS for the page:
#navbar{
Position: fixed;
Width: 100%;
Height: 10%;
}
#pageselection{
Position: absolute;
Width: 40%;
Height: 100%;
Right: 30%;
}
#HomeButton{
Position: absolute;
Width: 33%;
Height: 100%;
Left: 0%;
text-align: center;
}
#AboutButton{
Position: absolute;
Width: 33%;
Height: 100%;
Left: 33%;
text-align: center;
}
Essentially, I'm trying to build a 'div' that runs across the top of the page, then mark off the central 40% of that 'div' as a space for a second 'div' to sit, which will in turn hold the three links that users can use to navigate the page.
Thanks in advance, hopefully its a stupid error, and I hope my description was clear enough :)
You missed an quote mark in
<a href="#About>
If you are only starting, i recommend you to use some free editors like Atom or Sublime that will alert you about this kind of errors.
Also, it is not allowed to use "id" two times on an element. A better practice would be to use classes where you will be able to stack them like:
<div class="navbar top"></div>
You missed an ending quote " in <a href="#About>. I also recommend another approach on your nav bar. Don't absolute position the links in the nav bar, that's not needed. And also, one element can have one ID, not multiple. I've made some changes and used classes instead of IDs below. Please have a look.
#navbar{
position: fixed;
width: 100%;
height: 10%;
top: 0;
left: 0;
background-color: whitesmoke;
height: 40px;
}
#pageselection{
position: absolute;
width: 40%;
height: 100%;
right: 30%;
background-color: gray;
}
.nav-button{
float:left;
width:33%;
height: 100%;
text-align: center;
line-height:40px;
}
.nav-button a{
display:block;
height: 100%;
}
<div id="navbar" id="top">
<div id="pageselection">
<div class="nav-button">HOME</div>
<div class="nav-button">ABOUT</div>
<div class="nav-button">TEST</div>
</div>
</div>

top:-50% not working?

Working on a my example from this tutorial for making a unique CSS for my website, I found a trouble in the code :
HTML
<div class="items">
<div class="outerContainer">
<div class="innerContainer">
<div class="element">Finally over four lines, all the code is the same for each list</div>
</div>
</div>
</div>
CSS
.items .outerContainer
{
height: 180px;
width: 200px;
background-color: #EBEBEB;
position: relative;
}
.items .outerContainer .innerContainer
{
width: 100%;
text-align: center;
position: absolute;
top: 50%;
background-color:red;
}
.items .outerContainer .innerContainer .element
{
position: relative;
top: -50%;
}
why top: -50%; of .items .outerContainer .innerContainer .element doesnt "move" the element to the top? If I write top: -20px for example works well, but I want %.
Why? And how can I fix it?
Strange, it works only on IE7 :)
Moving an element by percentage requires the containing element to have a height attribute. So if you put height:90px; (Which I gather is correct as the item is 180px high and the innercontainer should be 50% from the top) it should work.

Nested divs with mixed height mode (% & px) Keep the '%' div fill out space not used by the 'px' div

I want the "blue" container to always be 70px high, while the previous "green" div always max out the height available when the div is resized with javascript.
I've played around with it for a while without finding a proper solution. Help will be appreciated.
As promised, here's my answer.
absolute inside relative positioning is the easiest way to do this.
Live Demo
HTML:
<div id="parent">
<div id="left">height: 100%</div>
<div id="right">Content</div>
<div id="rightFooter">height: 70px</div>
</div>
CSS:
#parent {
position: relative;
height: 200px
}
#left, #right, #rightFooter {
position: absolute
}
#left {
width: 200px;
top: 0;
bottom: 0;
left: 0
}
#right {
top: 0;
right: 0;
bottom: 70px;
left: 200px;
overflow-y: auto
}
#rightFooter {
height: 70px;
right: 0;
bottom: 0;
left: 200px
}
Would something like this work?
Live Demo
Added an animation of the height so you can see the content extending.
Markup
<div id="parent">
<div class="left">
Lefty
</div>
<div class="right">
<div id="rightContent">
right Content
</div>
<div id="rightFooter">
Right Footer
</div>
</div>
<div class="clear"></div>
</div>
CSS
#parent{
height:300px;
}
.left{
float: left;
width: 33%;
background: red;
height:100%;
}
.right{
float : left;
width: 66%;
height:100%;
}
#rightContent{
height: 100%;
background: blue;
}
#rightFooter{
background: yellow;
height: 70px;
float: right;
width: 100%;
margin-top: -70px;
}
.clear{
clear:both;
}
Bah, before the comments come this is a partial solution, the text for the content area will bleed into the footer... looking at a solution for this, or someone else might be able to modify my markup/css to account for that.
Made an example for you here :)
you need to have a left floated div for the left content and a wrapper for the two other right divs, also floated left.
Take a look :)