FE/HTML,CSS

[css] position

mandelina 2022. 4. 11. 16:24

๐Ÿš€position ?

์›นํŽ˜์ด์ง€์—์„œ ๋งŒ๋“ค์—ˆ๋˜ htmlํƒœ๊ทธ , id , class ๋ฐ•์Šค ๋“ฑ์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ์†์„ฑ์ด๋‹ค.

 

1) absolute / static / absolute

- static : ๊ธฐ๋ณธ๊ฐ’ , htmlํƒœ๊ทธ ์ˆœ์œผ๋กœ ์œ„์น˜๊ฐ€ ์ง€์ •๋œ๋‹ค.

 (๊ตณ์ด ์“ธ ํ•„์š”๋Š” ์—†์ง€๋งŒ , ๋ถ€๋ชจ ๊ฐ์ฒด์—์„œ ๋‹ค๋ฅธ position ์†์„ฑ๊ฐ’์ด ์ฃผ์–ด์กŒ์„๋•Œ ๋ฌด์‹œํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•จ.)

 

 

box1 , box2 , box3๋ฅผ static์œผ๋กœ ๋†“์•˜์„ ๊ฒฝ์šฐ

 

 

-absolute : ๊ธฐ์ค€์ ์ด html์œ„์น˜์— ์žˆ๋‹ค.

( ์™ผ์ชฝ ์ œ์ผ ์ƒ๋‹จ์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋ฉฐ ์›€์ง์ธ๋‹ค.)

 

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>relative-absolute</title>
  <style>

  .box1{
    position:relative;
    top:40px;
    background-color: green;
    color:white;
    width: 100px;
    height: 100px;
  }
  .box2{
    position:absolute;
    top: 40px;
    background-color: red;
    color:white;
    width: 100px;
    height: 100px;
  }
  .box3{
    position: absolute;
	top: 30px;
    left: 30px;
    background-color: blue;
    color:white;
    width: 100px;
    height: 100px;
  }
  </style>
</head>
<body>
  <div class="box3">box3</div>
  <div class="box1">box1
    <div class="box2">
      box2
    </div>

  </div>
</body>
</html>

 

box2๋Š” absolute๋กœ ์ฃผ์–ด์กŒ๊ธฐ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฐ•์Šค๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ์ ค ์œ„์ชฝ ์ƒ๋‹จ์„ ๊ธฐ์ค€์œผ๋กœ top : 40px; ๋งŒํผ ๊ฐ„ ๊ฒƒ์„ ์•Œ ์ˆ˜์žˆ๊ณ , box3 ์—ญ์‹œ ์™ผ์ชฝ ์ƒ๋‹จ์„ ๊ธฐ์ค€์œผ๋กœ top:30px, left:30px ๋งŒํผ ๊ฐ„ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

- relative : ์›๋ž˜ ์ž์‹ ์ด ์žˆ์–ด์•ผ ํ•  ์œ„์น˜๋กœ ๋ถ€ํ„ฐ ์ƒ๋Œ€์ ์œผ๋กœ ์œ„์น˜

 

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <title>relative</title>
  <style>
  .box1{
    position: static;
    background-color: green;
    color: white;
    width: 100px;
    height: 100px;
  }
  .box2{
    position:relative;
    left: 40px;
    background-color: red;
    color:white;
    width: 100px;
    height: 100px;
  }
  </style>
</head>
<body>
  <div class="box1">box1</div>
  <div class="box2">box2</div>
</body>
</html>

 

 

box2๋Š” ์›๋ž˜ box1๋ฐ‘์— ์ž๋ฆฌ (์›๋ž˜์žˆ์–ด์•ผ ํ•  ์œ„์น˜)๋กœ ๋ถ€ํ„ฐ  left:40px ๋งŒํผ ์ด๋™ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜์žˆ๋‹ค. 

 

 

 


์ •๋ฆฌ

static : position์„ ์ฃผ์ง€ ์•Š์•˜์„๋•Œ ๋””ํดํŠธ ๊ฐ’

relative : nomal flow๋ฅผ ๋”ฐ๋ฅผ ๋•Œ ์›๋ž˜ ์žˆ์–ด์•ผํ•˜๋Š” ์œ„์น˜์—์„œ ์ƒ๋Œ€์ ์œผ๋กœ ์–ผ๋งˆ๋‚˜ ์›€์ง์ด๋Š๋ƒ๋ผ๋Š” ๊ธฐ์ค€

absolute : ์ž๊ธฐ ๋ถ€๋ชจ๋“ค ์ค‘ static์ด ์•„๋‹Œ ํƒœ๊ทธ์˜ ์œ„์น˜ ๊ธฐ์ค€

 

 

์ด ๊ธ€์„ ํฌ์ŠคํŒ…ํ•œ ์ด์œ ๋Š” ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์™œ ์“ฐ๋Š”๊ฐ€์— ๋Œ€ํ•œ ๊ถ๊ธˆ์ฆ ๋•Œ๋ฌธ์ด์˜€๋‹ค.

  position: absolute;
  top: 0;
  width: 100%;

๋กœ๊ณ ๊ฐ™์ด ๋งจ ์œ„์— ์œ„์น˜๋˜์–ด์•ผํ•  ํƒœ๊ทธ์— position์„ absolute๋กœ ๋‘๊ณ  top:0; ๋ฅผ ์ฃผ๋ฉด 

์ด ๊ฐ’์€ html ์ œ์ผ ์™ผ์ชฝ ์ƒ๋‹จ์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค.

 

 


 

2) fixed / sticky

 

- fixed

์Šคํฌ๋กค์„ ์˜ฌ๋ฆฌ๊ฑฐ๋‚˜ ๋‚ด๋ฆด๋•Œ ํŠน์ • ๋ฐ•์Šค๊ฐ€ ๊ณ ์ •๋˜์–ด ์›€์ง์ด์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ์†์„ฑ

absolute๊ฐ€ position์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ถ€๋ชจ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์œ„์น˜๋ฅผ ์ •ํ•จ.

ex) ๋„ค์ด๋ฒ„ ๊ฒ€์ƒ‰์ฐฝ , ๋‹คํฌ๋ชจ๋“œ๋ฒ„ํŠผ

 

See the Pen Untitled by mandelina (@mandelina) on CodePen.

 

-sticky

๋ฌธ์„œ์˜ ํ๋ฆ„์— ๋”ฐ๋ผ ๋ฐฐ์น˜๋˜๋ฉฐ ์ž์‹ ์˜ ์ƒ์œ„์š”์†Œ์— ๋ถ™๊ฒŒ ๋œ๋‹ค.

ex) ์›น ๊ด‘๊ณ ๋ฐฐ๋„ˆ

 

See the Pen Untitled by mandelina (@mandelina) on CodePen.

(0.25๋กœ ๋ณด์…”์•ผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

 

 

์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ ?

 

- fixed๋Š” ๋ฌธ์žฅ์˜ ํ๋ฆ„๊ณผ ์ƒ๊ด€์—†์ด ํ•ญ์ƒ ์ž๊ธฐ์ž๋ฆฌ ์— ๊ณ ์ •์ด ๋˜์–ด์žˆ๋‹ค๋ฉด,

- sticky๋Š” ๋ฌธ์„œ์˜ ํ๋ฆ„์— ๋”ฐ๋ผ ๋ฐฐ์น˜ ๋˜๋ฉฐ ์ƒ์œ„์š”์†Œ, ํ•˜์œ„์š”์†Œ์— ๊ฐ€๋ฉด ๋”ฑ ๋‹ฌ๋ผ๋ถ™๋Š”๋‹ค.

 

 


๐ŸŒˆ๊ทธ๋ฐ–์—

- z-index์˜ ๊ฒฝ์šฐ ๋ณดํ†ต 10,100,1000 ๋‹จ์œ„ ๋“ฑ์œผ๋กœ ํฌ๊ฒŒํฌ๊ฒŒ ์“ด๋‹ค. (ํšŒ์‚ฌ ์ปจ๋ฒค์…˜์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค.)

- ๋ฏธ๋ฆฌ๋ฏธ๋ฆฌ ์ด๋ ฅ์„œ๋ฅผ ์“ฐ์ž!

 

 

๐Ÿ”ฅ ํšŒ๊ณ 

์‹œ์ž‘ํ•œ์ง€ ๋ณ„๋กœ ์•ˆ๋œ๊ฒƒ ๊ฐ™์€๋ฐ ๋ฒŒ์จ 3์ฃผ์ฐจ๋กœ ๋„˜์–ด๊ฐ€๊ณ  ์žˆ๋‹ค.

css๋ฅผ ๋“ค์–ด์˜ค๋ฉฐ ํ™•์‹คํžˆ ์ดํ•ดํ•˜์ง€์•Š์œผ๋ฉด ํ—ท๊ฐˆ๋ฆด๋งŒํ•œ ๊ฐœ๋…๋“ค์ด ๋‚˜์˜จ๋‹ค. (ir๊ธฐ๋ฒ•์ด๋ผ๋˜์ง€ , ์„ ํƒ์ž ์‹ฌํ™”๋ถ€๋ถ„์ด๋ผ๋˜์ง€..)

 

์ผ๋‹จ ๊ธ‰ํ•œ ๊ณผ์ œ๋“ค์„ ๋งˆ๋ฌด๋ฆฌ ํ•˜๊ณ , ir๊ธฐ๋ฒ•๊ณผ ๊ฐ€์ƒ์„ ํƒ์ž๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค.

๋˜ํ•œ ์ ์  ๊ณต๋ถ€ํ•  ์–‘์ด ๋งŽ์•„์ง€๋Š”๋ฐ ๋‚˜ํƒœํ•ด์ง€์ง€ ๋ง๊ณ  ์ด ํŽ˜์ด์Šค ๊ทธ๋Œ€๋กœ ์ด์–ด๊ฐ€์ž!

์›”์š”์ผ ํŒŒ์ดํŒ… ๐Ÿ‘Š