How can I prevent relatively positioned div from pushing down content that follows? In following example I am trying to display a small green div on top of a bigger red one but the next red div gets pushed down when the green div appears.
If there is a better way of doing this, I’d appreciate tips.
Here is a running example: http://jsfiddle.net/38Rqh/
<html>
<head>
<style type="text/css" media="screen">
.top {
display: none;
background-color: green;
width: 100px;
position: relative;
top: -50px;
}
.bottom {
width: 200px;
height: 200px;
background-color: red;
border: 1px solid black;
}
.bottom:hover + div {
display: block;
}
</style>
</head>
<body>
<div class="bottom">
</div>
<div class="top">Displaying data</div>
<div class="bottom">
</div>
<div class="top">Displaying data</div>
<div class="bottom">
</div>
<div class="top">Displaying data</div>
</body>
</html>
relativestill takes up space. What you need isabsolute. One possibility is to set your.bottomelements toposition: relativeand then place the.topelements within them and position them absolutely.http://jsfiddle.net/38Rqh/1/
In this way, the
.topelements will be positioned in relation to their containing.bottom.This has the added benefit of not hiding the
.topwhen hovering on the.topitself, causing the flicker you see in your example.