[HTML/CSS] 9. CSS Position

최재원's avatar
Mar 11, 2025
[HTML/CSS] 9. CSS Position

1. static

notion image
  • position : static 기본값
  • 요소의 흐름을 따른다
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Document</title> <style> .box1 { width: 300px; height: 300px; background-color: yellow; display: inline-block; } .box2 { width: 300px; height: 300px; background-color: red; display: inline-block; position: static; top: 100px; left: 100px; } .box3 { width: 300px; height: 300px; background-color: green; display: inline-block; } .box4 { width: 300px; height: 300px; background-color: blue; display: inline-block; } .box5 { width: 300px; height: 300px; background-color: bisque; display: inline-block; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <div class="box5"></div> </body> </html>

2. relative

notion image
  • 요소의 흐름을 따른다
  • 자신의 흐름을 기준으로 위치 이동이 가능하다
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Document</title> <style> .box1 { width: 300px; height: 300px; background-color: yellow; display: inline-block; } .box2 { width: 300px; height: 300px; background-color: red; display: inline-block; position: relative; top: 100px; left: 100px; } .box3 { width: 300px; height: 300px; background-color: green; display: inline-block; } .box4 { width: 300px; height: 300px; background-color: blue; display: inline-block; } .box5 { width: 300px; height: 300px; background-color: bisque; display: inline-block; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <div class="box5"></div> </body> </html>

3. absolute

notion image
  • 요소의 흐름에서 벗어난다
  • 부모를 기준으로 이동한다
  • 자신의 위치 기준 상위의 요소 중에서 relative, absolute 등 위치 지정이 있는 부모가 없으면 body를 기준으로 한다
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Document</title> <style> .box1 { width: 300px; height: 300px; background-color: yellow; display: inline-block; } .box2 { width: 300px; height: 300px; background-color: red; display: inline-block; position: absolute; top: 100px; left: 100px; } .box3 { width: 300px; height: 300px; background-color: green; display: inline-block; } .box4 { width: 300px; height: 300px; background-color: blue; display: inline-block; } .box5 { width: 300px; height: 300px; background-color: bisque; display: inline-block; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <div class="box5"></div> </body> </html>

absolute를 사용해 특정 요소 기준으로 움직이고 싶을 때

notion image
notion image
notion image
<div class="box4"> <div class="box5"></div> </div>
  • box5가 box4를 기준으로 움직이고 싶다
.box4 { width: 300px; height: 300px; background-color: blue; display: inline-block; position: relative; } .box5 { width: 50px; height: 50px; background-color: bisque; display: inline-block; position: absolute; left: 50px; bottom: 50px; }
  • box5를 absolute
  • box4를 relative

4. fixed

notion image
notion image
  • 요소의 흐름을 벗어남
  • 화면을 기준으로 위치 이동
  • 화면에 고정 되어 있음
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Document</title> <style> .box1 { width: 300px; height: 300px; background-color: yellow; display: inline-block; } .box2 { width: 300px; height: 300px; background-color: red; display: inline-block; } .box3 { width: 300px; height: 300px; background-color: green; display: inline-block; position: fixed; top: 200px; } .box4 { width: 300px; height: 300px; background-color: blue; display: inline-block; } .box5 { width: 300px; height: 300px; background-color: bisque; display: inline-block; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> <div class="box5"></div> </body> </html>
 
Share article

jjack1