HTML CSS

html/css) 포지션, 트랜지션

조충희 2025. 6. 4. 19:56

position

위치 지정


요소의 위치를 어떻게 배치할지 설정한다.

 

static: 기본값, 문서 흐름에 따라 배치(기본 위치).
relative: 문서 흐름에 따라 배치되지만, top, left, right, bottom으로 원래 위치에서 상대적으로 이동.
absolute: 문서 흐름에서 벗어나며, 가장 가까운 position이 relative/absolute/fixed인 부모 요소를 기준으로 top, left, right, bottom으로 이동.
fixed: 문서 흐름에서 벗어나며, 뷰포트(화면)를 기준으로 top, left, right, bottom으로 이동(스크롤해도 고정됨).


주의.

relative, absolute, fixed를 사용할 때는 top, left, right, bottom 속성을 함께 설정해야 위치를 조정할 수 있다.

 

예제1)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>우주탐험</title>

    <style>
      @keyframes star {
        0% {
          transform: translate(0, 0);
          opacity: 1;
        }
        50% {
          transform: translate(-100px, 100px);
          opacity: 1;
        }
        55% {
          opacity: 0; /* 사라졌다가 */
        }
        95% {
          opacity: 0; /* 사라졌다가 */
        }
        100% {
          transform: translate(0, 0);
          opacity: 1; /* 바로 시작 위치로 돌아오며 다시 나타남 */
        }
      }
      @keyframes star1 {
        25% {
          transform: translate(0, 0);
          opacity: 1;
        }
        75% {
          transform: translate(-100px, 100px);
          opacity: 1;
        }
        80% {
          opacity: 0; /* 사라졌다가 */
        }
        100% {
          opacity: 0; /* 사라졌다가 */
        }
      }
      body {
        background-color: midnightblue;
        margin: 0;
        padding: 0;
        /* static: 기본값 */
        position: static;
      }
      .star {
        font-size: 50px;
        position: absolute;
        animation: star 3s ease-in-out infinite;
      }
            .star1 {
        font-size: 50px;
        position: absolute;
        animation: star1 3s ease-in-out infinite;
      }
      .planet {
        width: 100px;
        height: 100px;
        font-size: 150px;
        position: absolute;
        top: 100px;
        left: 100px;
      }
      .rocket {
        /* fixed: 문서의 흐름에서 벗어나 지정된다.
        스크롤을 움직여도 현재화면 기준으로 고정된다
          */
        position: fixed;
        top: 50px;
        right: 50px;
        font-size: 100px;
        z-index: 1000;
      }
    </style>
  </head>
  <body>
    <div class="star" style="top: 50px; left: 50px">🌠</div>
    <div class="star1" style="top: 50px; left: 200px">🌠</div>
    <div class="star" style="top: 200px; left: 300px">🌠</div>
    <div class="star1" style="top: 200px; left: 500px">🌠</div>
    <div class="star" style="top: 400px; left: 200px">🌠</div>
    <div class="star1" style="top: 400px; left: 400px">🌠</div>
    <div class="star" style="top: 500px; left: 100px">🌠</div>
    <div class="star1" style="top: 500px; left: 300px">🌠</div>
    <div class="star" style="top: 1600px; left: 300px">🌠</div>
    <div class="star1" style="top: 3200px; left: 100px">🌠</div>
    <div class="planet">🤔</div>
    <div class="rocket">🚀</div>
  </body>
</html>

 

예제2)

<!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 {
        background-color: rgb(0, 0, 30);
        color: greenyellow;
        padding: 40;
        font-family: "Courier New", Courier, monospace;
        position: static;
      }
      .gallery {
        width: 400px;
        height: 300px;
        border: 2px solid greenyellow;
        position: absolute;
        overflow: scroll;
      }
      .photo-card {
        width: 150px;
        height: 150px;
        border: 2px solid rgb(6, 8, 4);
        background-color: darkgreen;
        text-align: center;
        font-size: 18px;
        position: absolute;
      }
      .card1 {
        top: 20px;
        left: 20px;
        background-color: purple;
        z-index: 4;
      }
      .card2 {
        top: 50px;
        left: 100px;
        background-color: violet;
        z-index: 3;
      }
      .card3 {
        top: 80px;
        left: 180px;
        background-color: darkmagenta;
      z-index: 2;
      }
      .card4 {
        top: 1px;
        left: 1px;
        background-color: plum;
      z-index: 5;
      }
    </style>
  </head>
  <body>
    <h2>나만의 포토갤러리</h2>
    <div class="gallery">
      <div class="photo-card card1">사진1</div>
      <div class="photo-card card2">사진2</div>
      <div class="photo-card card3">사진3</div>
      <div class="photo-card card4">사진4</div>
    </div>
  </body>
</html>

 

예제3)

 

<!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>
      .box {
        width: 100px;
        height: 100px;
        top: 1px;
        left: 1px;
        background-color: tomato;
        /* 애니메이션효과: 가로크기 변경 */
        transition-property: width, height, top, left background-color;
        transition-duration: 3s;
        position: absolute;
      }
      .box:hover {
        position: absolute;
        top: 100px;
        left: 100px;
        width: 800px;
        height: 800px;
        background-color: navy;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
  </body>
</html>

 

마우스오버하면(hover) 1. 우하단으로 이동하고 2.크기가 커지고 3.색깔이 바뀌는 애니메이션 구현