๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

CSS ๋ ˆ์ด์•„์›ƒ : Position, Flex ๋ณธ๋ฌธ

full stack/HTML, CSS

CSS ๋ ˆ์ด์•„์›ƒ : Position, Flex

Shinjio 2023. 3. 8. 19:05

2023.03.07~2023.03.08 ์ฝ”๋“œ์ž‡ ํ•™์Šต๋‚ด์šฉ

 

๐Ÿ”Ž Position ์†์„ฑ

๊ธ€์˜ ํ๋ฆ„์—์„œ ๋ฒ—์–ด๋‚˜ ์š”์†Œ๋ฅผ ์ž์œ ๋กญ๊ฒŒ ๋ฐฐ์น˜ํ•  ๋•Œ ์“ฐ๋Š” ์†์„ฑ

๊ธฐ๋ณธ๊ฐ’์€ static์ด๊ณ , static์ธ ๊ฒฝ์šฐ ์›๋ž˜ ์žˆ์–ด์•ผ ํ•  ์œ„์น˜์— ๋ฐฐ์น˜๋จ

 

๐Ÿ“– ์œ„์น˜ ์ •ํ•˜๊ธฐ

top, right, bottom, left ์†์„ฑ์œผ๋กœ ์œ„์น˜๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ์Œ

์œ„์˜ 4๊ฐœ์˜ ๊ฐ’์ด ๋ชจ๋‘ ๊ฐ™์€ ๊ฒฝ์šฐ inset ์†์„ฑ ์‚ฌ์šฉ

 

๐Ÿ“– relative ํฌ์ง€์…˜

์š”์†Œ์˜ ์›๋ž˜ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์น˜ํ•จ. ์ด๋•Œ ์š”์†Œ์˜ ์›๋ž˜ ์ž๋ฆฌ๋Š” ๊ทธ๋Œ€๋กœ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ์Œ.

 

    .blue {
      background-color: blue;
      width: 200px;
      height: 50px;
      position: relative;
      top: 15px;
      left: 10px;
    }

 

 

 

๐Ÿ“– absolute ํฌ์ง€์…˜

๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ํฌ์ง€์…”๋‹์ด ๋œ ์กฐ์ƒ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์น˜. ์ด๋•Œ ํฌ์ง€์…”๋‹์ด ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ static์ด ์•„๋‹ˆ๋ผ๋Š” (position ์†์„ฑ์„ ์ง€์ •ํ–ˆ๋‹ค๋Š”) ์˜๋ฏธ. absolute ํฌ์ง€์…˜์€ ๊ธ€์˜ ํ๋ฆ„์—์„œ ์™„์ „ํžˆ ๋น ์ ธ, ์š”์†Œ์˜ ์›๋ž˜ ์ž๋ฆฌ๋Š” ์ฐจ์ง€ํ•˜์ง€ ์•Š์Œ.

 

    .blue {
      background-color: blue;
      width: 200px;
      height: 200px;
      position: relative;
      /* top: 15px;      left: 10px; */
    }

    .green {
      background-color: green;
      width: 50px;
      height: 50px;
      position: absolute;
      bottom: 10px;
      left: 30px;
    }

 

 

 

๐Ÿ“– fixed ํฌ์ง€์…˜

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์„ ๊ธฐ์ค€์œผ๋กœ ๊ณ ์ •๋œ ๋ฐฐ์น˜. ๊ธ€์˜ ํ๋ฆ„์—์„œ ์™„์ „ํžˆ ๋น ์ ธ์„œ ์š”์†Œ์˜ ์›๋ž˜ ์ž๋ฆฌ๋Š” ์ฐจ์ง€ํ•˜์ง€ ์•Š์Œ.

๋”ฐ๋ผ์„œ ๋‚ด๋น„๊ฒŒ์ด์…˜์„ ๋งŒ๋“ค ๋•Œ ๊ฒน์น˜์ง€ ์•Š๋„๋ก ๋งˆ์ง„์„ ๋„ฃ์–ด์คŒ

 

    .red {
      background-color: red;
      width: 100%;
      height: 50px;
      position: fixed;
      top: 0;
      left: 0;
      z-index: 1
    }

    .blue {
      margin: 50px;
      background-color: blue;
      width: 200px;
      height: 200px;
      position: relative;
      /* top: 15px;      left: 10px; */
    }

 

 

 

๐Ÿ“– sticky ํฌ์ง€์…˜

static์ฒ˜๋Ÿผ ์›๋ž˜ ์œ„์น˜์— ๋ฐฐ์น˜๋˜์–ด ์žˆ๋‹ค๊ฐ€, ์ •ํ•ด์ง„ ์œ„์น˜์— ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์Šคํฌ๋กค๋˜๋ฉด ๊ทธ๋•Œ๋ถ€ํ„ฐ fixed์ฒ˜๋Ÿผ ๊ณ ์ •๋˜์–ด ๋ฐฐ์น˜

๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” static์ฒ˜๋Ÿผ ๋ฐฐ์น˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ์š”์†Œ์˜ ์›๋ž˜ ์ž๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•จ

 

    .red {
      background-color: red;
      width: 100%;
      height: 50px;
      position: sticky;
      top: 0;
      left: 0;
      z-index: 1
    }

    .blue {
      /* margin: 50px; */
      background-color: blue;
      width: 200px;
      height: 200px;
      position: relative;
      /* top: 15px;      left: 10px; */
    }

 

 

 

๐Ÿ“– z-index๊ฐ’

์•ž ๋’ค ์ˆœ์„œ๋ฅผ ์ •ํ•  ๋•Œ ์“ฐ๋Š” ๊ฐ’์œผ๋กœ ์ˆœ์„œ๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์œ„ ์—†์ด ์‚ฌ์šฉ๋จ. ๊ฐ’์ด ๋†’์„์ˆ˜๋ก ํ™”๋ฉด์—์„œ ์•ž์ชฝ์ด๋ฉฐ, ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ์ฝ”๋“œ์—์„œ ์•„๋ž˜ ์ค„์— ์žˆ๋Š” ์š”์†Œ๊ฐ€ ์•ž์ชฝ์— ๋ณด์ž„.

 

 

 

๐Ÿ”Ž Flexbox

๐Ÿ“– ํ”Œ๋ ‰์Šค๋ฐ•์Šค ๋งŒ๋“ค๊ธฐ

display: flex;

ํ”Œ๋ ‰์Šค๋ฐ•์Šค๋Š” ๊ธฐ๋ณธ์ด row ์ •๋ ฌ (๊ฐ€๋กœ๋กœ ์ •๋ ฌ)์ด๊ณ  ์„ธ๋กœ๊ฐ€ ๊ต์ฐจ์ถ•

 

๊ธฐ๋ณธ flex

 

 

๐Ÿ“– ๋ฐฐ์น˜๋ฐฉํ–ฅ

๊ธฐ๋ณธ - ๊ฐ€๋กœ ์ •๋ ฌ
flex-direction: row;

์„ธ๋กœ ์ •๋ ฌ
flex-direction: column;

 

flex-direction: column

 

๐Ÿ“– ๊ธฐ๋ณธ ์ถ• ์ •๋ ฌ

์™ผ์ชฝ๋ฐฉํ–ฅ ํ˜น์€ ์œ„์ชฝ ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ์ •๋ ฌ
justify-content: flex-start

์˜ค๋ฅธ์ชฝ ํ˜น์€ ์•„๋ž˜์ชฝ๋ถ€ํ„ฐ ์ •๋ ฌ
justify-content: flex-end

์ค‘์•™ ์ •๋ ฌ
justify-content: center

์–‘์ชฝ ์ •๋ ฌ
justify-content: space-between

flex-end ์ •๋ ฌ
center ์ •๋ ฌ
space-between ์ •๋ ฌ

 

๐Ÿ“– ๊ต์ฐจ ์ถ• ์ •๋ ฌ

๋Š˜๋ ค์„œ ๋ฐฐ์น˜ํ•˜๊ธฐ (๊ธฐ๋ณธ)
align-items: stretch;

์™ผ์ชฝ ํ˜น์€ ์œ„๋ถ€ํ„ฐ ์ •๋ ฌ
align-items: flex-start;

์ค‘์•™ ์ •๋ ฌ
align-items: center;

์˜ค๋ฅธ์ชฝ ํ˜น์€ ์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ
align-items: flex-end;

 

๐Ÿ“– ์š”์†Œ๊ฐ€ ๋„˜์น  ๋•Œ

flex-wrap: wrap์„ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๊ต์ฐจ ์ถ• ๋ฐฉํ–ฅ์œผ๋กœ ๋„˜์–ด๊ฐ€์„œ ๋ฐฐ์น˜๋จ

flex-wrap: wrap;

 

 

๐Ÿ“– ๊ฐ„๊ฒฉ

์„ธ๋กœ, ๊ฐ€๋กœ ์ˆœ์„œ๋Œ€๋กœ ์ˆซ์ž ๋‘๊ฐœ๋ฅผ ์“ฐ๋ฉด ์„ธ๋กœ ๊ฐ„๊ฒฉ, ๊ฐ€๋กœ ๊ฐ„๊ฒฉ์„ ๊ฐ๊ฐ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ

gap: 50px 30px;

 

 

๐Ÿ“– ์š”์†Œ ๋Š˜๋ ค์„œ ์ฑ„์šฐ๊ธฐ

๊ธฐ๋ณธ๊ฐ’์€ 0์œผ๋กœ flex-grow ๊ฐ’์ด ํด์ˆ˜๋ก ๋งŽ์ด ๋Š˜์–ด๋‚จ

flex-grow: 1;

 

์ดˆ๋ก์ƒ‰ ๋ฐ•์Šค์—๋งŒ flex-grow : 1 ์ง€์ •ํ–ˆ์„ ๋•Œ

 

๐Ÿ“– ์š”์†Œ ์ค„์—ฌ์„œ ์ฑ„์šฐ๊ธฐ

๋งŒ์•ฝ ์š”์†Œ๋“ค์˜ ํฌ๊ธฐ๊ฐ€ ์ปค์„œ ๋„˜์น˜๋Š” ๊ฒฝ์šฐ, ์š”์†Œ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์—ฌ ํ”Œ๋ ‰์Šค๋ฐ•์Šค ์•ˆ์— ๊ฐ€๋“ ์ฑ„์šฐ๊ฒŒ ๋จ. flex-shrink์˜ ๊ธฐ๋ณธ ๊ฐ’์ด 1์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์œผ๋กœ ์š”์†Œ๋ฅผ ์ค„์—ฌ์„œ ๋ฐฐ์น˜ํ•˜๊ณ , 0์œผ๋กœ ์ง€์ •ํ•˜๋ฉด ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค์ง€ ์•Š์Œ. flex-shrink ๊ฐ’์ด ํด์ˆ˜๋ก ๋งŽ์ด ์ค„์–ด๋“ฆ

flex-shrink: 1;

 

ํŒŒ๋ž€์ƒ‰ ๋ฐ•์Šค์—๋งŒ flex-shrink : 0 ์ง€์ •ํ–ˆ์„ ๋•Œ