Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "z-index" to make a menu always on top of the content

Tags:

css

z-index

I have a problem and I can't figure how to correct this. What I want is that the "Red box" stay on top of the page in a z-index 2, while the all the content on the background stay on index 1 but somehow this code is "collapsing" the layers. If someone can help me I really appreciate it. Sorry for my English. Thanks in advance.

<html>
<head>
<title></title>
<style type="text/css">

body { margin: 0; }

#container {
    position: absolute;
    float: right;
    z-index: 1;

}

.left1 { 
    background-color: blue;
    height: 50px;
    width: 100%;

}
.left2 { 
    background-color: green;
    height: 50px;
    width: 100%;

}


#right { 
    background-color: red;
    height: 300px;
    width: 300px;
    float:right;
    z-index: 999999;
    margin-top: 0px;
    position: relative;
}




</style>
</head>

<body>

<div id="container"></div>
<div class="left1">LEFT BLUE</div>
<div class="left2">LEFT GREEN</div>
</div>
<div id="right">RIGHT RED</div>

</body>
</html>
like image 547
rmz Avatar asked May 08 '12 21:05

rmz


People also ask

Which Z index is on top?

The maximum range is ±2147483647. In CSS code bases, you'll often see z-index values of 999, 9999 or 99999.

How do I make an element on top in CSS?

If position: absolute; or position: fixed; - the top property sets the top edge of an element to a unit above/below the top edge of its nearest positioned ancestor. If position: relative; - the top property makes the element's top edge to move above/below its normal position.

How to use z index with image in CSS?

Z index with an Image. As you can see in the above which ever z index value is greater that is always overlapping lower index value box. Z index in CSS property is used to put the any element on top of the other element. But Z index can be applied with only positioned element like absolute, relative and sticky etc.

What is the difference between stack order and z index?

An element with greater stack order is always in front of an element with a lower stack order. Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky).

How to use z-index property in HTML?

An element with highest or greater stack order value will be always in front of the element with lower stack order value. Keep in mind that z-index only worked with position elements like position: absolute, position: relative, position: sticky. This clears a point that z- index property can be applied with positioned elements in z order.

How do I make content on top of an image?

So by using z-index property we can make content on top of the image. Why we use CSS in HTML? Provide common Logic between all the pages instead of writing same style logic in each HTML page we use CSS file for writing common logic. And it includes CSS page in each HTML page with <link> tag. How does Z-index property work in CSS?


3 Answers

You most probably don't need z-index to do that. You can use relative and absolute positioning.

I advise you to take a better look at css positioning and the difference between relative and absolute positioning... I saw you're setting position: absolute; to an element and trying to float that element. It won't work friend! When you understand positioning in CSS it will make your work a lot easier! ;)

Edit: Just to be clear, positioning is not a replacement for them and I do use z-index. I just try to avoid using them. Using z-indexes everywhere seems easy and fun at first... until you have bugs related to them and find yourself having to revisit and manage z-indexes.

like image 120
rafaelbiten Avatar answered Oct 06 '22 18:10

rafaelbiten


you could put the style in container div menu with:

<div style="position:relative; z-index:10">
   ...
   <!--html menu-->
   ...
</div>

before enter image description here

after

enter image description here

like image 41
Tarek Kalaji Avatar answered Oct 06 '22 18:10

Tarek Kalaji


Ok, Im assuming you want to put the .left inside the container so I suggest you edit your html. The key is the position:absolute and right:0

#right { 
    background-color: red;
    height: 300px;
    width: 300px;
    z-index: 999999;
    margin-top: 0px;
    position: absolute;
    right:0;
}

here is the full code: http://jsfiddle.net/T9FJL/

like image 3
adedoy Avatar answered Oct 06 '22 18:10

adedoy