css기초이론 - css포지셔닝 (position: relative, absolute)

HTML, CSS 2021. 1. 15. 01:36
반응형

relative : 부모 요소

absolute : 자식요소

 

자식은 부모요소 안에서 포지셔닝 됨

 

<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Relative(부모요소), Absolute(자식요소)</title>
    <style>
      body {
        margin: 0;
      }

      .parent {
        width: 400px;
        height: 400px;
        border: 1px solid crimson;
        position: relative;
      }
      .child {
        width: 50px;
        height: 50px;
        background-color: dodgerblue;
        position: absolute;
        right: 0;
        bottom: 0;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="child"></div>
    </div>
  </body>
</html>

<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Relative(부모요소), Absolute(자식요소)</title>
    <style>
      body {
        margin: 0;
      }

      .parent {
        width: 400px;
        height: 400px;
        /* border: 1px solid crimson; */
        position: relative;
        background-color: crimson;

        margin: 100px;
      }
      .child {
        width: 100px;
        height: 100px;
        background-color: dodgerblue;
        position: absolute;
        /* bottom: -50px;
        left: 150px; */
        left: 50%;
        bottom: -50px;
        /* 상대적임  */
        /* 부모의 사이즈가 변해도 함께 이동 */
        transform: translateX(-50%);
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="child"></div>
    </div>
  </body>
</html>

www.youtube.com/watch?v=jx5jmI0UlXU

 

반응형
: