[CSS] Position 속성(relative, absolute, fixed)

μ–‘μ£Όμ˜Β·2021λ…„ 11μ›” 2일
0

css

λͺ©λ‘ 보기
2/3

πŸ’‘ Position 속성

position 속성은 μ›Ή λ¬Έμ„œ μ•ˆ μš”μ†Œλ“€μ„ μ–΄λ–»κ²Œ λ°°μΉ˜ν•  지λ₯Ό μ •ν•˜λŠ” 속성이닀.
position은 크게 λ„€ κ°€μ§€μ˜ μ’…λ₯˜κ°€ μžˆλ‹€.

βœ… static

  • κΈ°λ³Έκ°’. position 속성을 μž…λ ₯ν•˜μ§€ μ•Šμ•˜μ„ λ•Œμ˜ κΈ°λ³Έκ°’

βœ… relative

  • ν•΄λ‹Ή μš”μ†Œμ˜ μ›λž˜ μœ„μΉ˜λ₯Ό κΈ°μ€€μœΌλ‘œ μœ„μΉ˜λ₯Ό μž‘μ„ λ•Œ.
    (μ—¬κΈ°μ„œ μ›λž˜ μœ„μΉ˜λž€, position:relative 속성이 없을 λ•Œμ˜ μœ„μΉ˜)

position:relative; μžμ²΄λ‘œλŠ” νŠΉλ³„ν•œ μ˜λ―Έκ°€ μ—†κ³ , λ”±νžˆ μ–΄λŠ μœ„μΉ˜λ‘œ μ΄λ™ν•˜μ§€λŠ” μ•ŠλŠ”λ‹€.
μœ„μΉ˜λ₯Ό μ΄λ™μ‹œμΌœμ£ΌλŠ” top, right, bottom, left ν”„λ‘œνΌν‹°κ°€ μžˆμ–΄μ•Ό μ›λž˜μ˜ μœ„μΉ˜μ—μ„œ 이동할 수 μžˆλ‹€.

🌟🌟 top, right, bottom, leftλŠ” positionμ΄λΌλŠ” ν”„λ‘œνΌν‹°κ°€ μžˆμ„ λ•Œλ§Œ μ μš©λ˜λŠ” ν”„λ‘œνΌν‹°μ΄λ‹€.

βœ… absolute

  • νŠΉμ • λΆ€λͺ¨μ— λŒ€ν•œ κΈ°μ€€μœΌλ‘œ μ ˆλŒ€μ μΈ μœ„μΉ˜(κ²ΉμΉ¨ κ°€λŠ₯)에 λ‘˜ 수 μžˆλŠ” 속성값
    적용된 μš”μ†ŒλŠ” 아무도 μ•Œμ•„λ³΄μ§€ λͺ»ν•œλ‹€(λΆ•λœ¬λ‹€)

μ—¬κΈ°μ—μ„œ λΆ€λͺ¨μ˜ 쑰건은 position이 κΈ°λ³Έκ°’(static)이 μ•„λ‹Œ relative, fixed, absolute ν•˜λ‚˜λΌλ„ μžˆμ–΄μ•Ό ν•œλ‹€.
absoluteλŠ” λΈŒλΌμš°μ €κ°€ λ¬Έμ„œμ˜ 흐름과 상관없이 left, right, top, bottom 속성값을 μ΄μš©ν•˜μ—¬ μš”μ†Œλ₯Ό μœ„μΉ˜μ‹œν‚€λŠ” 속성값이닀.


μ•„λž˜ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•˜μ—¬ μ œλŒ€λ‘œ μ΄ν•΄ν•΄λ³΄μž.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <style>
      * {
          box-sizing: border-box;
        }

      .input-box {
        width: 300px;
        position: relative;
      }

      input {
        width: 100%;
        border: 1px solid #bbb;
        border-radius: 8px;
        padding: 10px 12px;
        font-size: 14px;
      }

      img {
        position: absolute;
        width: 17px;
        margin: 0;
      }
    </style>
  </head>
  <body>
    <div class="input-box">
      <input type="text" placeholder="검색어 μž…λ ₯"/>
      <img src="https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/icon/search.png" alt="돋보기"/>
    </div>
  </body>
</html>

기쀀이 λ˜λŠ” input-box에 relativeλ₯Ό 쀬고,
μ ˆλŒ€μ μΈ μœ„μΉ˜μ— μžˆμ–΄μ•Ό ν•˜λŠ”( = 겹쳐야 ν•˜λŠ” ) 값에 absoluteλ₯Ό μ€¬λ”λ‹ˆ input-boxλ‚΄λΆ€κ°€ μ•„λ‹ˆλΌ μœ„μ™€ 같이 λ°”κΉ₯μͺ½μ— μœ„μΉ˜ν•œλ‹€.
μ—¬κΈ°μ—μ„œ μš°λ¦¬κ°€ μ•Œ 수 μžˆλŠ” 것은 κΌ­ left, right, top, bottom 속성값을 μ΄μš©ν•΄μ•Ό μ›ν•˜λŠ” 곳에 λ°°μΉ˜ν•  수 μžˆλ‹€λŠ” 점이닀.
μ•„λž˜ μ˜ˆμ‹œλ₯Ό 보자.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>position:absolute;</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <style>
      * {
          box-sizing: border-box;
        }

      .input-box {
        width: 300px;
        position: relative;
      }

      input {
        width: 100%;
        border: 1px solid #bbb;
        border-radius: 8px;
        padding: 10px 12px;
        font-size: 14px;
      }

      img {
        position: absolute;
        width: 17px;
        top: 10px;
        right: 12px;
        margin: 0;
      }
    </style>
  </head>
  <body>
    <div class="input-box">
      <input type="text" placeholder="검색어 μž…λ ₯"/>
      <img src="https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/icon/search.png" alt="돋보기"/>
    </div>
  </body>
</html>

img에 top:10px, right:12px을 μ£Όλ‹ˆκΉŒ 기쀀점(input-box)μ—μ„œ top만큼 10px, 였λ₯Έμͺ½μœΌλ‘œλΆ€ν„° 12px만큼 μ΄λ™ν•œ 것을 λ³Ό 수 μžˆλ‹€.

βœ… fixed

  • 무쑰건 화면을 κΈ°μ€€μœΌλ‘œ μœ„μΉ˜λ₯Ό μž‘μ„ λ•Œ(κ³ μ •μ‹œν‚΄)

fixedλŠ” absolute 속성값과 λ§ˆμ°¬κ°€μ§€λ‘œ λ¬Έμ„œμ˜ 흐름과 상관없이 μœ„μΉ˜λ₯Ό κ²°μ •ν•œλ‹€.
ν•˜μ§€λ§Œ position:relative인 제일 κ°€κΉŒμš΄ λΆ€λͺ¨ ν˜Ήμ€ 쑰상 μš”μ†Œκ°€ μ•„λ‹Œ λΈŒλΌμš°μ € 창이 기쀀이 λœλ‹€.
λ”°λΌμ„œ λΈŒλΌμš°μ € 창을 μ–΄λ””λ‘œ 슀크둀 ν•˜λ”λΌλ„ 계속 κ³ μ •λ˜μ–΄ ν‘œμ‹œλ˜κ²Œ λœλ‹€.
주둜 λ‚΄λΉ„κ²Œμ΄μ…˜ λ°”λ₯Ό κ³ μ •μ‹œν‚€λŠ” μš©λ„λ‘œ μ‚¬μš©ν•œλ‹€.

기쀀점 : λΈŒλΌμš°μ € μ™Όμͺ½ μœ„ 꼭짓점


μ•„λž˜ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•΄λ³΄μž.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <style>
      body {
        background-color: #eee;
        height: 800px;
        padding-top: 48px;
      }

      header {
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
        height: 48px;
        background-color: rgba(45, 45, 45, 0.95);
      }

      header > img {
        position: absolute;
        left: 50%;
        margin-left: -10px;
      }

    </style>
  </head>
  <body>
    <header>
      <img  width="20px" height="48px" src="https://www.apple.com/ac/globalnav/4/en_US/images/globalnav/apple/image_small.svg" alt="μ• ν”Œλ‘œκ³ "/>
    </header>
  </body>
</html>


μœ„μ˜ absolute와 λ™μΌν•˜κ²Œ, fixed의 속성을 κ°€μ§€λŠ” μš”μ†Œμ—λ„ left: 0; right: 0; top: 0; 속성값을 μ΄μš©ν•˜μ—¬ μœ„μΉ˜μ‹œμΌœμ•Ό ν•œλ‹€.



μ°Έμ‘° >
https://developer.mozilla.org/ko/docs/Web/CSS/position
https://www.youtube.com/watch?v=_uYY1oB8yec

profile
λšœλ²…λšœλ²…

0개의 λŒ“κΈ€