별초롱언니 2025. 3. 14. 17:32

position-relative

자기자신을 기준으로 이동하여 배치된다. 다른요소의 위치에는 영향을 미치지 않는다.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>relative</title>
  <style>
    .box {
      width: 100px;
      height: 100px;
      border: 1px dashed red;
      background-color: lightblue;
      margin: 10px;
    }
    .relative {
      position: relative;
      top: 20px;
      left: 50px;
    }
  </style>
</head>
<body>
  <div class="box">1</div>
  <div class="box relative">2</div>
  <div class="box">3</div>
</body>
</html>

position-absolute

부모요소(위치상의)를 기준으로 이동하여 배치된다.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>absoulte</title>
  <style>
    .grand-parent {
      width: 400px;
      height: 300px;
      border: 4px dashed red;
      padding: 30px 100px 100px 30px;
    }
    .parent {
      width: 400px;
      height: 80px;
      border: 4px dashed blue;
    }
    .child {
      width: 120px;
      height: 80px;
      border: 4px dashed yellow;
      background-color: tomato;
    }
    .absoulte {
      position: absolute;
      top: 80px;
      left: 100px;
    }
  </style>
</head>
<body>
  <div class="grand-parent">
    <div class="parent">
      <div class="child">1</div>
      <div class=" child absoulte">2</div>
      <div class="child">3</div>
    </div>
  </div>
</body>
</html>


position-fixed

브라우저를 기준으로 배치가 되며 위치에 고정적으로 위치한다. 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      height: 4000px;
    }
    .grand-parent {
      width: 400px;
      height: 300px;
      padding: 30px 100px 100px 30px;
      border: 1px dashed red;
    }
    .parent {
      width: 400px;
      height: 300px;
      border: 4px dashed blue;
      position: relative;
    }
    .child {
      width: 120px;
      height: 80px;
      border: 4px dashed yellow;
      background-color: tomato;
    }
    .fixed {
      position: fixed;
      top: 80px;
      right: 100px;
    }
  </style>
</head>
<body>
  <div class="grand-parent">
    <div class="parent">
      <div class="child">1</div>
      <div class="child fixed">2</div>
      <div class="child">3</div>
    </div>
  </div>
</body>
</html>

 


position-sticky

창의  스크롤 영역 기준으로 배치 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .section {
      height: 200px;
      border: 4px dashed lightgray;
    }
    .section h1 {
      text-align: center;
      line-height: 2;
      font-size: 24px;
      font-weight: bold;
      position: sticky;
      top: 0;
    }
  </style>
</head>
<body>
  <div class="section">
    <h1>Title1</h1>
  </div>
  <div class="section">
    <h1>Title2</h1>
  </div>
  <div class="section">
    <h1>Title3</h1>
  </div>
  <div class="section">
    <h1>Title4</h1>
  </div>
  <div class="section">
    <h1>Title5</h1>
  </div>
  <div class="section">
    <h1>Title6</h1>
  </div>
  <div class="section">
    <h1>Title7</h1>
  </div>
</body>
</html>