I started to learn CSS, and I got to that example in the internet:
https://www.w3schools.com/css/tryit.asp?filename=trycss_margin_shorthand
But I tried to play with it, and changed some things. Now the code looks like:
div#my {
border: 8px solid black;
margin-left: 0%;
margin-right: 0%;
height: 500px;
background-color: red;
}
div {
border: 5px solid black;
margin: inherit;
background-color: lightblue;
}
<div id="my">
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</div>
What I see is a big blue block inside a big red block. The blue block collapse with his left and right sides to the red block's border, but from the top I have margin for some reason.
I think its because I chose inherit in the css element. But that says that I have a default margin to the page. Please, help here to understand.
As noone seems to have explained this properly I will try for you.
The inherit keyword in css means that you inherit the styles directly from it's parent. As you have set all divs to inherit their margins, they will inherit that property from their direct parent.
The final div (the div that is nested inside #my) will inherit from the #my div. - As you have not set any margin for top and bottom, this will also stay as inherit.
This means that the #my div will inherit the top and bottom margin (left and right have been set and so will not be inherited) from it's parent, which is the body tag.
The body tag has 8px of margin (depending on which browser you use - I use chrome) so this is inherited by all the divs, which is why you have 8px margin at the top of the inner div. If you want no margin on that final div, you can either remove the margin property altogether, or just set it to 0 instead of inherit.
So to summarise inner div inherits from parent div called my, which in turn inherits from parent body tag, which is set to 8px (or browser default) so your margin comes from your body tag.
More information on css inherit
There's default margin to body tag of 8px which gets attached to your child divs because of explicitly written rule margin: inherit. You have to change the margin form inherit to 0 or remove it, in order to child elements don't have weird (default) margins.
div#my {
border: 8px solid black;
margin-left: 0%;
margin-right: 0%;
height: 500px;
background-color: red;
}
div {
border: 5px solid black;
margin: 0;
background-color: lightblue;
}
<div id="my">
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</div>
There is a default margin on the body which is also inherited in your case (with margin: inherit), you can reset it like this:
html,
body {
margin: 0;
}
div#my {
border: 8px solid black;
margin-left: 0%;
margin-right: 0%;
height: 500px;
background-color: red;
}
div {
border: 5px solid black;
margin: inherit;
background-color: lightblue;
}
<div id="my">
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</div>
W3C schools explanation of inherit
The inherit keyword specifies that a property should inherit its value
from its parent element.
In your code you have inherited the margin property of ALL DIV.
The div with id "my": inherit margin top and bottom from body (8px of margin), left and right are set.
The div without id: inherit the margin top and bottom from div#id (always 8px) and the left and right from div#id (0% = 0px).
To better understand I added a different margin to body, and set a different margin left/right on div#my.
body {
margin:50px;
background-color:yellow;
}
div#my {
border: 8px solid black;
margin-left: 5px;
margin-right: 0%;
height: 500px;
background-color: red;
}
div {
border: 5px solid black;
margin: inherit;
background-color: lightblue;
}
<div id="my">
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</div>
Related
I am placing a div inside another div. Please see the code below JS Fiddle link
HTML:
<body>
<div>
<div class="wrapper"><div class="set1"></div></div>
</div>
</body>
CSS:
* {
box-sizing: border-box;
}
.wrapper{
border: 1px solid black;
padding: 0;
}
.set1{
width: 100px;
height: 100px;
background-color: grey;
border: 5px solid;
margin: 0;
}
Here I am expecting the height of parent div = the height of child. But that's not the case as seen in fig below. I have applied box-sizing : border-box hence the border for the parent should be included in it's height but is not the case. Can you please explain? Also how can I make the child to fully occupy the parent in such case?
It is because of border(1px each side), use maybe outline instead.
set1
wrapper
As already mentioned, you are using a border on wrapper which adds these 2px to height.
If you want that the wrapper has the same height as the parent, you can use a margin on set1 div.
Like this:
.set1{
width: 100px;
height: 100px;
background-color: grey;
border: 5px solid;
margin: -1px 0;
}
So the margin should always have the height of the wrapper border.
Here your working fiddle: https://jsfiddle.net/gzs9u7m2/2/
Actually it has to be a little less that 100% because 100% sets it a little off screen to the right. When I load my page on a tablet or laptop screen it zooms into the top left of the screen, if I zoom out I see that the divs only take up about half the page left to right. I've tried playing around with min-width with no luck.
.first {
height: 75px;
width: 99.55%;
background-color: red;
margin-top: 19px;
border: 3px yellow solid;
min-width: 1000px;
margin: 0 auto;
}
h2 {
text-align: center;
width: 100%;
}
<div class="animated fadeInUpBig first o">
<h2>Need to fix positioning on mobile</h2>
</div>
<div class="animated fadeInUpBig first t">
<h2>More work will be done tomorrow evening</h2>
</div>
<div class="animated fadeInUpBig first tr">
<h2>Make it look half good by weekend</h2>
</div>
If you are wanting each red block with yellow border to span a specific width (to be 90%, 300px, etc.), you can add box-sizing: border-box to the element which allows for the width to include both padding and border. However since both the .first (a div) and the h2 are both block levels elements, by default they will take up 100% width of their parent.
I consolidated the margin-top with the margin: 0 auto; which keeps the margin: from overriding the margin-top.
Also the shorthand for border should be width, style, and color, so I have altered it slightly.
Lastly, the body element has a default margin of 8px in most browsers, so in order to get your boxes to touch the outside of the browser window, you'll want to add that last body property or use a common CSS reset (such as http://meyerweb.com/eric/tools/css/reset/)
.first {
height: 75px;
background-color: red;
border: 3px solid yellow;
min-width: 1000px;
margin: 19px auto 0;
}
h2 {
text-align: center;
}
body {
margin: 0;
}
http://codepen.io/phawley/pen/amLRYK
As you can see in this picture, I've got an orange div inside a green div with no top border. The orange div has a 30px top margin, but it's also pushing the green div down. Of course, adding a top border will fix the issue, but I need the green div to be top borderless. What could I do?
.body {
border: 1px solid black;
border-top: none;
border-bottom: none;
width: 120px;
height: 112px;
background-color: lightgreen;
}
.body .container {
background-color: orange;
height: 50px;
width: 50%;
margin-top: 30px;
}
<div class="header">Top</div>
<div class="body">
<div class="container">Box</div>
</div>
<div class="foot">Bottom</div>
You could add overflow:auto to .body to prevent margin-collapsing. See http://www.w3.org/TR/CSS2/box.html#collapsing-margins
What you experience is margin collapsing. The margin doesn't specify an area around an element, but rather the minimum distance between elements.
As the green container doesn't have any border or padding, there is nothing to contain the margin of the orange element. The margin is used between the top element and the orange element just as if the green container would have the margin.
Use a padding in the green container instead of a margin on the orange element.
Use padding instead of margin:
.body .container {
...
padding-top: 30px;
}
Not sure if this will work in your case, but I just solved this with the following CSS properties
#element {
padding-top: 1px;
margin-top: -1px;
}
#element was being pushed down because it's first child element had a margin-top: 30px. With this CSS, it now works as expected :) Not sure if it'll work for every case, YMMV.
You can either add padding-top: 30 on the green box, use relative positioning on the orange box with top: 30px, or float the orange box and use the same margin-top: 30px.
You read this document:
Box model - Margin collapsing
CSS
.body {
border: 1px solid black;
border-bottom: none;
border-top: none;
width: 120px;
height: 112px;
background-color: lightgreen;
padding-top: 30px;
}
.body .container {
background-color: orange;
height: 50px;
width: 50%;
}
Not sure how hackish this sounds, but how about adding a transparent border?
Two divs are side by side, one is floating left with a width of 25%, the other just has a width of 75%. But when padding is applied on the right hand div, the padding doesn't work properly.
Here is a JSfiddle example:
http://jsfiddle.net/88upt/
<div id="top">
</div>
<div id="middle">
</div>
<div id="bottom">
</div>
CSS
#top {
float: left;
background-color: green;
width: 25%;
height: 100%;
}
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
}
#bottom {
background-color: red;
min-height: 70%;
}
Can someone explain to me why this is happening?
Thanks
Floating something is kind of like making it's position absolute. It will hover on top of it's neighboring containers. Add a margin-left equal to the width of the floated element to make the container the correct width.
http://jsfiddle.net/88upt/4/
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
margin-left:25%
}
EDIT Elaborating a bit more.
The floated element pushes the content of the sibling elements over. It will not push the left side of the content's element over. The padding is there it's just hidden by the floating element.
Add overflow = "auto" in the #middle.
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
overflow: auto;
}
In this way, you don't need to know the width of floating element.
Width doesn't factor in padding.
Source: http://www.w3schools.com/css/css_boxmodel.asp
The width only applies to content, not padding, border, or margin.
You can find more information here.
In the example below why does removing the border on the #enclosing div make the background lightblue color not fill the entire div background anymore?
#enclosing
{
background: lightblue;
margin: 0;
border: 1px solid blue;
}
#outer
{
margin: 40px;
}
#inner
{
margin: 20px;
border: 1px solid black;
}
<div id="enclosing">
<div id="outer">
<div id="inner">This is nested div</div>
</div>
</div>
Example also on JSFiddle
The outer div has margin, which needs something to "push" against.
When the enclosing div has no border (or padding), there is nothing for the margin of the outer div to push against.
Adding border or padding to the top/bottom of the div gives it the necessary containment for the outer div to calculate off of.
I believe this is what's known as collapsing margins in the Box Model