I'm aware of the position:relative
and position:absolute
trick to position a div relative to its parent. But what if the div is not its parent and I want to position it relative to that? I'm trying to implement something along those lines.
I'm also aware of position:fixed
to fix a div but I'm building a responsive website and I'd like to avoid that. I found a question here which mentions using jQuery to do it: How to position one element relative to another with jQuery? Is jQuery the only way to do it? Can it be done using CSS as well?
I've put up a fiddle here: http://jsfiddle.net/19bdpgsf/. In the fiddle, I'm trying to position the child2
element relative to the notparentdiv
just like it has been positioned relative to the parent
div. Is it possible using only css?
Thanks.
First set position of the parent DIV to relative (specifying the offset, i.e. left , top etc. is not necessary) and then apply position: absolute to the child DIV with the offset you want. It's simple and should do the trick well.
By using a div with style z-index:1; and position: absolute; you can overlay your div on any other div . z-index determines the order in which divs 'stack'. A div with a higher z-index will appear in front of a div with a lower z-index . Note that this property only works with positioned elements.
Relative Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.
Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.
var nonparent = $('.notParent');
var position = nonparent.offset();
$('.child1').offset({
top: position.top,
left: position.left
});
.notParent {
background-color: red;
padding: 20px;
}
.child2 {
background-color: yellow;
}
.child1 {
background-color: green;
}
.parent {
background-color: purple;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notParent">
not parent div
</div>
<br/>
<br/>
<br/>
<br/>
<div class="parent">
parent
<div class="child1">child1</div>
<div class="child2">
child2
</div>
</div>
Another way if you dont want to use css positions use offset jquery as below to position you div
var nonparent = $('.notParent');
var position = nonparent.offset();
$('.child1').offset({
top: position.top,
left: position.left
});
.notParent {
background-color: red;
padding: 20px;
}
.child2 {
background-color: yellow;
}
.child1 {
background-color: green;
}
.parent {
background-color: purple;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="notParent">
not parent div
</div>
<br/>
<br/>
<br/>
<br/>
<div class="parent">
parent
<div class="child1">child1</div>
<div class="child2">
child2
</div>
</div>
You can position a div inside another div which is not parent with absolute relative positions as below
.notParent {
background-color: red;
position: relative;
}
.child2 {
background-color: yellow;
position: absolute;
top: 10px;
}
.child1 {
background-color: green;
top: 30px;
}
.parent {
background-color: purple;
}
<div class="notParent">
not parent div
</div>
<br/>
<br/>
<br/>
<br/>
<div class="parent">
parent
<div class="child1">child1</div>
<div class="child2">
child2
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With