Why does the outer div here not completely surround the inner div?
What you want can be done using a combination of inline-block
and min-width:100%
. A block element has its width defined based on its parent element (containing block) while inline-block
will have its width defined by its content.
Adding the min-width:100%
will make it behave as block element. it's not mandatory in this case since you already have a lot of content so you are sure to cover all the width:
body {
margin: 4em;
}
.demo {
background-color: #BFC;
box-sizing: border-box;
margin: 0;
padding: 1em;
position: relative;
display:inline-block;
min-width:100%;
}
.demo p:first-child {
margin-top: 0;
}
.other-stuff {
align-items: center;
display: flex;
}
button {
margin-right: 1em;
}
.square {
display: inline-block;
background-color: #699;
height: 80px;
margin-right: 1em;
min-width: 80px;
width: 80px;
}
.circle {
border-radius: 50px;
display: inline-block;
background-color: #969;
height: 100px;
margin-right: 1em;
min-width: 100px;
width: 100px;
}
<div class="demo">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
</div>
Why does inner DIV spill out of outer DIV?
Because of the margin - the width is 100% PLUS the margin. To avoid that, write width: calc(100% - 10px);
(= twice the margin.) Same for height.
#outer { position: fixed; width: 30%; height: 30%; background-color: black;}#inner {
width: calc(100% - 10px); height: calc(100% - 10px); margin: 5px; background-color: orange;}
<div id="outer"> <div id="inner"> </div></div>
Why does this inner div show up below the outer div?
Your problem is not with the absolute position. Indeed inner div will have top,left of outer div as root coordinates, the problem is laying down in how divs (block elements) are rendered when no width/height attributes are set, check this below it will answer your question. :)
<div id="outer" style="position: relative; background-color: #f1af1f; width: 100px;">
Outer
<div id="inner" style="position: absolute; top: 0; right: 0;">
Inner
</div>
</div>
Edit:
I updated the code above, you can run it in your browser and you'll see the follow (I will try to explain how the browser understands it).
A yellow rectangle with text "Outer" on left and "Inner" on right on the same line. The rectangle is 100px width. Now how the browser understands the code above.
First the browser parses the html into DOM Tree, so you can this structure (simplified)
body ->
div#outer -> // position relative, yellow background, 100px in width, auto-height
div#inner -> // position absolute, top offset 0 units, right offset 0 units.
Now the browser renders div#outer at top left corner of the browser's window, with the width of 100px, auto calculates the height based on the font used to display the string "Outer". Then it moves to its children (div#inner). It sees its positioned absolute with top and right. Now the browser knows div#outer is positioned relatively so it sets (top,left) coordinates of div#inner to start from (top,left) coordinates of div#outer. Once this is measured, the browser knows how to set top and right. In our case the bounds of div#outer are (top, left, right, bottom): 0, 0, 100, X (depending on div#outer content height). So div#inner position start from div#outer top border to the bottom and from its right border to the left one (depending on the content).
Now, one this to mention here. If you have more content in your div#inner then the height/width of your div#outer the inner one will overlap and go out the boundaries of div#outer. You can fix this by adding: overflow: hidden; to div#outer.
Hope this makes the thing more clear.
Note: I skipped details on how position absolute is handled by the browser.
Inner div not taking the whole scroll width of outer div
Yes, simply set the .innerDiv
to display: inline-block;
. This way, the .innerDiv
behaves like an inline element, which is always as wide as its content, if you don't specify a width
.
Here is the working example:
.outerDiv { max-width:500px; border:2px solid purple; overflow:auto; height: 100px;}.innerDiv { display: inline-block; /* ← this does the trick */ border:1px dashed red; background-color: lightgreen; white-space:nowrap; /* width: 100%; ← this has to be removed */}
<div class="outerDiv"> <div class="innerDiv"> This is inner div This is inner div This is inner div This is inner div This is inner div This is inner div This is inner div </div></div>
How to make an inner div border expand to outer div
For older browsers (without grid support) this can be a solution (paddings/position values are just examples to see some space around the content)
.outer {
background: yellow;
position: relative;
padding: 10px;
}
.inner {
position: relative;
}
.inner:before {
content: '';
border: 1px solid red;
position: absolute;
bottom: -10px;
left: -10px;
right: -10px;
top: -10px;
}
.inner:focus {
outline: 0;
position: static;
}
.inner:focus:before {
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255,0,0,.1);
}
<div class="outer">
<p>Dolor sit amet...</p>
<p>Dolor sit amet...</p>
<div class="inner" tabindex="-1">
lorem ipsum...
</div>
</div>
Nested floating divs cause outer div to not grow
Non-float blocks containing float blocks will not automatically expand, since float blocks are taken outside the normal flow (or at least specially outside the flow). One way to correct that is to specify the overflow
property to hidden
or auto
.
.tip-box { overflow: auto; }
See quirksmode for more.
Outer div accommodating the inner divs is too large by a few pixels
Add display:table-cell
to .plugin
and canvas
.plugin {
position: absolute;
z-index: 800;
border-bottom-right-radius: 5px;
-webkit-box-shadow: 5px 5px 12px 1px rgba(0, 0, 0, 0.6);
box-shadow: 5px 5px 12px 1px rgba(0, 0, 0, 0.6); width:auto;
display:table-cell;
}
.plugin_canvas {
position: relative;
background-color: black;
border: 1px solid #300;
border-bottom-right-radius: 5px;
z-index: 800;
display:table-cell;
}
DEMO
Safari 6.1 hides inner div, when outer div has position:fixed and overflow:auto
Workaround
If overflow:auto
is removed or fixed
is changed to absolute
the child is showed.
Example: http://jsfiddle.net/5kfbe/
This may not solve the problem in all situations though, e.g. when you need overflow:auto
and position:fixed
to be set on your parent div. Anyone got a better solution?
Make the width of outer div to fit inner divs automatically
Your outer
div is a block-level element. You need to make it an inline-level element. Inline
elements automatically take the size of the content it contains. In terms of the question you've asked, just setting :
display: inline-block
on your outer div will do the trick. See the code snippet below for the demo :
body { font-size: 0; } #outer { border: 1px solid black; display: inline-block; } .inner { font-size: 12px; display: inline-block; border: 1px solid red; }
<div id='outer'>
<div class='inner'> text1 </div> <div class='inner'> text2 </div>
</div>
How can I make a inner-div fixed on the bottom of outer-div, so it moves when the outer-div expands
Here is a fiddle Fiddley de fiddly da
What you want is:
#inner {
position:relative;
top: 80%;
width: 100%;
height: 100%;
}
#outer {
width:200px;
height: 100px;
}
Hope it helps. May the code be with you.
Related Topics
Do I Use ≪Img≫, ≪Object≫, or ≪Embed≫ For Svg Files
Change Color of Png Image Via Css
Force Ie Compatibility Mode Off Using Tags
Align an Element to Bottom With Flexbox
Can an Option in a Select Tag Carry Multiple Values
How to Remove the Arrow from a Select Element in Firefox
Invert CSS Font-Color Depending on Background-Color
Why the Content Is Not Covered by the Background of an Overlapping Element
How to Set the Margin or Padding as Percentage of Height of Parent Container
With Ng-Bind-Html-Unsafe Removed, How to Inject Html
Difference Between HTML Div and Span Elements
How to Write a CSS Selector Selecting Elements Not Having a Certain Class or Attribute
How to Use Colspan and Rowspan in HTML Tables
Using ≪Style≫ Tags in the ≪Body≫ With Other Html
Set Textview Text from Html-Formatted String Resource in Xml