Position

강기호·2023년 2월 21일
0

HTML/CSS

목록 보기
2/4

position : fixed

화면을 스크롤 해도 위치가 변경되지 않고 고정됨.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 1000vh;
        }
        div { 
            width: 200px;
            height: 200px;
        }
        #first {
            background-color: red;
        }
        #second {
            position: fixed;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div id = "first"></div>
    <div id = "second"></div>

</body>
</html>

id = first인 box 바로 아래에 id = second인 box가 생기고(서로 같은 레이어에 있어서 겹치는 현상이 없음) , 스크롤을 내리면 second box의 위치는 고정

하지만 second에 top , left , bottom , right 등의 옵션을 주게 되면 위치가 무시되고 두 박스가 겹치게 됨.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 1000vh;
        }
        div { 
            width: 200px;
            height: 200px;
        }
        #first {
            background-color: red;
        }
        #second {
            top : 50px;
            position: fixed;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div id = "first"></div>
    <div id = "second"></div>

</body>
</html>

position : relative

  • element가 처음 놓인 자리에서 상하좌우 움직일 수 있음(초기 위치에서 상대적으로 움직임)
  • position : relative; 옵션을 주면 top, bottom , left , right 옵션을 주어서 자신의 초기 위치를 기준으로 상대적으로 위치 변경 가능
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            width: 100vh;
            height: 100vh;
        }
        div {
            width: 100px;
            height: 100px;
            background-color: red;
        }
        #child {
            position: relative;
            top : -10px;
            
            width: 40px;
            height: 40px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div>
        <div id = "child"></div>
    </div>
</body>
</html>

position : absolute

  • 가장 가까운 relative한 부모를 기준으로 절대좌표가 되어 이동시킬 수 있다.
  • 지금 예시에서 id = "child"의 부모는 div이지만 div가 현재 relative가 되지 않아서 그 다음 부모인 body가 기준으로 움직이게 된다. 이때 id = "child"의 부모인 div의 position : relative로 변경하게 되면 00의 부모를 기준으로 절대좌표로 하여 움직이게 된다.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            width: 100vh;
            height: 100vh;
        }
        div {
            width: 100px;
            height: 100px;
            background-color: red;
        }
        #child {
            position: absolute;
            top : 0px;
            right: 0px;
            
            width: 40px;
            height: 40px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div>
        <div id = "child"></div>
    </div>
</body>
</html>

id ="child"의 부모의 position : relative 옵션을 준 경우

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            width: 100vh;
            height: 100vh;
        }
        div {
            position: relative;
            width: 100px;
            height: 100px;
            background-color: red;
        }
        #child {
            position: absolute;
            top : 0px;
            right: 0px;
            
            width: 40px;
            height: 40px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div>
        <div id = "child"></div>
    </div>
</body>
</html>

position : static

  • position의 default 값

0개의 댓글