How to Overlap Parent Div From Child Div - html

I have some divs at my HTML and one of them is loading image div so I want it overlap its parent div. Here is my code:
<div id="something1">
<div id="something2"></div>
<div id="parent" style="border: 15px solid #c1c1c1;width:200px; height:250px;">
<div id="child" style="position: absolute; border: 15px solid #a2f2e2"></div>
</div>
</div>
When I use different positions (i.e. absolute, relative etc.) child div couldn't overlap its parent. Here is my fiddle link: http://jsfiddle.net/yNFxj/4/ I don't want to see anything from parent div but I want to see that child div increased as parent's size and overlapped it.
Any ideas about how can I dot it just with pure html and css and with a generic way to implement it my any other pages?
PS: Border, width, height etc. are just for example, it can be removed.

Sajjan's the closest from what I can tell, but his has a few flaws:
Position: absolute requires its parent to have a non-static position (this is often done with position: relative, which effectively works the same way as static).
You don't need to set the height and width of the child, just the parent.
Here's my Fiddle for it to demonstrate.
#parent {
border: 5px solid gray;
position: relative;
height: 100px;
width: 100px;
margin-left: 50px;
}
#child {
position: absolute;
top:0;
left: 0;
right: 0;
bottom: 0;
background: red;
}
The key here is the position: relative on the parent.
I am curious, though, what exactly you're trying to achieve with this. I have a feeling that whatever it is, there's a better way.

Why doesnt this work for you? If i understand you right, you just want to mask the parent DIV with the child DIV?
<div id="something1">
<div id="something2"></div>
<div id="parent" style="border: 15px solid #c1c1c1;width:200px; height:250px;">
<div id="child" style="position: absolute; border: 15px solid #a2f2e2;top:0;left:0;width:200px; height:250px;"></div>
</div>
</div>
Output on chrome:
To make it generic, get the parents position/dimension using offsetTop/Left/Height/Width methods and set the child's dimensions/position using these, im sure there are plenty of posts on SO that do this..

First problem is that absolute positioned elements refers to the first parent element with a position different from static.
This means that if no parents have fixed, relative, or absolute position, it will refer to the body, that is not what you want in this case.
Then put position: relative; to your parent div.
Second problem: with absolute position, you can stop using width and height and start using top, left, bottom and right properties;
Setting all them to 0 means extends the object to the parent boundaries.
But here you want to overlap them, then... simply, use a negative value equals to the size of the parent borders: 15px;
top: -15px;
left: -15px;
bottom: -15px;
right: -15px;
Demo: http://jsfiddle.net/yNFxj/9/
I've used dashed borders so you can see the underneath parent's borders ;)

Try this:
<div id="something1">
<div id="something2"></div>
<div id="parent" style="border: 15px solid #c1c1c1;position:relative; width:200px; height:250px;">
<div id="child" style="position: absolute; border: 15px solid #a2f2e2; width:200px; height:250px; left:-15px; top:-15px"></div>
</div>
</div>
update a fiddle for you http://jsfiddle.net/yNFxj/16/

HTML
<div id="something1">
<div id="something2"></div>
<div id="parent">
<div id="child"></div>
</div>
</div>
CSS
#parent {
width: 100px;
height: 300px;
background-color: #a0a0a0;
width:200px;
height:250px;
position: relative;
}
#child {
width: inherit;
height: inherit;
position: absolute;
background-color: #a2f2e2;
top: 0px;
left: 0px;
}
Works fine
EDIT: added with and height inherit for you & positioned properly

Related

How to put a div over another div or element, but have it be 50% of that div (trying to do click event)

Basically, I've been trying all sorts of positioning and what not, but here's the issue:
given one div that is shaped like a rectangle, I am trying to overlay another div on it that will be 50% of the width. However, if I do this then the div below also changes its shape/positioning.
How can I prevent htis from happening?
I know how to make containers, but the inside content is bound by the outer content.
In this case, I want to put a smaller div on top of a larger dive without messing up the div below.
<div class="overlay">
<div class="image">
</div>
</div>
What if you contain them in another div?
Something like this:
<!DOCTYPE html>
<html>
<head>
<style>
div.container {
height: 300px;
width: 250px;
position: relative;
}
div.overlay {
position: absolute;
border: 3px solid #73AD21;
left: 0px;
top: 0px;
width: 50%;
bottom: 0px;
display:block;
}
div.image {
position: absolute;
top: 0px;
width: 200%;
height: 100%;
border: 3px solid #73AD21;
}
</style>
</head>
<body>
<h2>position: absolute;</h2>
<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed):</p>
<div class="container">
<div class="overlay">This div element has position: relative;
<div class="image">This div element has position: absolute;</div>
</div>
</div>
</body>
</html>
https://www.w3schools.com/code/tryit.asp?filename=FE87FF3WRXHF
Please checkout this fiddle, if I understand you correctly
you can either use
float:left
https://jsfiddle.net/wuvxuddk/
to take the div out of the normal flow or use
position: absolute
https://jsfiddle.net/tygpoxuk/

absolute and relative div positioning

I have created a few divs inside divs:). The problem is that the FOOTER div is printed above the div which contains absolute divs and is not dipalyed as last (at the bottom).
I tried a few combinations for footer div like: "position: absolute; top: 0px; left:0px", but it still displyed at the top of page.
How to move it to the bottom with current divs?
http://jsfiddle.net/hsbgpmus/2/
Where is the problem?
ADDED:
I want to have footer div not at the bottom of web browser window, but as last div right after div containing BBBB string.
You have used "position:static" in your example fiddle. Static means the element will flow into the page as it normally would.
For more info. about positioning element using css refer this link.
Positioning element absolutely and setting top and left to 0px means you want to align it at the top. To align element at the bottom of the parent element, you have to set bottom property (not top property)
CSS code :
<footer_element_class_or_id> {
position: absolute;
bottom: 0px;
left: 0px;
}
Hope it helps.
try this
<div style="position:relative; height:600px">
<div style="width: 100px">
<h1>HEaDER</h1>
<div>
<div style="position: static">
<div style="padding: 10px; position: absolute; top: 100px; left: 100px; background-color: yellow;">aaaaa</div>
<div style="padding: 10px; position: absolute; top: 200px; background-color: yellow;">cccc</div>
<div style="padding: 10px; position: absolute; top: 300px; background-color: yellow;">bbbb</div>
</div>
<div style="position: absolute; bottom:0">
<h1>FOOTER</h1>
</DIV>
</div>
http://jsfiddle.net/anjum121/xmox7pjq/
you need to wrapp your parent div to relative position
try this fiddle
http://jsfiddle.net/hsbgpmus/3/
.footer{
position:absolute;
bottom:0;
}
html
<div class ="footer"><h1>FOOTER</h1></DIV>
use below code for footer,
position: absolute; bottom: 0px; left:0px

Relative parent ignored with absolutely positioned children

I know that there are a lot of questions about positioning out there, including about absolute positioning inside a relative parent.
I've read many of these questions and found a informative link on css-tricks (Absolute positioning inside relative parent). After all troubleshooting fails, I turn to you ;)
This JSFiddle contains I think is right, but clearly isn't.
Why are the child elements of the parent div positioned relative to the body and not the div?
code:
<div id="editorWrapper" style="posotion: relative; width:751px; height:250px; margin-left: 20px; margin-top: 20px; border: 1px solid blue;">
<a class="lnk" href="http://www.google.be" style="display: inline-block; position:absolute; left: 1%; top: 1%; padding: 5% 5%;"></a>
<a class="lnk" href="http://www.google.be" style="display: inline-block; position:absolute; left: 80%; top: 80%; padding: 10% 10%;"></a>
<div style="position: absolute; padding: 10px; left: 60%; top: 60%; background-color: red;" />
</div>
EDIT
The answer is just a typo. posotion should be position in the parent div.
For future references: this positioning methods does in fact work :)
Thanks Gaurang!
Take note: You have written posotion: relative in the style attribute of div#editorWrapper. It should be position: relative instead.
Fixed Fiddle

IE6 and IE7 absolute positioned div on top of multiple relative divs

Is it possible to make multiple absolute-positioned divs overlap multiple relative-positioned divs in IE6 & IE7?
See this jsFiddle for more information: http://jsfiddle.net/btker/1/
HTML
<div class="wrapper">
<div class="relative_div">Relative div.
<div class="absolute_div">This div have absolute position and is placed in a relative positioned div. This div should always be on top of all relative divs.</div>
</div>
</div>
<div class="wrapper">
<div class="relative_div">Relative div.
<div class="absolute_div">This div have absolute position and is placed in a relative positioned div. This div should always be on top of all relative divs.</div>
</div>
</div>
CSS
.wrapper{
height: 100%;
width: 100%;
}
.relative_div {
height: 75px;
width: 200px;
border: 1px solid #000;
padding: 10px;
background: #e6e6e6;
margin: 0 0 35px 0;
position: relative;
}
.absolute_div {
height: 100px;
width: 250px;
border: 1px solid #000;
background: #c6c6c6;
padding: 10px;
position: absolute;
top: 40px;
left: 100px;
z-index: 100;
}
There are two relative <div>s (placed in identical wrappers) containing each one a absolute <div> that overlap all the relative <div>s. This works great without any problems in updated versions of Chrome, Firefox etc, the absolute <div> with z-index is always placed on top.
In IE6 and IE7 this is not working. The different between this problem and the standard "dropdown in header display its menus behind the page content" is that in those situations its often fixed by give the parent element of that specific menu other z-index etc. In this case the both absolute <div>s are put in identical <div>s.
Can this be solved so the absolute <div>s are always on top of all relative <div>s in IE6 & IE7? Conditional comments for IE can be used to make the solution cross-browser.
It is possible but only by decreasing the z-index of the second .wrapper or increasing the z-index of the first .wrapper.
On a simple level, each positioned element with a non-auto z-index creates a new stacking context, although there are other circumstances in which a new stacking context is created - see The stacking context.
The problem is one that affects IE <= 7, from quirksmode.org
In Internet Explorer positioned elements generate a new stacking context, starting with a z-index value of 0. Therefore z-index doesn't work correctly.
CSS
.wrapper{
height: 100%;
width: 100%;
}
.lower {
position: relative;
z-index: -1;
}
.higher {
position: relative;
z-index: 2;
}
.relative_div {
height: 75px;
width: 200px;
border: 1px solid #000;
padding: 10px;
background: #e6e6e6;
margin: 0 0 35px 0;
position: relative;
}
.absolute_div {
height: 100px;
width: 250px;
border: 1px solid #000;
background: #c6c6c6;
padding: 10px;
position: absolute;
top: 40px;
left: 100px;
z-index: 1;
}
HTML
<div class="wrapper"> <!-- add higher class here -->
<div class="relative_div">Relative div.
<div class="absolute_div">This div have absolute position and is placed in a relative positioned div. This div should always be on top of all relative divs.</div>
</div>
</div>
<div class="wrapper"> <!-- or add lower class here -->
<div class="relative_div">Relative div.
<div class="absolute_div">This div have absolute position and is placed in a relative positioned div. This div should always be on top of all relative divs.</div>
</div>
</div>

CSS position relative without relative width?

I'd like an element to have relative positioning. But I don't want its child element (with position:absolute) to have relative width to the parent.
For example: http://jsfiddle.net/t2yJP/. I'd like the second body>div to have position:relative, but have its child's width maintain the same behavior.
Try adding width:inherit to the .abs class.
How about this jsFiddle.
But you should really rethink your strategy. In your fiddle, your second example only works because the parent div is not positioned and therefore, the .abs div is technically not in the parent.
Normally, child elements are inside their parents. That's what containers are for! So if you don't want the .abs div to be constrained by the red rectangle, don't put it inside the red rectangle.
I was able to achieve a similar-looking effect as follows:
<div class='abs pad'>
Content content content
</div>
<div class='rel pad red'>
</div>
.rel {
position: relative;
}
.abs {
position: absolute;
z-index: 1;
}
.pad {
padding: 2px;
margin: 2px;
}
.red {
border: 1px solid black;
width: 100px;
height: 100px;
background-color: red;
}