Position
1. ๊ฐ์
position
ํ๋กํผํฐ๋ ์์์ ์์น๋ฅผ ์ ์ํ๋ค. ํด๋น ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ์น ๋ฌธ์์์ ์์์ ์์น๋ฅผ ์์ ๋กญ๊ฒ ์ ํ ์ ์๋ค. top
, bottom
, left
, right
ํ๋กํผํฐ์ ํจ๊ป ์ฌ์ฉํ์ฌ ์์น๋ฅผ ์ง์ ํ๋ค.
์๋์ ํ๋ position
ํ๋กํผํฐ๊ฐ์ด๋ค.
static
๋ฌธ์์ ํ๋ฆ์ ๋ง์ถฐ ๋ฐฐ์นํ๋ค. ๊ธฐ๋ณธ๊ฐ
relative
์์นซ๊ฐ์ ์ง์ ํ ์ ์๋ค๋ ์ ์ ์ ์ธํ๋ฉด static๊ณผ ๊ฐ๋ค.
absolute
relative๊ฐ์ ์ฌ์ฉํ ์์ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์์น๋ฅผ ์ง์ ํด ๋ฐฐ์นํ๋ค.
fixed
๋ธ๋ผ์ฐ์ ์ฐฝ์ ๊ธฐ์ค์ผ๋ก ์์น๋ฅผ ์ง์ ํด ๋ฐฐ์นํ๋ค.
2. static (๊ธฐ๋ณธ์์น)
๊ธฐ๋ณธ์ ์ธ ์์์ ๋ฐฐ์น ์์์ ๋ฐ๋ผ ์์์ ์๋๋ก, ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์์์ ๋ฐ๋ผ ๋ฐฐ์น๋๋ฉด ๋ถ๋ชจ ์์ ๋ด์ ์์๋ก์ ์กด์ฌํ ๋๋ ๋ถ๋ชจ ์์์ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐฐ์น๋๋ค.
์ขํ ํ๋กํผํฐ(top
, bottom
, left
, right
)๋ฅผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ผ๋ฉฐ ์ฌ์ฉํ ๊ฒฝ์ฐ ๋ฌด์๋๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
.parent {
width: 150px;
height: 150px;
}
.children {
background-color: bisque;
height: 100%;
}
</style>
</head>
<body>
<div class="parent">
<div class="children">Static</div>
</div>
</body>
</html>

3. relative (์๋์์น)
๊ธฐ๋ณธ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ์ขํ ํ๋กํผํฐ(top
, bottom
, left
, right
)๋ฅผ ์ฌ์ฉํ์ฌ ์์น๋ฅผ ์ด๋์ํฌ ์ ์๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
.parent {
width: 150px;
height: 150px;
background-color: rgb(78, 78, 78);
}
.child {
position: relative;
background-color: bisque;
height: 100%;
top: 20px;
left: 20px;
}
</style>
</head>
<body>
<div class="parent">
<div class="child">relative</div>
</div>
</body>
</html>

4. absolute (์ ๋์์น)
๋ถ๋ชจ ์์ ๋๋ ๊ฐ์ฅ ๊ฐ๊น์ด ์๋ ์กฐ์ ์์(static ์ ์ธ)๋ฅผ ๊ธฐ์ค์ผ๋ก ์ขํ ํ๋กํผํฐ(top
, bottom
, left
, right
)๋งํผ ์ด๋ํ๋ค. ์ฆ, relative, absolute, fiexed ํ๋กํผํฐ๊ฐ ์ ์ธ๋์ด ์๋ ๋ถ๋ชจ ๋๋ ์กฐ์ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์์น๊ฐ ๊ฒฐ์ ๋๋ค.
๋ง์ผ ๋ถ๋ชจ ๋๋ ์กฐ์ ์์๊ฐ staitc์ธ ๊ฒฝ์ฐ, document body๋ฅผ ๊ธฐ์ค์ผ๋ก ํ์ฌ ์ขํ ํ๋กํฐํฐ๋๋ก ์์นํ๊ฒ ๋๋ค.
absolute ํ๋กํผํฐ ์ ์ธ ์, block ๋ ๋ฒจ ์์์ width๋ inline ์์์ ๊ฐ์ด content์ ๋ง๊ฒ ๋ณํ๋๋๋ก ์ ์ ํ width๋ฅผ ์ง์ ํ์ฌ์ผ ํ๋ค.
relative ํ๋กํผํฐ๋ ๊ธฐ๋ณธ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ์ขํ ํ๋กํผํฐ(top
, bottom
, left
, right
)๋ฅผ ์ฌ์ฉํ์ฌ ์์น๋ฅผ ์ด๋์ํจ๋ค. ํ์ง๋ง absoulte ํ๋กํฐํผ ์์๋ ๋ถ๋ชจ ์์์ ์์ญ์ ๋ฒ์ด๋ ์์ ๋กญ๊ฒ ์ด๋๋ ์ง ์์นํ ์ ์๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: flex;
}
.parent {
width: 150px;
height: 150px;
margin: 50px;
background-color: rgb(78, 78, 78);
}
.children-relative {
position: relative;
background-color: bisque;
height: 100%;
top: 20px;
left: 20px;
}
.children-absolute {
position: absolute;
background-color: bisque;
width: 150px;
height: 150px;
top: 20px;
left: 20px;
}
</style>
</head>
<body>
<div class="parent">
<div class="children-absolute">absolute</div>
</div>
<div class="parent">
<div class="children-relative">relative</div>
</div>
</body>
</html>

5. fixed (๊ณ ์ ์์น)
๋ถ๋ชจ ์์์ ๊ด๊ณ์์ด ๋ธ๋ผ์ฐ์ ์ viewport๋ฅผ ๊ธฐ์ค์ผ๋ก ์ขํ ํ๋กํผํฐ(top
, bottom
, left
, right
)๋ฅผ ์ฌ์ฉํ์ฌ ์์น๋ฅผ ์ด๋์ํจ๋ค.
์คํฌ๋กค์ด ๋๋๋ผ๋ ํ๋ฉด์์ ์ฌ๋ผ์ง์ง ์๊ณ ํญ์ ๊ฐ์ ๊ณณ์ ์์นํ๋ค.
fixed ํ๋กํผํฐ ์ ์ธ ์, block ๋ ๋ฒจ ์์์ width๋ inline ์์์ ๊ฐ์ด content์ ๋ง๊ฒ ๋ณํ๋๋๋ก ์ ์ ํ width๋ฅผ ์ง์ ํ์ฌ์ผ ํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: rgb(137, 137, 137);
color: white;
padding: 10px;
box-sizing: border-box;
}
.nav {
position: fixed;
width: 100%;
top: 0;
right: 0;
}
.side{
position: fixed;
height: 100%;
top: 0;
right: 0;
}
</style>
</head>
<body>
<div class="nav">fixed nav-bar</div>
<div class="side">fixed side-bar</div>
</html>

6. overflow ํ๋กํผํฐ
overflow ํ๋กํผํฐ๋ ์์ ์์๊ฐ ๋ถ๋ชจ ์์์ ์์ญ์ ๋ฒ์ด๋ฌ์ ๋ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ์ ์ ์ํ๋ค.
์๋๋ overflow ํ๋กํผํฐ ๊ฐ์ ๋ํ ์ค๋ช ์ด๋ค.
visible
์์ญ์ ๋ฒ์ด๋ ๋ถ๋ถ์ ํ์ํ๋ค. ๊ธฐ๋ณธ๊ฐ
hidden
์์ญ์ ๋ฒ์ด๋ ๋ถ๋ถ์ ์๋ผ๋ด์ด ๋ณด์ด์ง ์๊ฒ ํ๋ค.
scroll
์์ญ์ ๋ฒ์ด๋ ๋ถ๋ถ์ด ์์ด๋ ์คํฌ๋กค ํ์ํ๋ค.(ํ์ฌ ๋๋ถ๋ถ ๋ธ๋ผ์ฐ์ ๋ auto์ ๋์ผํ๊ฒ ์๋ํ๋ค.)
auto
์์ญ์ ๋ฒ์ด๋ ๋ถ๋ถ์ด ์์๋๋ง ์คํฌ๋กค ํ์ํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: flex;
}
div {
background-color: rgb(187, 187, 187);
padding: 10px;
box-sizing: border-box;
width: 200px;
height: 200px;
margin-right: 20px;
}
.visible {
overflow: visible;
}
.hidden {
overflow: hidden;
}
.scroll {
overflow: scroll;
}
.auto {
overflow: auto;
}
</style>
</head>
<body>
<div class="visible">
<h3>Visibel</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae veniam quisquam tenetur architecto amet possimus omnis ullam error, eius dolores aliquid? Eaque recusandae esse quidem. Provident labore id cupiditate ratione.</p>
</div>
<div class="hidden">
<h3>Hidden</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae veniam quisquam tenetur architecto amet possimus omnis ullam error, eius dolores aliquid? Eaque recusandae esse quidem. Provident labore id cupiditate ratione.</p>
</div>
<div class="scroll">
<h3>Scroll</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae veniam quisquam tenetur architecto amet possimus omnis ullam error, eius dolores aliquid? Eaque recusandae esse quidem. Provident labore id cupiditate ratione.</p>
</div>
<div class="auto">
<h3>Auto</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae veniam quisquam tenetur architecto amet possimus omnis ullam error, eius dolores aliquid? Eaque recusandae esse quidem. Provident labore id cupiditate ratione.</p>
</div>
</html>

7. Conclusion
position: relative;
๋ฅผ ์ง๊ธ๊น์ง๋ ๋จ์งabsolute
๊ฐ์ ์ค์ ํ๊ธฐ ์ํด ๋ถ๋ชจ์์ ์ค ์ํ๋ ์์์ ์์ฑํด์ผํ๋ ํ๋กํผํฐ๋ก ์๊ณ ์์๋ค. ํ์ง๋งrelative
๋ง์ ํน์ง์ด ์์๊ณ ๊ทธ ์ฐ์์๊ฐabsolute
์ ๊ต์ฅํ ๋น์ทํ๋ค๋ ๊ฒ์ ์์๋ค. ๋จ์ง ๋ฐ๋ก ์์ ๋ถ๋ชจ ์์์ธ์ง ์๋์ง์ ์ฐจ์ด๋ง ์๋ค. ํ์ง๋ง ์ง๊ธ๊น์ง ๋จ๋ ์ผ๋กrelative
๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๊ฒ์ ์๊ฐํ๋ฉด ํด๋น ํ๋กํผํฐ ๋์ ํ์ฌpadding
์ ์ฌ์ฉํ ์ ์์ด์ ๊ทธ๋ฐ ๊ฒ์ด์ง ์์๊น ์ถ๋ค. ๊ทธ๋ฆฌ๊ณ ์ง๊ธ๊น์ง ์ปจํ ์ธ ๋ด์ scroll๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋absolute
๋ฅผ ์ ๊ทน์ ์ผ๋ก ์ฌ์ฉํ๋๋ฐ ๊ตณ์ด ๊ทธ๋ฌ์ง ์๊ณrelative
๋ฅผ ์ฌ์ฉํด๋ ๋ ๋ฏ ํ๋ค~
์ฐธ๊ณ
poiemaweb 2-8 ์์์ ์์น ์ ์ ๋์ - HTML + CSS + ์๋ฐ์คํฌ๋ฆฝํธ ์น ํ์ค์ ์ ์
๐ 2022-07-20
Last updated