z-index not active - css

I have a small example, with two adjacent divs with a background image. This divs are tiles in a tile based editor. I want to place an image into the first div and change the position of the image, so that the image overlaps both divs (see http://jsfiddle.net/WRZJe/16/). I've set the z-index of all divs and I've set the z-index of the image. The position attribute for both, divs and image is set to absolute:
<body id="exploration-body">
<div class="dungeon-container" style="left: 1040px; top: 720px;">
<div class="dungeon-canvas-full-screen" id="dungeon_canvas">
<div style="top: -720px; left: -1040px;
background-position: -82px -162px;" class="show-tile">
<img id="token-1" class="token-img"
src="1.png" alt="token1" style="top: 0px; left: 40px">
</div>
<div style="top: -720px; left: -960px; background-position: -82px -162px;" class="show-tile">
</div>
</div>
</div>
</body>
Here is the relevant css-code:
.dungeon-container {
position: absolute;
}
.show-tile {
background-image: url("stone_dungeon.png");
height: 80px;
width: 80px;
position: absolute;
z-index: 5;
}
.token-img {
position: absolute;
z-index: 50;
}
As the z-index of the images is higher then both divs, I expect the image to be drawn in front of both divs. But page behaves like no z-indexes where given. The image hides behind the second div and would be in front of every div before the image containing div.
What might cause the browser (I've tested Safari and FF) to ignore the given z-index?
Update: I've added a screenshot from the actual application (http://robitzki.de/zindex.png) that shows, that the image moves behind those divs, that are placed after the containing div.

Each div.show-tile (which are all siblings) creates an own stacking context! Child elements remain in the stacking context of the parent, thus your img will be hidden if it is adjacent to a div which has a higher z-index as the images parent div. The z-index of the image itself (=child) does not matter in this case.
In order to have the image overlap all the divs, the cleanest solution would be to not put it into one of the divs, but put it separately as a sibling to your .show-tile divs and give it the highest z-index.
Alternatively, you could omit your absolute positioning on the divs - this would make the img having it's position depend on #dungeon_canvas.
If you cannot do so, you have to assure that the div which holds your image always has the highest z-index.

You have to put the second div before the first one so that it comes before the img tag
<body id="exploration-body">
<div class="dungeon-container" style="left: 1040px; top: 720px;">
<div class="dungeon-canvas-full-screen" id="dungeon_canvas">
<div style="top: -720px; left: -960px; background-position: -82px -162px;" class="show-tile">
</div>
<div style="top: -720px; left: -1040px; background-position: -82px -162px;" class="show-tile">
<img id="token-1" class="token-img" src="http://dungeonpilot.com/assets/tokens/1.png" alt="token1" style="top: 0px; left: 40px">
</div>
</div>
</div>
</body>
http://jsfiddle.net/WRZJe/17
If you can move the img tags out of the divs and position them using similar coordinates to the divs then the following will produce the desired display
<body id="exploration-body">
<div class="dungeon-container" style="left: 1040px; top: 720px;">
<div class="dungeon-canvas-full-screen" id="dungeon_canvas">
<img id="token-1" class="token-img" src="http://dungeonpilot.com/assets/tokens/1.png" alt="token1" style="top: -720px; left: -1000px" />
<img id="token-2" class="token-img" src="http://dungeonpilot.com/assets/tokens/1.png" alt="token1" style="top: -720px; left: -930px" />
<div style="top: -720px; left: -960px; background-position: -82px -162px;" class="show-tile">
</div>
<div style="top: -720px; left: -1040px; background-position: -82px -162px;" class="show-tile">
</div>
</div>
</div>
</body>
http://jsfiddle.net/WRZJe/20

Because the second .show-tile has a higher z-index than the image, the image will be cut off
try this:
.dungeon-container {
position: absolute;
}
.show-tile {
background-image: url("stone_dungeon.png");
height: 80px;
width: 80px;
position: absolute;
/*z-index: 5;*/
}
.token-img {
position: absolute;
z-index: 1;
}
Check fiddle to see it work

Now do easily this as like this Live Demo
Html code
<div class="main-container">
<div class="pic-1 pic-5"></div>
<div class="pic-1 pic-3"></div>
<div class="pic-1 pic-2"></div>
<div class="pic-1 pic-4"></div>
<img src="http://dungeonpilot.com/assets/tokens/1.png" alt="" class="pic-change">
</div>
Css
.pic-1 {
background-image: url("http://dungeonpilot.com/assets/stone_dungeon.png");
height: 80px;
width: 80px;
float:left;
position:relative;
background-position: -3px -3px;
}
.pic-2{
clear:left;
}
.pic-change{
position:absolute;
left:40px;
top:40px;
z-index:3;
}
.main-container{
position:relative;
}
.pic-4{
z-index:4;
}
Demo

Related

CSS Particles Js Overlap Issue

Here is the repository for my personal website: https://github.com/flakpanzer40/flakpanzer40.github.io
As you may notice, the particles I've used are simply showing below my name, image, and description. I've tried numerous times to overlap them so that the particles happen in the back, but to no avail. I've tried absolute positions, z-index, re-arranging the DIVs, etc. I'm terrible at CSS.
Can anyone help?
Please add following styles:
.container {
/*other css*/
position: relative;
z-index: 1;
}
.particles-js-canvas-el {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
z-index: 0;
}
Use the following Code:
HTML:
<div id="particles-js" style="">
<canvas class="particles-js-canvas-el" width="1903" height="952" style="width: 100%;height: 100%;position: absolute;top: 0;left: 0;"></canvas>
<div class="container">
<div class="row">
<div class="col-lg-12">
<img class="img-responsive" src="img/EvanRen.jpg" alt="Evan Ren" style="width:500px;height:600px;border:5px solid white;border-radius: 50%;">
<div class="intro-text">
<span class="name">University of Waterloo Computer Science</span>
<hr class="star-light">
<span class="skills">Problem_solving expert - Hardworking - Passionate</span>
</div>
</div>
</div>
</div>
</div>
CSS:
#particles-js {
width: 100%;
height: 100%;
background-color: #13717c;
position: relative;
top: 0;
}
It is important that the parent (#particles-js) has position: relative so that you can use absolute positioning for the children.

Images dont work with css

Sorry if this is a stupid question, but for some reason after 5 hours of work I'm stuck with nothing working...
I have a header which has a logo and the telephone (both of which are images). I want the telephone to be at the middle right of header and then move to the left as the user resizes the window. But when I tried, it moves down. I'd like the logo to be positioned on top of ΛΟΓΙΣΜΙΚΟ and move with the menu (staying on top) as the user resizes.
Here is the site: http://www.altasoft.gr/index_.htm
Can this be done with JavaScript?
<div class="header">
<div style="position:relative; left:17%; top:10px; z-index:99;">
<img src="images/Altasoft_Logo.png" alt="" width="354" height="91" />
</div>
<div style="position:relative; left:80%; top:-30px; z-index:5;">
<img src="images/telephone.gif" alt="" width="142" height="16" />
</div>
</div>
.header {
margin: 0 auto;
padding: 0;
position: relative;
text-align: left;
width: 931px; /* make this same width than 'main content'*/
z-index: 10;
}
in your first divider, style it this way :
element.style {
/* left: 17%; */ /* remove the left:17% */
position: relative;
top: 10px;
width: 50%;
z-index: 99;
}
It will align your header and menu

Center div within div with respect to the page

I know how to center a div layer with respect to it's wrapper or parent container, but what about When I have a div inside of a div inside of the page body. How do I center the smallest div with respect to the body?
<div id="1" style="width: 100%">
<div id="2" style="width: 22.5%; height: 1000000000px; margin-left: 12%;">
<div id="3" style="position: absolute;">
</div>
</div>
</div>
In the above example, How can I center div with id="3" in respect to div with id="1"?
#1 must be position: absolute, #3 set left: 50% and offset half the width to the left
#d1 {
position: absolute;
}
#d3 {
position: absolute;
left: 50%;
width: 100px;
margin-left: -50px;
}
JSFiddle for playing.
I have moved the inline styles to the CSS panel and changed the ids to d1, d2 and d3 respectively.
set marging:0 auto for div id=3
div#2 {
position: relative;
}
div#3{
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
This will center the div#3 not only horisontaly but vertically to div#2, if you are asking that. And keep in mind you can't have digits for id.
If you want to center div#3 to div#1 ... you just have to move the position:relative style from div#2 to div#1 .
If you want it positioned exactly in the screen's center, you need to enclose ID#3 inside 2 divs, one that takes care of vertical alignment, and the other takes care of horizontal alignment:
<div id="vertical">
<div id="horizontal">
<div id="3" style="position: absolute;">
</div>
</div>
More details are here: dead-center

Corner images using CSS

I have a relatively simple design that is puzzling me. It has 4 large images that need to be stuck to the top left, right and bottom left, right corners. The images are quite large and the content container overlaps them. A little something like this:
Structure http://www.kalleload.net/uploads/nizyjc/zxyagpfrmjqe.png
My problem is that my implementation works fine in all major browsers except IE8 (which I was just starting to respect). Is there a better way I can do this?
I'm using the following markup at the moment:
<div class="corner-top">
<div><img src="./images/top-left-corner.png" /></div>
</div>
<div class="corner-bottom">
<img src="./images/bottom-left-corner.png" />
</div>
<div id="container">
....
</div>
#container {
margin: 60px auto;
width: 488px;
}
.corner-top {
background: url('./images/top-right-corner.png') top right no-repeat;
height: 356px;
min-width: 868px;
overflow: hidden;
position: absolute;
top: 0;
width: 100%;
z-index: -20;
}
.corner-top div {
min-width: 868px;
}
.corner-bottom {
background: url('./images/bottom-right-corner.png') bottom right no-repeat;
bottom: 0;
height: 325px;
min-width: 868px;
overflow: hidden;
position: absolute;
width: 100%;
z-index: -20;
}
.corner-bottom div {
min-width: 868px;
}
There are many approaches to rounded corners (basically the same). I think the most comfortable one to have four divs in each other:
<div id="container" class="topleft">
<div class="topright">
<div class="bottomleft">
<div class="bottomright">
<!-- content -->
</div>
</div>
</div>
</div>
Another advantage is that you don't need the <img> tags.
you could try forcing IE8 into IE7 compatablity mode.
stick
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
in your <head>

Vertical aligning an absolute positioned div inside a containing div

I'm using the jQuery Cycle plugin to rotate images in a slideshow type fashion. That works fine. The problem I'm having is getting these images (of different sizes) to center in the containing div. The images are inside a slidshow div that has it's position set to absolute by the Cycle plugin.
I've tried setting line-height/vertical-align and whatnot but no dice. Here is the relevant HTML and CSS
HTML:
<div id="projects">
<div class="gallery">
<span class="span1">◄</span><span class="span2">►</span>
<div class="slideshow">
<img src="images/img1.png" />
<img src="images/img1.png" />
<img src="images/img1.png" />
</div>
</div>
</div>
CSS:
#main #home-column-2 #projects
{
width: 330px;
background: #fefff5;
height: 405px;
padding: 12px;
}
#main #home-column-2 #projects .gallery
{
width: 328px;
height: 363px;
position: relative;
background: url('images/bg-home-gallery.jpg');
}
#main #home-column-2 #projects .gallery img
{
position: relative;
z-index: 10;
}
And in case you want to see it, the jQuery:
$('#home-column-2 #projects .gallery .slideshow').cycle(
{
fx: 'scrollHorz',
timeout: 0,
next: "#home-column-2 #projects .gallery span.span2",
prev: "#home-column-2 #projects .gallery span.span1"
});
Any ideas on getting these images to center?
Try this:
http://www.brunildo.org/test/img_center.html
Vertical centering is a pain! Here's what the W3C page says about the vertical center:
CSS level 2 doesn't have a property
for centering things vertically. There
will probably be one in CSS level 3.
But even in CSS2 you can center blocks
vertically, by combining a few
properties. The trick is to specify
that the outer block is to be
formatted as a table cell, because the
contents of a table cell can be
centered vertically.
This method involves a little jquery, but works fantastic in most situations...
let me explain:
if all the images of the slideshow are contained within their own element div pos:absolute and those images are pos:relative, then on a $(window).load() you can run a .each() and find each img in the slideshow and adjust it's top positioning to be offset a certain number of pixels from the top..
jcycle automatically sets each parent div containing the image to pos:absolute on every onafter() so it's useless to apply this pos adjustment to them... instead target each img you have set to pos:relative...
Here is the example:
$(window).load(function() {
// move all slides to the middle of the slideshow stage
var slideshowHeight = 600; //this can dynamic or hard-coded
$('.slideImg').each(function(index) {
var thisHeight = $(this).innerHeight();
var vertAdj = ((slideshowHeight - thisHeight) / 2);
$(this).css('top', vertAdj);
});
});
and this is the html it's working on...
<div class="slideshow" style="position: relative; ">
<div style="position: absolute; top: 0px; left: 0px; display: none; width: 1000px; height: 600px; " id="img0">
<img class="slideImg" src="/images/picture-1.jpg" style="top: 0px; "><!-- the style=top:0 is a result of the jquery -->
</div>
<div style="position: absolute; top: 0px; left: 0px; display: none; width: 1000px; height: 600px; " id="img1">
<img class="slideImg" src="/images/picture-1.jpg" style="top: 89.5px; "><!-- the style=top:89.5px is a result of the jquery -->
</div>
<div style="position: absolute; top: 0px; left: 0px; display: none; width: 1000px; height: 600px; " id="img2">
<img class="slideImg" src="/images/picture-1.jpg" style="top: 13px; "><!-- the style=top:13px is a result of the jquery -->
</div>
</div>
just make sure
.slideImg {
position:relative;
}
I think that's everything... I have an example, but it's on a dev site.. so this link might not last.. but you can take a look at it here:
http://beta.gluemgmt.com/portfolio/rae-scarton-editorial.html
The positions are relative according to the style sheet, so did you try setting them to display: block and margin-top: auto; margin-bottom: auto; ?
Another option is to align them manually in javascript based on the containing div's height.
You need to nest two divs inside each cycle item. The first must have the display: inline-table; and the second must have display: table-cell; both these divs have vertical-align: middle.
So the structure would look something like this:
<div class="slide-container">
<div class="slide">
<div class="outer-container">
<div class="inner-container">
Centered content
</div>
</div>
</div>
<div class="slide">
<div class="outer-container">
<div class="inner-container">
Centered content
</div>
</div>
</div>
</div>
With the following css:
.slide-container {
height: 300px;
}
.outer-container {
height: 300px;
display: inline-table;
vertical-align: middle;
}
.inner-container{
vertical-align: middle;
display: table-cell;
}
You can see it working here http://jsfiddle.net/alsweeet/H9ZSf/6/

Resources