element | description |
---|---|
Content | 요소의 텍스트나 이미지 등 실제 내용이 위치하는 영역, width, height property를 가짐 |
Padding | border 안쪽에 위치하는 요소의 내부 여백 영역, padding property 값 = padding 영역의 두께, 기본 색 = 투명색, 요소에 적용된 배경의 색, 이미지는 padding 영역까지 적용 |
Border | 테두리 영역, border property 값 = border 영역의 두께 |
Margin | border 바깥쪽에 위치하는 요소의 외부 여백 영역, margin property 값 = margin 영역의 두께, 기본 색 = 투명색, 배경색 지정 불가능 |
<!DOCTYPE html>
<html>
<head>
<style>
div {
/* 배경색 지정 - 컨텐츠와 패딩 영역에 적용됨 */
background-color: lightgray;
/* 컨텐츠 영역의 너비 */
width: 300px;
/* padding 영역의 두께 */
padding: 25px;
/* 테두리 : 두께 형태 색상 */
border: 25px solid navy;
/* margin 영역의 두께 */
margin: 25px;
}
</style>
</head>
<body>
<h2>Box Model</h2>
<div>
In web development, the CSS box model refers to how HTML elements are modeled in browser engines and how the dimensions of those HTML elements are derived from CSS properties. It is a fundamental concept for the composition of HTML webpages.
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
div {
width: 300px;
height: 100px;
background-color: cornsilk;
border: 5px solid navy;
}
</style>
</head>
<body>
<div>
In web development, the CSS box model refers to how HTML elements are modeled in browser engines and how the dimensions of those HTML elements are derived from CSS properties. It is a fundamental concept for the composition of HTML webpages.
</div>
</body>
</html>
박스 전체 크기 계산
- 전체 너비 : width + left padding + right padding + left border + right border + left margin + right margin
- 전체 높이 : height + top padding + bottom padding + top border + bottom border + top margin + bottom margin
위 box 모델의 전체 크기 계산
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
div {
background-color: beige;
}
</style>
</head>
<body>
<div>This is div</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
div {
background-color: beige;
height: 100px;
width: 50%;
}
</style>
</head>
<body>
<div>This is div</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 5px solid red;
margin-top: 40px;
margin-right: 30px;
margin-bottom: 20px;
margin-left: 10px;
padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 40px;
}
</style>
</head>
<body>
<div>In web development, the CSS box model refers to how HTML elements are modeled in browser engines and how the dimensions of those HTML elements are derived from CSS properties. It is a fundamental concept for the composition of HTML webpages.</div>
</body>
</html>
margin, padding 1개의 property만으로 4방향의 property를 한번에 정할 수 있음
margin: 25px 50px 75px 100px
margin-top: 25px
margin-right: 50px
margin-bottom: 75px
margin-left: 100px
margin: 25px 50px 75px
margin-top: 25px
margin-right: 50px, margin-left: 50px
margin-bottom: 75px
margin: 25px 50px
margin-top: 25px, margin-bottom: 25px
margin-right: 50px, margin-left: 50px
margin: 25px
margin-top: 25px, margin-right: 25px, margin-bottom: 25px, margin-left: 25px
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 5px solid red;
margin: 40px 30px 20px 10px;
padding: 10px 20px 30px 40px;
}
</style>
</head>
<body>
<div>In web development, the CSS box model refers to how HTML elements are modeled in browser engines and how the dimensions of those HTML elements are derived from CSS properties. It is a fundamental concept for the composition of HTML webpages.</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 5px solid red;
width: 600px;
margin: 0 auto;
}
</style>
</head>
<body>
<div>In web development, the CSS box model refers to how HTML elements are modeled in browser engines and how the dimensions of those HTML elements are derived from CSS properties. It is a fundamental concept for the composition of HTML webpages.</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 5px solid red;
max-width: 600px;
margin: auto;
}
</style>
</head>
<body>
<div>In web development, the CSS box model refers to how HTML elements are modeled in browser engines and how the dimensions of those HTML elements are derived from CSS properties. It is a fundamental concept for the composition of HTML webpages.</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p {
background-color: palegreen;
padding: 10px;
}
p.dotted { border-style: dotted; }
p.dashed { border-style: dashed; }
p.solid { border-style: solid; }
p.double { border-style: double; }
p.groove { border-style: groove; }
p.ridge { border-style: ridge; }
p.inset { border-style: inset; }
p.outset { border-style: outset; }
p.none { border-style: none; }
p.hidden { border-style: hidden; }
p.mix { border-style: dotted dashed solid double; }
</style>
</head>
<body>
<h2>border-style Property</h2>
<p class="dotted">dotted</p>
<p class="dashed">dashed</p>
<p class="solid">solid</p>
<p class="double">double</p>
<p class="groove">groove</p>
<p class="ridge">ridge</p>
<p class="inset">inset</p>
<p class="outset">outset</p>
<p class="none">none</p>
<p class="hidden">hidden</p>
<p class="mix">dotted dashed solid double</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p {
background-color: palegreen;
padding: 10px;
}
p.d1 {
/* four sides */
border-style: dashed;
}
p.d2 {
/* horizontal | vertical */
border-style: dotted solid;
}
p.d3 {
/* top | vertical | bottom */
border-style: hidden double dashed;
}
p.d4 {
/* top | right | bottom | left */
border-style: none solid dotted dashed;
}
</style>
</head>
<body>
<p class="d1">border-style: dashed;</p>
<p class="d2">border-style: dotted solid;</p>
<p class="d3">border-style: hidden double dashed;</p>
<p class="d4">border-style: none solid dotted dashed;</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p {
background-color: palegreen;
padding: 10px;
border-style: solid;
}
p.one {
border-width: thin; /* 1px */
}
p.two {
border-width: medium; /* 3px */
}
p.three {
border-width: thick; /* 5px */
}
p.four {
border-width: 15px;
}
p.five {
border-width: 2px 10px 4px 20px;
}
</style>
</head>
<body>
<h2>border-width Property</h2>
<p class="one">thin: 1px</p>
<p class="two">medium: 3px</p>
<p class="three">thick: 5px</p>
<p class="four">15px</p>
<p class="five">2px 10px 4px 20px</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p {
background-color: palegreen;
padding: 10px;
border-style: solid;
}
p.one {
border-color: red;
}
p.two {
border-color: green;
}
p.three {
border-color: red green blue yellow;
}
</style>
</head>
<body>
<h2>border-color Property</h2>
<p class="one">border-color: red</p>
<p class="two">border-color: green</p>
<p class="three">border-color: red green blue yellow</p>
</body>
</html>
border-radius : 테두리 모서리를 둥글게 표현하도록 지정
property 값은 길이 단위 (px, em 등)와 %를 사용
property 개수에 따라 4개의 방향 (top, right, bottom, left) 각각의 모서리에 대하여 지정 가능
하나 혹은 두개의 반지름을 설정하여 각각의 모서리 굴곡을 설정 가능하므로, 원이나 타원 모양으로 정의 가능함
<!DOCTYPE html>
<html>
<head>
<style>
div {
background: #eaeaed;
color: #666;
display: inline-block;
width: 90px;
height: 90px;
line-height: 90px;
margin: 0 14px;
text-align: center;
}
.border-rounded {
/* 꼭지점에 대해 radius 지정 */
border-radius: 5px;
}
.border-circle {
border-radius: 50%;
}
.border-football {
/* top-left & bottom-right | top-right & bottom-left */
border-radius: 15px 75px}
</style>
</head>
<body>
<div class="border-rounded">5px</div>
<div class="border-circle">50%</div>
<div class="border-football">15px 75px</div>
</body>
</html>
.border-rounded {
border-radius: 20px;
/* border-top-left-radius: 20px;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
border-bottom-left-radius: 20px*/
}
<!DOCTYPE html>
<html>
<head>
<style>
div {
background: #eaeaed;
color: #666;
width: 150px;
height: 150px;
line-height: 150px;
text-align: center;
}
.border-rounded {
border-radius: 20px;
}
</style>
</head>
<body>
<div class="border-rounded">border-radius: 20px</div>
</body>
</html>
.border-rounded {
border-radius: 10px 40px 40px 10px;
/* border-top-left-radius: 10px;
border-top-right-radius: 40px;
border-bottom-right-radius: 40px;
border-bottom-left-radius: 10px*/
}
<!DOCTYPE html>
<html>
<head>
<style>
div {
background: #eaeaed;
color: #666;
width: 200px;
height: 150px;
line-height: 150px;
text-align: center;
}
.border-rounded {
/* top-left | top-right | bottom-right | bottom=left */
border-radius: 10px 40px 40px 10px;
}
</style>
</head>
<body>
<div class="border-rounded">10px 40px 40px 10px</div>
</body>
</html>
.border-rounded {
border-top-left-radius: 50px 25px;
}
<!DOCTYPE html>
<html>
<head>
<style>
div {
background: #eaeaed;
color: #666;
width: 300px;
height: 150px;
line-height: 150px;
text-align: center;
}
.border-rounded {
border-top-left-radius: 50px 25px;
}
</style>
</head>
<body>
<div class="border-rounded">border-top-left-radius: 50px 25px</div>
</body>
</html>
.border-rounded {
border-radius: 50px 50px 0 0 / 25px 25px 0 0;
}
<!DOCTYPE html>
<html>
<head>
<style>
div {
background: #eaeaed;
color: #666;
width: 450px;
height: 150px;
padding: 10px;
}
.border-rounded {
border-radius: 10px 20px 30px 40px / 5px 10px 15px 20px;
}
</style>
</head>
<body>
<div class="border-rounded">
border-radius: 10px 20px 30px 40px / 5px 10px 15px 20px;
<ul>
<li>border-top-left-radius: 10px 5px</li>
<li>border-top-right-radius: 20px 10px</li>
<li>border-bottom-right-radius: 30px 15px</li>
<li>border-bottom-left-radius: 40px 20px</li>
</ul>
</div>
</body>
</html>
p {
/* border-width border-style border-color*/
border: 5px solid red;
}
box-sizing | description |
---|---|
content-box | width, height 값은 content 영역을 의미함 (기본값) |
border-box | width, height 값은 content 영역, padding, border 포함된 값을 의미함 |
<!DOCTYPE html>
<html>
<head>
<style>
.content-box {
width: 600px;
border: 10px solid;
padding: 50px;
margin: 50px;
background-color: red;
}
.border-box {
box-sizing:border-box;
width: 600px;
border: 10px solid;
padding: 50px;
margin: 50px;
background-color: red;
}
</style>
</head>
<body>
<div class="content-box">content-box</div>
<div class="border-box">border-box</div>
</body>
</html>
html {
box-sizing: border-box;
}
*,*:before, *:after {
box-sizing: inherit;
}