Child div margin in relation to parent div - css

I have a simple child div nested inside a parent div, like so...
I am trying to understand why I cannot move the child div down (ex. 25px), in relation to the parent div, by using margin-top: 25px, unless I give the parent div a border. I am thinking that the child div is using the border as a reference point, which is why the margin-top actually works once the border is applied. That is all fine and dandy, but in the specific example I'm working on, the parent div has a background image, and I don't want to give it a border. But without a border, the child div won't move!
<body>
<div id="main">
<div id="child">
</div>
</div>
</body
#main {width: 500px;
border: 1px solid black;
height: 500px;
background-color: red;
margin: auto;
margin-top: 200px;
}
#child {width: 100px;
height: 100px;
background: blue;
position: relative;
top: 5px;
}

I had this issue few days ago , I resolved it by adding a small padding (1px) to the parent div , and then use margin on the child div.

You should rather give display:inline-block; property to child div.

Related

Why does margin-top on the App component pushes the entire body down unless the body has a border? [duplicate]

I try to add margin values on a div inside another div. All works fine except the top value, it seems to be ignored. But why?
What I expected:
What I get:
Code:
#outer {
width: 500px;
height: 200px;
background: #FFCCCC;
margin: 50px auto 0 auto;
display: block;
}
#inner {
background: #FFCC33;
margin: 50px 50px 50px 50px;
padding: 10px;
display: block;
}
<div id="outer">
<div id="inner">
Hello world!
</div>
</div>
W3Schools have no explanation to why margin behaves this way.
You're actually seeing the top margin of the #inner element collapse into the top edge of the #outer element, leaving only the #outer margin intact (albeit not shown in your images). The top edges of both boxes are flush against each other because their margins are equal.
Here are the relevant points from the W3C spec:
8.3.1 Collapsing margins
In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.
Adjoining vertical margins collapse [...]
Two margins are adjoining if and only if:
both belong to in-flow block-level boxes that participate in the same block formatting context
no line boxes, no clearance, no padding and no border separate them
both belong to vertically-adjacent box edges, i.e. form one of the following pairs:
top margin of a box and top margin of its first in-flow child
You can do any of the following to prevent the margin from collapsing:
Float either of your div elements
Make either of your div elements inline blocks
Set overflow of #outer to auto (or any value other than visible)
The reason the above options prevent the margin from collapsing is because:
Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
Margins of inline-block boxes do not collapse (not even with their in-flow children).
The left and right margins behave as you expect because:
Horizontal margins never collapse.
Try using display: inline-block; on the inner div. Like so:
#outer {
width:500px;
height:200px;
background:#FFCCCC;
margin:50px auto 0 auto;
display:block;
}
#inner {
background:#FFCC33;
margin:50px 50px 50px 50px;
padding:10px;
display:inline-block;
}
What #BoltClock mentioned are pretty solid.
And Here I just want to add several more solutions for this problem.
check this w3c_collapsing margin. The green parts are the potential thought how this problem can be solved.
Solution 1
Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
that means I can add float:left to either #outer or #inner demo1.
also notice that float would invalidate the auto in margin.
Solution 2
Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
other than visible, let's put overflow: hidden into #outer. And this way seems pretty simple and decent. I like it.
#outer{
width: 500px;
height: 200px;
background: #FFCCCC;
margin: 50px auto;
overflow: hidden;
}
#inner {
background: #FFCC33;
height: 50px;
margin: 50px;
}
Solution 3
Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).
#outer{
width: 500px;
height: 200px;
background: #FFCCCC;
margin: 50px auto;
position: absolute;
}
#inner{
background: #FFCC33;
height: 50px;
margin: 50px;
}
or
#outer{
width: 500px;
height: 200px;
background: #FFCCCC;
margin: 50px auto;
position: relative;
}
#inner {
background: #FFCC33;
height: 50px;
margin: 50px;
position: absolute;
}
these two methods will break the normal flow of div
Solution 4
Margins of inline-block boxes do not collapse (not even with their in-flow children).
is the same as #enderskill
Solution 5
The bottom margin of an in-flow block-level element always collapses with the top margin of its next in-flow block-level sibling, unless that sibling has clearance.
This has not much work to do with the question since it is the collapsing margin between siblings. it generally means if a top-box has margin-bottom: 30px and a sibling-box has margin-top: 10px. The total margin between them is 30px instead of 40px.
Solution 6
The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.
This is very interesting and I can just add one top border line
#outer{
width: 500px;
height: 200px;
background: #FFCCCC;
margin: 50px auto;
border-top: 1px solid red;
}
#inner {
background: #FFCC33;
height: 50px;
margin: 50px;
}
And Also <div> is block-level in default, so you don't have to declare it on purpose. Sorry for not being able to post more than 2 links and images due to my novice reputation. At least you know where the problem comes from next time you see something similar.
Not sure why what you have doesn't work, but you can add overflow: auto; to the outer div.
Not exactly sure why, but changing the inner CSS to
display: inline-block;
seems to work.
If you add any padding to #outer, it works. Demo here:
#outer {
width:500px;
height:200px;
background:#FFCCCC;
margin:50px auto 0 auto;
display:block;
padding-top:1px;
}
#inner {
background:#FFCC33;
margin:50px 50px 50px 50px;
padding:10px;
display:block;
}
<div id="outer">
<div id="inner">
Hello world!
</div>
</div>
Doesn't answer the "why" (has to be something w/ collapsing margin), but seems like the easiest/most logical way to do what you're trying to do would be to just add padding-top to the outer div:
#outer {
width:500px;
height:200px;
background:#FFCCCC;
margin:50px auto 0 auto;
padding-top: 50px;
}
#inner {
background:#FFCC33;
margin:0px 50px 50px 50px;
padding:10px;
}
<div id="outer">
<div id="inner">
Hello world!
</div>
</div>
Minor note - it shouldn't be necessary to set a div to display:block; unless there's something else in your code telling it not to be block.
Create new block formatting context
You can use display: flow-root on the parent element to prevent margin collapsing through the containing element as it creates new Block Formatting Context.
Changing the value of the overflow property to auto or using flexbox will have the same effect.
https://codepen.io/rachelandrew/pen/VJXjEp
Try this:
#outer {
width:500px;
height:200px;
background:#FFCCCC;
margin:50px auto 0 auto;
display:table;
}
#inner {
background:#FFCC33;
margin:50px 50px 50px 50px;
padding:10px;
display:block;
}
<div id="outer">
<div id="inner">
Hello world!
</div>
</div>
Good luck!
I guess setting the position property of the #inner div to relative may also help achieve the effect. But anyways I tried the original code pasted in the Question on IE9 and latest Google Chrome and they already give the desirable effect without any modifications.
Use padding-top:50pxfor outer div. Something like this:
#outer {
width:500px;
height:200px;
background:#FFCCCC;
margin:50px auto 0 auto;
display:table;}
Note: padding will increase the size of your div. In this case if the size of your div is important, I mean if it must have a specific height. decrease the height by 50px.:
#outer {
width:500px;
height:150px;
background:#FFCCCC;
margin:50px auto 0 auto;
display:table;}
Have you tried !important before all, it will force everything:
margin:50px 50px 50px 50px !important;
If you have a margin collapse issue, then to resolve this you can add
display: flow-root; to the parent container.
Aside from that, if margin-top is being ignored, try margin-top with a negative value, for instance: margin-top: -2px;
Just for a quick fix, try wrapping your child elements into a div element like this -
<div id="outer">
<div class="divadjust" style="padding-top: 1px">
<div id="inner">
Hello world!
</div>
</div>
</div>
Margin of inner div won't collapse due to the padding of 1px in-between outer and inner div. So logically you will have 1px extra space along with existing margin of inner div.

child div with border radius shows color of parent div at borders

I have 2 divs, one is parent and 2nd is child div as follows
.outer{
background: #211182;
height: 50px;
}
.inner{
height: 50px;
border-radius: 60px 0 0 0;
background: #f9f9f9
}
<div class="outer">
<div class="inner">
</div>
</div>
only child div has border-radius.
it shows parent background color at border of child div which is not expected.
I tried overflow: hidden , also tried border : none and border-width : 0 and width : 100%; height:100% but no luck.
please help me to resolve this.
Attaching screenshot of the result.

problems with a negative top margin of a relative positioned element (vertical alignment)

I'm trying to align vertically a div inside a container with a height defined. I'm following the guide of http://www.vertical-align.com/, but I'm facing some issues.
According to the website, if I use this css with for this code:
#containingBlock {
height: 200px;
position: relative;
overflow: hidden;
border: 1px solid red;
}
#containingBlock > div {
position: absolute;
top: 50%;
border: 1px solid green;
}
#containingBlock > div > div {
position: relative;
top: -50%;
border: 1px solid orange;
}
<div id="containingBlock">
<div>
<div>
This should be placed in the middle
</div>
</div>
</div>
Fiddle available here
I should obtain a text perfectly in the middle. But this doesn't happen because the top: -50% doesn't work. According to Mozilla dev the top property + % value should be based on the parent's height, which has the same height of its child automatically in this case. But the "automatic wrap height" does not seem to be take into consideration. If I specify a explicit height for the parent div (I mean, the first one nested), everything seems to be ok, but I would like it to take the height of its child automatically! What's wrong with this?
If the height of the block to be positioned is known you can affect the correct positioning with negative margin (i.e 50% of the known height).
If it is not known you can affect it with a CSS transform as follows
-webkit-transform:translate(0%, -50%);
This moves the object vertically half it's own height...and so on
HTML
<div class="containingBlock one">
<div>
This should be placed in the middle
</div>
</div>
CSS
.containingBlock {
height: 200px;
position: relative;
border: 1px solid red;
}
.containingBlock > div {
position: absolute;
top: 50%;
border: 1px solid green;
-webkit-transform:translate(0%, -50%);
}
JSfiddle
here's a fiddle: http://jsfiddle.net/dC22r/4/
you have to set an height to the div that has to be centered then give it top:50% and subtract half his height with a negative margin.

Child divs overflow

I have a parent div inside which I have multiple divs which are children of parent div, But I don't get that why those child divs are overflowing outside the parent div,
I want parent div to adjust it's height according to the number of child divs inside it.
Heres the fiddle
Just make overflow:hidden instead of overflow:visible
Demo
Float #downloads left.
http://jsfiddle.net/CvMNH/1/
Either float #downloads left, or use overflow:hidden:
#downloads
{
background-color: #EEEEEE;
border: 1px solid #CCCDCF;
padding: 5px;
line-height: 25px;
overflow:hidden;
}​
or:
#downloads
{
background-color: #EEEEEE;
border: 1px solid #CCCDCF;
padding: 5px;
line-height: 25px;
float: left;
}​
overflow: hidden will make the overflowing child content simply hidden, which, I believe, is not what you are trying to achieve.
To expand the parent div to show all child content, either specify overflow: auto on the parent (may have side effects in some browsers), or put
<div style="clear: both"></div>
as the last child in your parent div.

How to add a border to an image hidden in part?

This is my code ( if the image has an height higher than 100px then show only the first 100px of the image and hide the rest ):
HTML:
<div>
<img>
</div>
CSS:
div{
max-height:100px;
overflow:hidden;
}
Now, I need to add a border of 5px:
img{
border:solid 5px #555555;
}
but if the image has an height higher than 100px, the bottom border doesn't appear. How can I resolve that ?
That's because the div's overflow is hidden. Instead, you should set the border on the div. This way, the div will only take up the height of the image but after 100px, it'll keep the border but the image won't show past that. You will need to adjust the width of the div to fit the image, though.
<span><img src="" /></span>
span{
max-height:100px;
border:solid 5px #555;
overflow: hidden;
display: inline-block
}
img {
width: 200px; /* image width */
height: 200px; /* image height */
}
Using a span with display: inline-block you no longer need to set the width of the outer container.
Issue: when image size is less than 100px, there is a small gap between the image bottom and outer container.
demo
Take a peek if this is what you need:
(1st try) http://jsfiddle.net/Vn4PM/
(last try) http://jsfiddle.net/Vn4PM/11/
HTML (with sample graphics):
<div>
<img src="http://www.dummyimage.com/100x150/ffff00/fff" />
</div>​
<div>
<img src="http://www.dummyimage.com/100x50/ffff00/fff" />
</div>​
CSS:
div {
max-height: 100px;
border: solid 5px #555555;
overflow: hidden;
display: inline-block;
}
img { margin-bottom: -5px; }​
--
display: inline-block, and border added to div so border will wrap around the image
margin-bottom added to img so extra gap is hidden

Resources