[HTML/CSS]10. CSS Position

손영민's avatar
Mar 10, 2025
[HTML/CSS]10. CSS Position
1.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; } .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>
notion image
2.relative
<!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>
notion image
3.absolute
 
<!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: 0px; } .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>
notion image
 
4.박스 안에서 박스를 돌게 하고싶으면
  1. 돌게한 박스안에 박스를 넣는다
  1. 부모박스에 relative를 준다
  1. 자식박스에 absolute를 준다
  1. 위치를 지정한다.
 
<!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; } .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: 100px; bottom: 100px; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"> <div class="box5"></div> </div> </body> </html>
 
5.fixed
<!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: 300px; } .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> <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> <div class="box5"></div> <div class="box5"></div> </body> </html>
 
예제
<!DOCTYPE html> <html lang="en"> <head> <style> .search-box { display: flex; align-items: center; border: 2px solid #ccc; border-radius: 5px; padding: 8px; width: 300px; position: relative; } .search-box input { border: none; outline: none; font-size: 16px; width: 100%; } .box1 { width: 20px; height: 20px; background-color: red; display: inline-block; position: absolute; top: 10px; left: 285px; } </style> </head> <body> <div class="search-box"> <input type="text" placeholder="Search..." /> <div class="box1"></div> </div> </body> </html>
 
Share article

sson17