margin as a part of box model of the element without any sibling element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sedigh mohseni
    New Member
    • Feb 2011
    • 64

    margin as a part of box model of the element without any sibling element

    hi

    we define margin as a part of box model of the element but what happen when i define margin for an element that don't have any sibling, are margins of that element affected?

    for example see the below example:
    Code:
    <CODE>
    <html>
    <head>
    <style type="text/css">
    p
    {
    color:blue;
    text-align:center;
    }
    .marked
    {
    background-color:red;
    }
    .marked p
    {
    color:white;
    margin: 50px;
    }
    </style>
    </head>
    
    <body>
    <p>This is a blue, center-aligned paragraph.</p>
    <div class="marked">
    <p>This p element should not be blue.</p>
    </div>
    <p>p elements inside a "marked" classed element keeps the alignment style, but has a different text color.</p>
    </body>
    </html>
    </CODE>
    you can see above example at http://www.w3schools.com/css/tryit.a...trycss_nesting,
    i added margin: 10px; declaration to .marked p selector only.

    In above example we have a p element that is matched with .marked p selector and this element doesn't have any sibling , I define margin: 50px for that but this margin is not affected because background-color of div element with marked class is spread under the p element without any margin for that, is that for the sake of no sibling exist?
    Last edited by Niheel; Jun 11 '11, 08:06 PM. Reason: please use code tags
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Whats happening here is called "collapsing margins". If the div of class "marked" were to have padding or a border the margins of .marked p would be applied within the div. However due to collapsing margins the div and the paragraph share the margin.

    Collapsing Margins

    Comment

    • sedigh mohseni
      New Member
      • Feb 2011
      • 64

      #3
      okay , when two margin areas place beside without any separator space , they are collapsed into one space toward larger margin area.

      Comment

      Working...