2025. 2. 15. 13:29ใStyle/CSS

position
CSS์์ position ์์ฑ์ ์์๋ฅผ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ฐฐ์นํ ์ง๋ฅผ ๊ฒฐ์ ํ๋ค.
- ๊ธฐ๋ณธ๊ฐ : static
- ๋ณ๊ฒฝ๊ฐ : relative, absolute, fixed
position์ผ๋ก ์์ฑ ์์๋ฅผ ์์น์ํค๋ ๋ฐฉ๋ฒ์ ์ง์ ํ๊ณ ,
ํฌ์ง์ ๋ ๊ด๋ จ ์์ฑ์ธ top, left, bottom, right ๋ฑ์ ์ฌ์ฉํ์ฌ ์์์ offset์ ์ง์ ํ ์ ์๋ค.
absolute์ ํน์ง
- ๋ถ๋ชจ ์์์ ์๋ฐ๋์ง ์์
- ์์๋ฅผ ํ๋ฉด์ ์ํ๋ ์์น์ ์์ ๋กญ๊ฒ ๋ฐฐ์น์ํฌ ์ ์์
- ์์ ์์๊ฐ relative๋ผ๋ฉด, ํด๋น ์์์ ๋ด๋ถ์์ ๋์ํจ
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="ex.css" type="text/css" />
</head>
<body>
<div class="parent">
parent
<div class="child">child 1</div>
<div class="child">child 2</div>
<div class="child_abs">child 3</div>
</div>
</body>
</html>
.parent{
border: 2px solid blue;
color: blue;
background-color: lightblue;
padding:1rem;
}
.child{
border: 2px solid red;
color: red;
background-color: lightcoral;
padding: 1rem;
}
.child_abs{
position: absolute;
border: 2px solid green;
color: green;
background-color: lightgreen;
padding: 2rem;
}

absolute ์์ฑ์ด ์ ์ฉ๋์ง ์์ child ํด๋์ค๋ฅผ ๊ฐ์ง child1, 2๋ ๋ถ๋ชจ ์์์ ์ข ์๋์ง๋ง,
absolute ์์ฑ์ ๊ฐ์ง child_abs๋ฅผ ๊ฐ์ง child 3๋ ๋ถ๋ชจ ์์์ ์ข ์๋์ง ์์ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด ๋ child 3๊ฐ ํ๋ฉด์ ๋ํด์๊ฐ ์๋ parent์ ๋ํด ์ ๋ ฌ๋ ์ด์ ๋
offset ์์ฑ์ ๋ช ์ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ์ธ auto๊ฐ ์ ์ฉ๋๋๋ฐ,
์ด offset auto๊ฐ์ด position ์์ฑ์ด static์ผ ๋์ ์์น์ ์ผ์นํ๊ธฐ ๋๋ฌธ์ด๋ค.
child_abs์ top๊ณผ left๋ฅผ 0, 0์ผ๋ก offset์ ์ง์ ํ๋ฉด ๋ธ๋ผ์ฐ์ ํ๋ฉด์ ๋ํด 0, 0 ์ง์ ์ ์์นํ๋ค.


๋ํ parent์ relative ๊ฐ์ ์ง์ ํ๋ฉด child_abs์ positioning context๊ฐ ์ ์ฒดํ๋ฉด(viewport)์์ ๋ถ๋ชจ ์์๋ก ๋ณ๊ฒฝ๋์ด
์์น๊ฐ ์ง์ ๋จ์ ํ์ธํ ์ ์๋ค.


'Style > CSS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| CSS : display์ float (0) | 2025.07.28 |
|---|---|
| CSS : ๋ฐ์ค ๋ชจ๋ธ (Box Model) (1) | 2025.07.28 |
| CSS : ํ ์คํธ ์คํ์ผ๋ง ์์ฑ๋ค (3) | 2025.07.28 |
| CSS ๋จ์ ์ด์ ๋ฆฌ : ์์, ํฌ๊ธฐ, ์๊ฐ, ๊ฐ๋ (5) | 2025.07.28 |
| CSS 1 : CSS ๊ธฐ๋ณธ ๊ฐ๋ (1) | 2025.01.23 |
GitHub