๐Ÿ‘“ TIL) CSS์˜ Position๊ณผ Display

Solmiiยท2020๋…„ 4์›” 19์ผ
1

CSS

๋ชฉ๋ก ๋ณด๊ธฐ
4/7
post-thumbnail

๐Ÿ‘ Display์˜ ๊ฐœ๋…

์˜ˆ์‹œ๋กœ ์•Œ์•„๋ณด๋Š” Display!

โ˜๏ธ์šฐ์„  html์—์„œ ๊ฐ๊ฐ div,span tag๋ฅผ 3๊ฐœ์”ฉ ๋งŒ๋“ ๋‹น

<!-- Block-level -->
  <div>1</div>
  <div>2</div>
  <div>3</div>
  
<!-- Inline level -->
  <span>1</span>
  <span>2</span>
  <span>3</span>

โœŒ๏ธ๊ทธ๋ฆฌ๊ณ  ์ด tag๋“ค์€ ๋ˆˆ์— ๋ณด์ด์ง€ ์•Š๋Š” box ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ๋ถ„์„ ์œ„ํ•ด CSS์—์„œ ๋ฐฐ๊ฒฝ์ƒ‰์„ ์ง€์ •ํ•ด์ค€๋‹ค.

div, span {
  width: 80px;
  height: 80px;
  margin: 20px;
  background: pink;
}

div {
  background: red;
}

span {
  background: blue;
}

none display๐Ÿคท๐Ÿปโ€โ™€๏ธ ๋˜์ž‰? ๋ถ„๋ช…ํžˆ span์—๋„ width, height ๊ฐ’์„ ์ง€์ •ํ–ˆ๋Š”๋ฐ ์š”๋กœ์ฝ”๋กฌ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค!
๐Ÿ’๐Ÿปโ€โ™€๏ธ ์™œ๋ƒํ•˜๋ฉด display:inline ์ž์ฒด๊ฐ€, content์˜ ํฌ๊ธฐ ๋งŒํผ๋งŒ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” tag์ด๊ธฐ ๋•Œ๋ฌธ!
์ด๊ฑด ๋งˆ์น˜, "์‹ฌํ”Œํ•œ๋ฐ ํ™”๋ คํ•œ ๋””์ž์ธ ํ•˜๋‚˜ ๋ฝ‘์•„์ฃผ์„ธ์š”. ๊ธ‰ํ•œ๊ฑฐ๊ธด ํ•œ๋ฐ ์ฒœ์ฒœํžˆ ํ•ด์š”." ๊ฐ™์€ ๋Š๋‚Œ๐Ÿ˜ฑ


๐Ÿ‘ display๋ฅผ inline์œผ๋กœ ์„ค์ • ์‹œ width, height, ์ƒํ•˜ margin, ์ƒํ•˜ padding, float ์„ค์ •์ด ๋จนํžˆ์ง€ ์•Š๋Š”๋‹ค.
(์ขŒ์šฐ margin,padding์€ ๋จน์Œ)


๐ŸคŸ ์ด๋ฒˆ์—” ๋“œ๋””์–ด display ์†์„ฑ์„ ์ง€์ •ํ•ด๋ณธ๋‹น

div, span {
  width: 80px;
  height: 80px;
  margin: 20px;
  background: pink;
}

div {
  background: red;
  display: inline-block;
}

span {
  background: blue;
  display: block;
}

display2๐Ÿ’๐Ÿปโ€โ™€๏ธ์šฐ์™•~ block ์†์„ฑ์ธ div๋Š” ํ•œ์ค„์— ์—ฌ๋Ÿฌ๊ฐœ ์ง„์—ด๋˜๊ณ , inline ์†์„ฑ์€ span์€ ํ•œ์ค„์— ํ•œ ๊ฐœ๋งŒ ์ง„์—ด๋˜์—ˆ๋‹ค!


๐Ÿ–– div์— display:inline-block;๋Œ€์‹  display:inline;์„ ์ ์šฉํ•ด๋ด๋„ ์žฌ๋ฐŒ๋‹ค!display

div๋„ width๋ž‘ height๋ฅผ ์ง€์ •ํ–ˆ๋Š”๋ฐ, inline์†์„ฑ์„ ์ฃผ๋‹ˆ๊นŒ โ˜๏ธ์ฒ˜๋Ÿผ ๋˜๋ฒ„๋ฆฐ๋‹ค!

๐Ÿ‘ฉ๐Ÿปโ€โš–๏ธ ์ •๋ฆฌํ•˜์ž๋ฉด!

inlineinline-blockblock
ํ•œ ์ค„์— ์—ฌ๋Ÿฌ๊ฐœ ์ง„์—ด ๊ฐ€๋Šฅํ•œ Itemํ•œ ์ค„์— ์—ฌ๋Ÿฌ๊ฐœ ์ง„์—ด ๊ฐ€๋Šฅํ•œ Boxํ•œ ์ค„์— ํ•œ ๊ฐœ๋งŒ ์ง„์—ด ๊ฐ€๋Šฅํ•œ Box
์ „ํ›„ ์ค„๋ฐ”๊ฟˆ ์—†์ด ๋‚˜๋ž€ํžˆ ๋ฐฐ์น˜์ „ํ›„ ์ค„๋ฐ”๊ฟˆ ์—†์ด ๋‚˜๋ž€ํžˆ ๋ฐฐ์น˜์ „ํ›„ ์ค„๋ฐ”๊ฟˆ์ด ๋“ค์–ด๊ฐ€์„œ ํ˜ผ์ž์„œ ํ•œ ์ค„ ์ฐจ์ง€
width, height, ์ƒํ•˜ margin,
์ƒํ•˜ padding ์ ์šฉ ์•ˆ๋จ
width, height, margin, padding ์ž˜ ์ ์šฉ๋จwidth, height, margin, padding ์ž˜ ์ ์šฉ๋จ
๋Œ€ํ‘œ ํƒœ๊ทธ : span, a, em๋Œ€ํ‘œ ํƒœ๊ทธ : button, select๋Œ€ํ‘œ ํƒœ๊ทธ : div, p, h1


๐Ÿ—บ Position์˜ ๊ฐœ๋…

์˜ˆ์‹œ๋กœ ์•Œ์•„๋ณด๋Š” Position!

โ˜๏ธ ์šฐ์„  html์—์„œ ์—ฌ๋Ÿฌ๊ฐœ์˜ div box๋“ค์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

<body>
    <article class="container">
       <div></div>
       <div class="box">I'm Box</div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
       <div></div>
   </article>
</body>

โœŒ๏ธ ๊ตฌ๋ถ„์„ ์œ„ํ•ด CSS๋กœ ๋ฐฐ๊ฒฝ์ƒ‰์„ ๋„ฃ์–ด์ค€๋‹ค.

div {
  width: 50px;
  height: 50px;
  margin-bottom: 20px;
  background: red;
}

.container {   =>Box, ๋ถ€๋ชจ ์š”์†Œ
  background: yellow;
}

.box {   =>Item, ์ž์‹ ์š”์†Œ
  background: blue;
  left: 20px;
  top: 20px;  =>position์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” Value (top,left,right,bottom)
  position: static;

๐ŸคŸ position: ์š”๋…€์„์˜ ์†์„ฑ๊ฐ’์„ ์•Œ์•„๋ณด์ž!

1. position: static; (CSS ๊ธฐ๋ณธ๊ฐ’)

  • static : ๊ณ ์ •๋œ, ์ •์ง€ ์ƒํƒœ์˜static

(1) position: static; : HTML์— ์ •์˜๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ธŒ๋ผ์šฐ์ €์ƒ์— ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณด์—ฌ์ค€๋‹ค.

(2) position์„ ์„ค์ •ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ธฐ๋ณธ๊ฐ’์ธ static์œผ๋กœ ์ ์šฉ๋˜์–ด์„œ left, top ์œ„์น˜๋ฅผ ์ง€์ •ํ–ˆ์–ด๋„ ์œ„์น˜๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.


2. position: relative;

  • relative : ์ƒ๋Œ€์ ์ธ โ‡’ ์ƒ๋Œ€ ์œ„์น˜ ์ง€์ •!relative

(1) position: relative; : ์›๋ž˜์žˆ์–ด์•ผ ํ•˜๋Š” ์ž๋ฆฌ์—์„œ ์ƒ๋Œ€์ ์œผ๋กœ ์œ„์น˜ ๋ณ€๊ฒฝ

(2) ํŒŒ๋ž€ Item์ด ์›๋ž˜ ์žˆ๋˜ ๊ณต๊ฐ„ ใ… ์€ ์œ ์ง€ํ•˜๋ฉด์„œ ์˜ฎ๊ฒจ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ์˜ค๋Š” ๋นจ๊ฐ„ Item์˜ ์œ„์น˜๊ฐ€ ๊ทธ๋Œ€๋กœ


3. position: absolute;

  • absolute : ์™„์ „ํ•œ, ํ™•๊ณ ํ•œ โ‡’ ์ ˆ๋Œ€ ์œ„์น˜ ์ง€์ •
    absolute

(1) position: absolute; : ๋‚ด Item์ด ๋‹ด๊ฒจ์žˆ๋Š”, ๋‚ด Item๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด Box ๊ธฐ์ค€์—์„œ ์œ„์น˜ ๋ณ€๊ฒฝ
(.box๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ƒ์ž๋Š” article class="container" ์ด๊ธฐ ๋•Œ๋ฌธ์— .container ์•ˆ์—์„œ ์œ„์น˜๊ฐ€ ์™ผ์ชฝ์—์„œ 20px, ์œ„์—์„œ 20px ์ด๋™๋œ๊ฒƒ)

(2) ํŒŒ๋ž€ Item์ด ์›๋ž˜ ์žˆ๋˜ ๊ณต๊ฐ„ ใ… ์—์„œ ์˜~์˜ฅ ๋น ์ ธ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ์˜ค๋Š” ๋นจ๊ฐ„ Item์€ ํŒŒ๋ž€ Item์ด ์žˆ๋˜ ๊ณต๊ฐ„ ใ… ์œผ๋กœ ์˜ฌ๋ผ์˜จ๋‹ค

(3) ์ผ๋ฐ˜์ ์œผ๋กœ absolute๋ฅผ ์“ธ ๊ฒฝ์šฐ, ์ ˆ๋Œ€์ ์œผ๋กœ ์›€์ง์ด๊ณ  ์‹ถ์€ ๋ถ€๋ชจ์—๊ฒŒ position: relative; ๋ฅผ ๋ถ€์—ฌ

(4) absolute ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋˜๋ฉด ๋งˆ์น˜ inline-element์ฒ˜๋Ÿผ ๋‚ด์šฉ์˜ ํฌ๊ธฐ๋งŒํผ๋งŒ ๊ฐ€๋กœ ํฌ๊ธฐ๊ฐ€ ์ง€์ •๋จ

=>p ํƒœ๊ทธ(block ์š”์†Œ) ์ธ๋ฐ inline ์š”์†Œ ์ฒ˜๋Ÿผ ๋‚ด์šฉ ๋งŒํผ๋งŒ ํฌ๊ธฐ ์ฐจ์ง€

์—ฌ๊ธฐ์„œ { left: 0; } ์„ ์ถ”๊ฐ€ํ•˜๋ฉด?
๋งˆ์น˜ width: 100%;๋ฅผ ์ค€ ๊ฒƒ์ฒ˜๋Ÿผ ๋ถ€๋ชจ ์š”์†Œ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ๋งŒํผ ๋ณ€๊ฒฝ๋œ๋‹ค.

๐Ÿ‘ position: absolute ์—์„œ ๊ฐ€์šด๋ฐ ์ •๋ ฌ ํ•˜๊ธฐ!

.item {
	position:absolute;
	left:50%; // ๋ถ€๋ชจ์š”์†Œ์˜ ์™ผ์ชฝ์—์„œ ๋ถ€ํ„ฐ 50% ์ด๋™
	transform:translateX(-50%); // ์ž์‹ ์˜ ๊ฐ€๋กœ ์ˆ˜์น˜์˜ ์ ˆ๋ฐ˜ ๋งŒํผ ๋งˆ์ด๋„ˆ์Šค ์ด๋™
}

transform:translateX(-50%); : left:50% ๋งŒ ์ž…๋ ฅํ•˜๊ณ  ๊ฒฐ๊ณผ ํ™”๋ฉด์„ ๋ณด๋ฉด, ๊ฐ€์šด๋ฐ ์ •๋ ฌ์ด๋ผ๊ธฐ์—”.....์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ข€ ๋” ์น˜์šฐ์ณ์ ธ ์žˆ๋‹ค!

์™œ๋ƒํ•˜๋ฉด ์‚ฌ์‹ค์€ left 50% + item์˜ ๊ฐ€๋กœ ๋„ˆ๋น„ ๋งŒํผ ์ด๋™ํ•œ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ!!
์ด๋Ÿด ๋•Œ, `transform:translateX(-50%) ๋ฅผ ์ด์šฉํ•˜๋ฉด item์˜ ๊ฐ€๋กœ ๋„ˆ๋น„์˜ ์ ˆ๋ฐ˜๋งŒํผ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™๋˜๋ฏ€๋กœ, ์ • ๊ฐ€์šด๋ฐ์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค!


4. position: fixed;

  • fixed : ๊ณ ์ •๋œ, ๋ณ€์น˜ ์•Š๋Š” โ‡’ ๊ณ ์ • ์œ„์น˜ ์ง€์ •
    fixed

(1) position: fixed; : Box์•ˆ์—์„œ ์™„์ „ํžˆ ๋ฒ—์–ด๋‚˜์„œ, Window ์•ˆ์—์„œ = ๋ˆˆ์— ๋ณด์ด๋Š” ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด ํฌ๊ธฐ๋งŒํผ, ํ™”๋ฉด ๋‚ด์—์„œ๋งŒ ์œ„์น˜ ๋ณ€๊ฒฝ
โ‡’ absolute์™€ ๋‹ฌ๋ฆฌ, relative๋ฅผ ๊ฐ€์ง„ ๋ถ€๋ชจ๊ฐ€ ํ•„์š”์—†๋‹ค!
(.container์—์„œ ์™„์ „ํžˆ ๋ฒ—์–ด๋‚˜์„œ, ํฐ ํ™”๋ฉด(window ํŽ˜์ด์ง€)์ƒ์—์„œ ์ด๋™)

(2) ํŒŒ๋ž€ Item์ด ์›๋ž˜ ์žˆ๋˜ ๊ณต๊ฐ„ ใ… ์—์„œ ์˜~์˜ฅ ๋น ์ ธ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ์˜ค๋Š” ๋นจ๊ฐ„ Item์€ ํŒŒ๋ž€ Item์ด ์žˆ๋˜ ๊ณต๊ฐ„ ใ… ์œผ๋กœ ์˜ฌ๋ผ์˜จ๋‹ค


5. position: sticky;

  • sticky : ๋ˆ๋ˆํ•œ, ๋ถ™์ผ ์ˆ˜ ์žˆ๋Š” โ‡’ ๋‹ฌ๋ผ๋ถ™๋Š” ์œ„์น˜ ์ง€์ •
    sticky

(1) position: sticky; : Scrolling ๋˜์–ด๋„ ์—†์–ด์ง€์ง€ ์•Š๊ณ , ์›๋ž˜์žˆ์–ด์•ผ ํ•˜๋Š” ์ž๋ฆฌ์— ๊ทธ๋Œ€๋กœ ์žˆ์Œ
ex) ์‡ผํ•‘๋ชฐ ๋ฐฐ๋„ˆ, ๋„ค์ด๋ฒ„ ์›นํˆฐ ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ” ๊ฐ™์€ ๋Š๋‚Œ..?


๋ง‰๊ฐ„! position์œผ๋กœ ๊ฐ€์šด๋ฐ ์ •๋ ฌ ํ•˜๋Š” ๋ฒ•

.center {
position: absolute;
left: 50%;
transform: translateX(-50%);
}

๐Ÿ’๐Ÿปโ€โ™€๏ธ left: 50%; => ํ™”๋ฉด์˜ ์ „์ฒด ๊ฐ€๋กœ ์ˆ˜์น˜์—์„œ ์ ˆ๋ฐ˜ ๋งŒํผ์˜ ์œ„์น˜๋กœ ์ด๋™
๐Ÿ’๐Ÿปโ€โ™€๏ธ transform: translateX(-50%); => ์ž์‹ ์˜ ๊ฐ€๋กœ ์ˆ˜์น˜์˜ ์ ˆ๋ฐ˜ ๋งŒํผ ๋งˆ์ด๋„ˆ์Šค ์ด๋™


์ฝ”๋ฆฐ์ด๊ฐœ๋ฐœ ์™•์ดˆ๋ณด ์ฝ”๋ฆฐ์ด์ž…๋‹ˆ๋‹ค!
์ด ๋‚ด์šฉ์€ ํ˜ผ์ž ๋™์˜์ƒ ๊ฐ•์˜&๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ๋ฐฐ์šด ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์ œ๊ฐ€ ์ดํ•ดํ•˜๊ณ  ๋„˜์–ด๊ฐ„ ๊ฐœ๋…์ด ํ‹€๋ ธ๊ฑฐ๋‚˜ ๋” ๋ณด์ถฉํ•  ๊ฐœ๋…์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ์ •๋ง ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!!

profile
ํ•˜๋ฃจ๋Š” ์น˜์—ดํ•˜๊ฒŒ ์ธ์ƒ์€ ์—ฌ์œ ๋กญ๊ฒŒ

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2020๋…„ 7์›” 20์ผ

position ๊ฐœ๋…์ด ๋งŽ์ด ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ ์˜ˆ์ œ๊ฐ€ ๋งŽ์•„์„œ ์ดํ•ดํ•˜๋Š”๋ฐ ๋„์›€์ด ๋งŽ์ด ๋์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ