브라우저는 CSS visual formatting model에 따라 컴퓨터 스크린(continuous media) 또는 프린트 양식(paged media)에 document tree를 어떻게 display할 지 결정한다.
visual formating model은 box model에 따라 element를 box 형태로 생성하며 box의 레이아웃은 아래 요소에 따라 결정된다.
padding edge
border edge
background의 default는 border-box까지 채우며 : box-sizing과는 무관
https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip
background-clip:padding-box;
background-clip:content-box;
background-clip:text;
background: linear-gradient(90deg, blue, red, yellow, green);
color:rgba(256,256,256,0.1);
inline box는
<div>
<span>hello</span>
<span>hello</span>
<span>hello</span>
</div>
span {
border: solid 1em;
padding: 1em;
margin: 1em;
}
span:nth-child(2) {
border: solid 2em;
padding: 2em;
margin: 2em;
}
span:last-child {
border: solid 3em;
padding: 3em;
margin: 3em;
}
<div>
<span>inline</span>
</div>
<div>
<span>inline</span>
</div>
span{
border: solid 1em; // border에 의한 height가 발생하지 않는다.
padding: 1em; // 적용x
margin:0;
}
div:last-of-type span{
line-height:4;
}
CSS boxes의 display type은
위에서 설명한 block/inline box는 outer display types에 해당되어 element의 layout flow에 영향을 미치고
inner display types는 children elements의 flow를 결정한다.
inner display
element의 size와 position은 property의 value에 따라 containing block에 의해 결정되기도 한다.
element의 containing block은, element의 position 속성값에 의해 결정된다.
일반적으로 containing block은 block-level의 ancestor element의 content-box이지만, 이는 상황에 따라 달라질 수 있다.
특수한 상황에서의 element의 containing block은 element의 position value에 의해 결정되며 이를 구분하면 다음과 같다.
static, relative, sticky인 경우
absolute
height
에 따라 계산width
bottom margin과 top margin이 single margin으로 collapse되는 현상으로
floating / absolutely positioned element는 margin collapsing이 발생하지 않으며
다음과 같이 elements에서 margin이 겹칠 때 발생한다.
adjacent siblings
<div>bottom margin</div>
<div>top margin</div>
div:first-child{
margin-top:1em;
};
div:last-child{
margin-bottom:1em;
};
- parent와 decendants 사이에 box 정보가 없을 때
```css
<div id="parent">
<div id="child">parent no content/padding/border</div>
</div>
1.
#parent{
margin-top:10em;
border: 1px;
}
#child{
margin-top:10em;
}
2.
#parent{
margin-top:10em;
}
#child{
margin-top:10em;
}
```
1. parent가 border를 가질 떄 : margin collapsing이 발생하지 않는다.
2. parent의 margin을 제외한 box model 정보가 없을 떄
- empty blocks