Tell us what’s happening:
Describe your issue in detail here.
I do not understand the margin part, I think when we add top-margin:20px to the div class=“one”, this div one should be below the div class=“canvas” by 20px, but instead of that, the top margin of the div one is outside the div canvas. Can you explain to me why? Thank you.
**Your code so far**
/* file: styles.css */
border: 50px solid red;
margin: 20px auto;
margin: 20px auto;
/* file: index.html */
<link href="./styles.css" rel="stylesheet">
**Your browser information:**
User Agent is:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/188.8.131.52 Safari/537.36
Challenge: Learn the CSS Box Model by Building a Rothko Painting - Step 21
Link to the challenge:
look at HTML tho.
.one is inside .canvas, right?
Someone please explain this.
Please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.
The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.
Thats right, but the question is:
WHY the top-margin of div class=“one” is outside the div class=“canvas”?
Finally found the explanation.
Top-margin of .one element COLLAPSE with top-margin of .canvas element, which is called Collapsing margin.
Relevant explanation 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 (Note that certain zero-height line boxes (see 9.4.2) are ignored for this purpose.)
- 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
The left and right margins behave as you expect because:
Horizontal margins never collapse.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.