Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a div with z-index to overlay another div on hover

I have a container div with several smaller div:s inside, all of them with float:left;. If I hover one of the smaller div:s the height and width should be increased and it should overlay the other div:s without moving them.

HTML:

 <div id="boxcontainer">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
 </div>

CSS:

.box {
  float:left;
  position: relative;
  width:150px;
  height:150px;
  margin:5px;
  background-color: yellow;
}

#boxcontainer {
  position: relative;
  width:500px;
  height:auto;
}

.box:hover {
  z-index:100;
  width:300px;
  height:auto;
}

How can I achieve this?

like image 875
caniball Avatar asked Apr 01 '14 18:04

caniball


People also ask

How do I overlay one div over another div?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

Does Z index affect hover?

the order of the elements will dictate which hover effect will occur. If you have a z-index on 1 element of 1000 and 999 on the other. does not allow the hover transitions of the element @ 999 to occur.

How do you make a div appear on top of everything else on the screen?

Set the DIV's z-index to one larger than the other DIVs. You'll also need to make sure the DIV has a position other than static set on it, too. position relative and z index set to 999999999999999999999999999999999999999999999999999.

How do you make a div appear in front of another?

Use the CSS z-index property. Elements with a greater z-index value are positioned in front of elements with smaller z-index values. Note that for this to work, you also need to set a position style ( position:absolute , position:relative , or position:fixed ) on both/all of the elements you want to order.


2 Answers

z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).

Instead of float left try position absolute.

I have added a container around each box and positioned each element absolutely within it. This way you can add as many boxes as you wish and keep the same class.

EXAMPLE

HTML

<div id="boxcontainer">
    <div class="box">
        <div class="Inside"></div> 
    </div>         
    <div class="box">
        <div class="Inside"></div> 
    </div>         
    <div class="box">
        <div class="Inside"></div> 
    </div>     
</div>

CSS

#boxcontainer{
    position: relative;
    width:500px;
    height:500px;
}
.box{
    position:relative;
    float:left;
    width:150px;
    height:150px;
    margin:5px;
}
.Inside{    
    background:green;
    position:absolute;
    top:0;
    left:0;
    width:150px;
    height:150px;
    transition:all 0.5s ease-in-out;
    -webkit-transition:all 0.2s ease-in-out;
}
.Inside:hover{
    z-index:100;
    width:250px;
    height:250px;
    background:#666666;
}

http://jsfiddle.net/JJ3v4/3/

like image 63
DreamTeK Avatar answered Oct 22 '22 11:10

DreamTeK


Live Demo: http://jsfiddle.net/hKL4f/1/

I think the easiest way to do what you want would be to use a CSS transform.

.box:hover {
    transform: scale(2, 2);
}

That way you alter the dimensions of the element on hover without affecting any of the other elements in the document flow around it.

If you want the boxes to expand in a different way (ie, to the right and bottom rather than in all directions) you can set a transform-origin property (default is 50% 50%).

like image 33
Nate Avatar answered Oct 22 '22 11:10

Nate