Position absolute and overflow hidden - html

We have two DIVs, one embedded in the other. If the outer DIV is not positioned absolute then the inner DIV, which is positioned absolute, does not obey the overflow hidden of the outer DIV.
#first {
width: 200px;
height: 200px;
background-color: green;
overflow: hidden;
}
#second {
width: 50px;
height: 50px;
background-color: red;
position: absolute;
left: 250px;
top: 250px;
}
<div id="first">
<div id="second"></div>
<div id="third"></div>
</div>
Is there any chance to make the inner DIV obey the overflow hidden of the outer DIV without setting the outer DIV to position absolute (cause that will muck up our complete layout)?
Also position relative for our inner DIV isn't an option as we need to "grow out" of a table TD.
#first {
width: 200px;
height: 200px;
background-color: green;
}
#second {
width: 50px;
height: 400px;
background-color: red;
position: relative;
left: 0px;
top: 0px;
}
<table id="first">
<tr>
<td>
<div id="second"></div>
</td>
</tr>
</table>
Are there any other options?

Make outer <div> to position: relative and inner <div> to position: absolute. It should work for you.

What about position: relative for the outer div? In the example that hides the inner one. It also won't move it in its layout since you don't specify a top or left.

An absolutely positioned element is actually positioned regarding a relative parent, or the nearest found relative parent. So the element with overflow: hidden should be between relative and absolute positioned elements:
<div class="relative-parent">
<div class="hiding-parent">
<div class="child"></div>
</div>
</div>
.relative-parent {
position:relative;
}
.hiding-parent {
overflow:hidden;
}
.child {
position:absolute;
}

Make sure.
parent position relative.
parent have manually assigned width and height(important as child element having absolute position).
child position absolute;
.outer{
position:relative;
width:200px;
height:100px;
overflow:hidden;
}
.inner{
position:absolute;
width:100px;
height:100px;
font-size:3rem;
}
<div class="outer">
<div class=inner>
Inner DIV to apply overflw hidden
</div>
</div>
}

You just make divs like this:
<div style="width:100px; height: 100px; border:1px solid; overflow:hidden; ">
<br/>
<div style="position:inherit; width: 200px; height:200px; background:yellow;">
<br/>
<div style="position:absolute; width: 500px; height:50px; background:Pink; z-index: 99;">
<br/>
</div>
</div>
</div>
I hope this code will help you :)

Related

CSS vertical and horizontal align center [duplicate]

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 4 years ago.
I use the following style to vertically and horizontally align content.
.middle_center{
top: 50%;
left: 50%;
transform:translate(-50%,-50%);
}
However, if the content of .middle_center is larger than 50%, the left:50% applied means the width of .middle_center can only stretch to 50% of it's parent.
Here is a full code:
.parent{
position:relative;
background:#ff00ff;
width:800px;
height:300px;
}
.middle_center{
position:absolute;
background:#0000ff;
color:#fff;
padding:20px;
top:50%;
left:50%;
transform:translate(-50%,-50%);
}
<div class="parent">
<div class="middle_center">This is some content. The left:50%; causes its width to be reduced to 50% of its parent's width.</div>
</div>
If I apply width: fit-content; then it works as expected. However this isn't supported by all browsers.
Does anyone know of a way to prevent the width from shrinking? It would like to add CSS only to the child element without adding styles to the parent if possible.
Here is a codepen link:
https://codepen.io/jonniejoejonson/pen/jvddPB
Make the element inline-block then center it horizontally using text-align:center and vertically using position:absolute considering an extra wrapper:
.parent {
position: relative;
background: #ff00ff;
width: 600px;
height: 200px;
border: 1px solid;
}
.middle_center {
position: absolute;
width: 100%;
top: 50%;
transform: translateY(-50%);
text-align: center;
}
.middle_center>div {
display: inline-block;
color: #fff;
padding: 20px;
background: #0000ff;
}
<div class="parent">
<div class="middle_center">
<div>This is some content.</div>
</div>
</div>
<div class="parent">
<div class="middle_center">
<div>This is some content. This is some content This is some content</div>
</div>
</div>
<div class="parent">
<div class="middle_center">
<div>This is some content. This is some content This is some content This is some content</div>
</div>
</div>
If you're trying to center the child horizontally and vertically, why not try using flexbox? An example of that with a blue box would look something like this:
.middle_center {
display: flex;
align-items: center;
justify-content: center;
height: 400px;
}
.blue_box {
width: 300px;
height: 300px;
background-color: #05c;
}
With the HTML like this:
<div class="middle_center">
<div class="blue_box"></div>
</div>
Here's a working example on JSFiddle.
if you try to center this using this technique then use the position:absolute; on that class (.middle_center) and give position:relative; to its parent.

div does not resize to height if child is positioned absolutly

I have an image inside a DIV.
I want to "overhang" the image outside the DIV a little, so I've positioned it absolute and the parent container as relative. When I do that, the parent DIV no longer resizes its height to contain the image.
How can I do this?
the HTML
<div class=".twelve.columns" id="header">
<div id="logoWrapper">
<img src="https://nbson.com/sni/images/logo.png" class="ssImg">
<div class="clr"></div>
</div>
</div>
the CSS
.ssImg{
width:100%;
}
.clr{
clear:both;
}
#header{
margin-top:0;
background:#000;
position:relative;
width:100%;
border:1pt solid pink;
}
JSFiddle
Absolutely positioned elements are completely removed from the document flow, and thus their dimensions cannot alter the dimensions of their parents.
If you really had to achieve this affect while keeping the children as position: absolute, you could do so with JavaScript [...]
To get the effect described without javascript, you could use negative values for bottom or top. I also updated your JSFiddle for your concrete example.
.ssImg{
width:100%;
}
.clr{
clear:both;
}
#header{
margin-top:0;
background:#000;
position:relative;a
width:100%;
border:1pt solid pink;
}
#logoWrapper{
width:15%;
min-width:120px;
margin-left:10px;
position:relative; /* this is new */
bottom: -40px; /* this is new */
}
<div class="twelve columns" id="header">
<div id="logoWrapper">
<img src="https://nbson.com/sni/images/logo.png" class="ssImg">
<div class="clr"></div>
</div>
</div>
How about this?
html, body {
height: 100%;
padding: 20px;
}
.ssImg{
width: 100%;
}
#header{
background-color: #000;
position: relative;
width: 100%;
height: 100%; /* Set the height what you want */
border: 1pt solid pink;
}
#logoWrapper{
width: 15%;
min-width: 120px;
position: absolute;
top: -15px;
left: -25px;
}
<div id="header">
<div id="logoWrapper">
<img src="https://nbson.com/sni/images/logo.png" class="ssImg">
</div>
</div>
First of all:
If you want to put two classes on an element use like <div class="twelve columns">, not like <div class=".twelve.columns">
Secondly, regarding your question:
Absolutely positioned elements are removed from the flow and thus, no longer taken into consideration when it comes to calculating dimensions for the parent element.
You can solve it by explicitly setting the height and width you need on the element.

CSS make absolute child div override absolute parent max-width

Is there any way to make the child div below override its parent max-width to stretch to 100% page width? both parent and child are absolute position
<div class="container" style="max-width:500px;position:absolute;">
<div class="content"></div>
<div class="special-content" style="position:absolute;width:100%"></div> <!--override to 100% page width? -->
<div class="content"></div>
<div class="content"></div>
<div>
also the above container is currently positioned inside another image container div set to position:relative
You could do something like this. This would force the width of the special-content container to fill the width of the window, beyond the parent container width.
JSfiddle: http://jsfiddle.net/tm752gr0/4/
HTML:
<div class="container">
<div class="content"></div>
<div class="content special-content"></div>
<div class="content"></div>
<div class="content"></div>
</div>
CSS:
.container {
max-width:500px;
margin:0 auto;
/* position:absolute; */
border:1px solid #000;
}
.special-content {
margin:0 -1000px;
padding:0 1000px;
/* position:absolute; */
width:100%;
border:1px solid #000;
}
.content {
overflow:hidden;
display:block;
border:1px solid #000;
width:100%;
}
* {
height:30px;
}
body {
overflow-x:hidden;
}
Note: I commented out the position:absolute to be able to illustrate how the concept works.
If you can use vw then try this:
Can I use
<div id="parent">
Yay!
<div id="child">ABC123</div>
</div>
#parent {
position: absolute;
max-width: 500px;
border: 1px solid gray;
}
#child {
position: absolute;
width: 100vw;
border: 1px solid yellow;
}
Fiddle for you

CSS Overflow with absolute positioning

I am trying to add a overflow scroll to jQueryUI draggable and resizable elements. Due to bug in jQuery UI, all resizable elements are absolute positioned by default and overflow does not work.
HTML
<div class="container">
<div class="object" style="top:10px;"></div>
<div class="object" style="top:20px;"></div>
<div class="object" style="top:30px;"></div>
<div class="object" style="top:40px;"></div>
<div class="object" style="top:50px;"></div>
</div>
CSS
.container{
width: 100%;
height: 40px;
background-color:#000000;
top:0px;
overflow:scroll;
}
.object{
position:absolute;
height:5px;
background-color: #AAAAAA;
left:100px;
width:50px;
}
Is it possible to add overflow scroll to container with absolute positioned divs?
jsfiddle
Yes, you can, use position: relative; on the parent container
Demo
Demo 2
.container{
width: 100%;
height: 40px;
background-color:#000000;
top:0px;
overflow:scroll;
position: relative;
}

div on top-left and bottom-right coner of parent div

I want to position first div to top-left of parent div and second div to bottom-right of parent div . Here is my code !
<div class="parent">
<div class="tl">TopLeft</div>
<div class="br">BottomRight</div>
</div>
Here is my css ,
.parent
{
width: auto;
height:300px;
background: Black;
}
.tl
{
width:100px;
height:40px;
background:Aqua;
}
.br
{
position:absolute;
width:100px;
height:40px;
right:0;
bottom:0;
background:Aqua;
}
By my code , the topLeft div is in the correct position , but the bottom-right div is outside of parent div . Want to know what I need in my code !
Here is Fiddle !
You need to set the parent element's position property to relative. That will make the children position themselves correctly in relation to the parent rather than the document.
.parent {
...
position: relative;
}
Example: http://jsfiddle.net/grc4/dQCpy/1/
.parent
{
width: auto;
height:300px;
background: Black;
position:relative;
}
Parent must have a relative position.
<style>
.parent{
background-color: yellow;width: 500px;
}
.tl{
background-color: yellowgreen;float: left;width: 200px;
}
.br{
background-color: wheat;float: right;width: 100px;
}
.clr{
clear:both;
}
</style>
<div class="parent">
<div class="tl">TopLeft</div>
<div class="clr"></div>
<div class="br">BottomRight</div>
<div class="clr"></div>
</div>