<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div, span {
width: 80px;
height: 80px;
margin: 20px;
}
div{
background: pink;
}
span {
background: skyblue;
}
</style>
</head>
<body>
<!-- Block-level -->
<div></div>
<div></div>
<div></div>
<!-- Inline-level -->
<span>1</span>
<span>2</span>
<span>3</span>
</body>
</html>
div
는 기본적으로 Block level이기 때문에 한 줄에 하나만 나온다. span
은 Inline level이기 때문에 공간이 많으면 한 줄에 여러개가 나온다.span
은 안에 내용이 있어야 출력이 된다.<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div, span {
width: 80px;
height: 80px;
margin: 20px;
}
div{
background: pink;
display: inline;
}
span {
background: skyblue;
display: block;
}
</style>
</head>
<body>
<!-- Block-level -->
<div>1</div>
<div>2</div>
<div>3</div>
<!-- Inline-level -->
<span>1</span>
<span>2</span>
<span>3</span>
</body>
</html>
inline-block
은 상자이긴 한데 한 줄에 여러개가 진열될 수 있는 특별한 상자이다.(block
은 한 줄에 하나만 나온다.)<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div, span {
width: 80px;
height: 80px;
margin: 20px;
}
div{
background: pink;
display: inline-block;
}
span {
background: skyblue;
display: block;
}
</style>
</head>
<body>
<!-- Block-level -->
<div>1</div>
<div>2</div>
<div>3</div>
<!-- Inline-level -->
<span>1</span>
<span>2</span>
<span>3</span>
</body>
</html>
position: static;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div {
width: 50px;
height: 50px;
margin-bottom: 20px;
background: pink;
}
.container {
background: #D3D3D3
left: 20px;
top: 20px;
}
.box {
background: skyblue;
}
</style>
</head>
<body>
<article class="container">
<div></div>
<div class="box">I'm Box</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
position: relative;
.container
, .box
의 위치가 위쪽과 왼쪽에서 20px씩 이동했다. 이것을 통해 relative는 원래 있어야 할 위치에서 옮겨진다는 것을 알 수 있다.<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div {
width: 50px;
height: 50px;
margin-bottom: 20px;
background: pink;
}
.container {
background: gray;
left: 20px;
top: 20px;
position: relative;
}
.box {
background: skyblue;
left: 20px;
top: 20px;
position: relative;
}
</style>
</head>
<body>
<article class="container">
<div></div>
<div class="box">I'm Box</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
position: absolute;
absolute
의 경우 내 아이템이 담겨 있는 박스를 기준으로 위치가 변경되는 것을 말한다.(여기서는 .container
가 기준이다.)<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div {
width: 50px;
height: 50px;
margin-bottom: 20px;
background: pink;
}
.container {
background: gray;
left: 20px;
top: 20px;
position: relative;
}
.box {
background: skyblue;
left: 20px;
top: 20px;
position: absolute;
}
</style>
</head>
<body>
<article class="container">
<div></div>
<div class="box">I'm Box</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
position: fixed;
fixed
는 상자에서 벗어나서 해당 웹 페이지를 기준으로 위치가 변경된다.<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div {
width: 50px;
height: 50px;
margin-bottom: 20px;
background: pink;
}
.container {
background: gray;
left: 20px;
top: 20px;
position: relative;
}
.box {
background: skyblue;
left: 20px;
top: 20px;
position: fixed;
}
</style>
</head>
<body>
<article class="container">
<div></div>
<div class="box">I'm Box</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
position: sticky;
sticky
는 원래 있어야 할 자리에 있으면서 스크롤을 해도 없어지지 않는다.<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
div {
width: 50px;
height: 50px;
margin-bottom: 20px;
background: pink;
}
.container {
background: gray;
left: 20px;
top: 20px;
position: relative;
}
.box {
background: skyblue;
left: 20px;
top: 20px;
position: sticky;
}
</style>
</head>
<body>
<article class="container">
<div></div>
<div class="box">I'm Box</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>