CSS Note: How to make Child's margin  contribute to parent's height!

CSS Note: How to make Child's margin contribute to parent's height!

CSS really bothers me, whenever I have to use it, I find out more unwritten untold shitty rules! Here we are gonna discuss one of those rules:

Default behavior is that a child element's margin does not affect the height of its parent. Take a look at the snippets below, and how they get rendered in a browser:

.html:

<body>
    <div id="parent1">
        <p id=child1>This is Child 1</p>
    </div>
</body>

.css:

#parent1 {
    background: lightcoral;
}


#child1 {
    margin: 30px 0;
}

and here is the result:

No alt text provided for this image


Now if I add a border to the parent element not only the border is added but also The child element's margin will be part of the content area of it's parent child. Child's margin will contribute to parent's height. I'm just gonna add another div tag just the same as what we already have, and apply the following CSS rule to it:

border: dotted;

see the difference:

No alt text provided for this image

Does this make any sense? Why is it behaving like that?

Well I found an explanation (which didn't really convince) I'm gonna quote it here may be it persuades you that this is the best possible behavior:

So long as the parent has no border, there is the possibility that the child's bottom margin might be collapsed with another element below it. Once the border is defined, that's not possible.

要查看或添加评论,请登录

Shahryar Saljoughi的更多文章

  • How can telnet client act as Http client?

    How can telnet client act as Http client?

    I was just reading this article and got surprised when it said that you can use telnet to send HTTP requests. But…

  • One Way Data Binding in Pure JS

    One Way Data Binding in Pure JS

    It is a simple task to do but we are too accustomed to having it be handled by frameworks to do it in pure javascript…

    1 条评论
  • Drive C is Full? Handle it Like a .Net Developer!

    Drive C is Full? Handle it Like a .Net Developer!

    drive C is always full and it drives me crazy. Whenever I want to install a utility first I need to free up some space .

  • How does JavaScript Hoisting really work?

    How does JavaScript Hoisting really work?

    w3schools js reference simply states that Hoisting is JavaScript's default behavior of moving declarations to the top…

  • misunderstanding of JS Compilation

    misunderstanding of JS Compilation

    We all had heard that JavasSript is an interpreted language, surprisingly in the second step of #cs_internship#web…

  • parseInt() not only parses but also truncates! is it okay?

    parseInt() not only parses but also truncates! is it okay?

    #JavaScript has two global methods named: parseFloat() and parseInt(). On the other hand there is only one data type to…

  • A CSS Note On Combination of Float and Overflow

    A CSS Note On Combination of Float and Overflow

    The code snippet below, will generate a navigation bar like the one above. CTYPE html ul { ? list-style-type:…

    2 条评论

社区洞察

其他会员也浏览了