<head>
<style>
.parent
{
width:1100px;
border:1px solid red;
height:1500px;
position:relative;
}
.childfix
{
width:300px;
float:left;
border:1px solid green;
height:200px;
position:fixed;/*when using this childfix remains fix for whole web page*/
top:5px;
}
.childscroll
{
width:680px;
float:left;
border:1px solid yellow;
height:1400px;
}
.anotherclass
{
width:1100px;
height:600px;
border:1px solid orange;
}
.clearfix
{
clear:both;
}
</style>
</head>
<body>
<div class="parent"><!--parent class-->
<div class="childfix"><!-- class has to be fixed with only parent class -->
</div>
<div class="childscroll"><!-- This class has to be scrolled with parent -->
</div>
<div class="clearfix"></div>
</div>
<div class="anotherclass"><!-- for thhis there is no fixed class -->
</div>
</body>
neha_k, can you please add more information about this issue to help explain the problem. I see the word “scrolling” within your comments but I also see all fixed heights. I do not understand what you expect to achieve. Perhaps you can expand your “working page” by adding content that shows what does and does not work.
You can probably reduce the dimensions of the boxes considerably for the demo so it fits on an average sized desktop monitor.
The only way you can make it APPEAR to be in relation to parent is to NOT set an offset coordinate. That means do not set a top: bottom: left: or right: value. But then you have no control of it with the offset properties, however it can be moved with negative margins
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Fixed Test</title>
<style>
.wrap {
width:50%;
margin:50px auto;
background:red;
}
.item {
position:fixed;
/*top:5px; now it APPEARS to be in relation to parent*/
margin-top:-50px;
background: lightblue;
}
p {margin:150px 0;}
</style>
</head>
<body>
<div class="wrap">
<div class="item">position fixed child</div>
<p>Scrolling text</p>
<p>Scrolling text</p>
<p>Scrolling text</p>
<p>Scrolling text</p>
<p>Scrolling text</p>
</div>
</body>
</html>
I am saying the fixed has to be fixed only with scrolling text.When I add another div after wrap.
fixed also has to be scrolled out.
Sorry for poor english
Then you can’t use fixed positioning.
Your option then would be to use position:absolute; on the child element.
That changes the rules of the game to your advantage though, now you CAN position in relation to the parent by setting position:relative on the parent.
If you are wanting it fixed to it’s parent and then to scroll out of view when the parent scrolls out of view then you would need a script to do that. Not possible with CSS.
Here is the absolute position example, but I don’t think it’s what you want.
I’ve been out of the house, not following Ray’s code, but since you are not yet satisfied with his efforts, may I suggest that, instead of code, you post an annotated, simulated screen shot, a photoshop drawing, that shows what you want to achieve. We might have better luck that way.