๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Stylesheet/CSS

[CSS3]Flexbox ๋งˆ์Šคํ„ฐํ•˜๊ธฐ: CSS3๋กœ ๋ฐ˜์‘ํ˜• ์›น์‚ฌ์ดํŠธ ๋งŒ๋“ค๊ธฐ

by YJ Dev 2024. 6. 2.
728x90
๋ฐ˜์‘ํ˜•
SMALL

CSS3 FLEXBOX๋Š” ์›น ํŽ˜์ด์ง€ ๋ ˆ์ด์•„์›ƒ์„ ์‰ฝ๊ฒŒ ์กฐ์ •ํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๋””๋ฐ”์ด์Šค์— ๋งž์ถฐ ๋ฐ˜์‘ํ˜• ์›น์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” FLEXBOX์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๋ถ€ํ„ฐ ๊ณ ๊ธ‰ ํ™œ์šฉ ๋ฐฉ๋ฒ•๊นŒ์ง€ ์ฒด๊ณ„์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

css3 flexbox

FLEXBOX ๊ธฐ๋ณธ ๊ฐœ๋…๐Ÿ“š

FLEXBOX๋Š” ํ”Œ๋ ‰์Šค ์ปจํ…Œ์ด๋„ˆ(Flex Container)์™€ ํ”Œ๋ ‰์Šค ์•„์ดํ…œ(Flex Item)์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.
Flex Container๋Š” Flex Item์„ ๊ฐ์‹ธ๋Š” ๋ถ€๋ชจ ์š”์†Œ์ด๊ณ , Flex Item์€ Flex Container ๋‚ด์— ์œ„์น˜ํ•œ ์ž์‹ ์š”์†Œ๋“ค์ž…๋‹ˆ๋‹ค.


์ฃผ์š” ์šฉ์–ด ์„ค๋ช…:

  • Main Axis: Flex Container์˜ ์ฃผ ์ถ•์ž…๋‹ˆ๋‹ค. flex-direction ์†์„ฑ์— ์˜ํ•ด ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.
  • Cross Axis: Main Axis์— ์ˆ˜์ง์ธ ์ถ•์ž…๋‹ˆ๋‹ค.

css3 flexbox

Flex Container ์†์„ฑ๐Ÿ“ฆ

display: flex;

display: flex; ์†์„ฑ์€ Flex Container๋ฅผ ์ •์˜ํ•˜๋Š” ๊ธฐ๋ณธ ์†์„ฑ์ž…๋‹ˆ๋‹ค. ์ด ์†์„ฑ์„ ๋ถ€๋ชจ ์š”์†Œ์— ์ ์šฉํ•˜๋ฉด ํ•ด๋‹น ์š”์†Œ์˜ ์ž์‹ ์š”์†Œ๋“ค์ด Flex Item์œผ๋กœ ๋ณ€ํ™˜๋˜์–ด, Flexbox ๋ ˆ์ด์•„์›ƒ ์‹œ์Šคํ…œ์˜ ๊ทœ์น™์— ๋”ฐ๋ผ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.

.container {
    display: flex; /* inline-flex */
}

์ด ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด Flex Container ๋‚ด์˜ ์ž์‹ ์š”์†Œ๋“ค์ด ์œ ์—ฐํ•œ ๋ฐฐ์น˜์™€ ์ •๋ ฌ ์˜ต์…˜์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ์ •๋ ฌ๋˜๊ณ , ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค. Flex Container์™€ Flex Item์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณต์žกํ•œ ๋ ˆ์ด์•„์›ƒ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • flex: ๋ถ€๋ชจ ์š”์†Œ๋ฅผ Flex Container๋กœ ์„ค์ •ํ•˜์—ฌ ์ž์‹ ์š”์†Œ๋“ค์ด Flex Item์ด ๋˜๋„๋ก ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • inline-flex: Flex Container๋ฅผ ์ธ๋ผ์ธ ์š”์†Œ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. Flex Item์œผ๋กœ ๋ฐฐ์น˜๋˜์ง€๋งŒ, ์ปจํ…Œ์ด๋„ˆ ์ž์ฒด๋Š” ์ธ๋ผ์ธ ์š”์†Œ์ฒ˜๋Ÿผ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

flex-direction

Flex Item์˜ ์ฃผ ์ถ• ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ row์ž…๋‹ˆ๋‹ค.

.container {
    flex-direction: row; /* row, row-reverse, column, column-reverse */
}
      • row: ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ, Flex Item์„ ํ–‰ ๋ฐฉํ–ฅ(์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ)์œผ๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
      • row-reverse: Flex Item์„ ํ–‰ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๋˜, ์ˆœ์„œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
      • column: Flex Item์„ ์—ด ๋ฐฉํ–ฅ(์œ„์—์„œ ์•„๋ž˜)์œผ๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
      • column-reverse: Flex Item์„ ์—ด ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๋˜, ์ˆœ์„œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.

css3 flexboxcss3 flexbox
css3 flexboxcss3 flexbox

flex-wrap

Flex Item์ด Flex Container๋ฅผ ๋ฒ—์–ด๋‚  ๋•Œ ์ค„๋ฐ”๊ฟˆ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

.container {
    flex-wrap: nowrap; /* nowrap, wrap, wrap-reverse */
}
  • nowrap: ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ, ๋ชจ๋“  Flex Item์„ ํ•œ ์ค„์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • wrap: Flex Item์„ ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • wrap-reverse: Flex Item์„ ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ๋ฐฐ์น˜ํ•˜๋˜, ์—ญ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.

css3 flexbox

flex-flow

flex-direction๊ณผ flex-wrap์„ ๋‹จ์ถ• ์†์„ฑ์œผ๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.

.container {
    flex-flow: row wrap;
}

justify-content

์ฃผ ์ถ•์„ ๋”ฐ๋ผ Flex Item์„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

.container {
    justify-content: flex-start; /* flex-start, flex-end, center, space-between, space-around, space-evenly */
}
  • flex-start: ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ, Flex Item์„ ์ฃผ ์ถ•์˜ ์‹œ์ž‘ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • flex-end: Flex Item์„ ์ฃผ ์ถ•์˜ ๋ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • center: Flex Item์„ ์ฃผ ์ถ•์˜ ์ค‘์•™์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • space-between: Flex Item ์‚ฌ์ด์— ๊ท ๋“ฑํ•œ ๊ฐ„๊ฒฉ์„ ๋ฐฐ์น˜ํ•˜๊ณ , ์ฒซ ๋ฒˆ์งธ ์•„์ดํ…œ์€ ์‹œ์ž‘ ์ง€์ ์—, ๋งˆ์ง€๋ง‰ ์•„์ดํ…œ์€ ๋ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • space-around: Flex Item ์ฃผ์œ„์— ๊ท ๋“ฑํ•œ ๊ฐ„๊ฒฉ์„ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • space-evenly: Flex Item์„ ๊ท ๋“ฑํ•œ ๊ฐ„๊ฒฉ์œผ๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.

css3 flexboxcss3 flexbox
css3 flexbox

align-item

๊ต์ฐจ ์ถ•์„ ๋”ฐ๋ผ Flex Item์„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

.container {
    align-items: stretch; /* flex-start, flex-end, center, baseline, stretch */
}
      • stretch: ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ, Flex Item์„ Flex Container์˜ ๋†’์ด์— ๋งž๊ฒŒ ๋Š˜๋ฆฝ๋‹ˆ๋‹ค.
      • flex-start: Flex Item์„ ๊ต์ฐจ ์ถ•์˜ ์‹œ์ž‘ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
      • flex-end: Flex Item์„ ๊ต์ฐจ ์ถ•์˜ ๋ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
      • center: Flex Item์„ ๊ต์ฐจ ์ถ•์˜ ์ค‘์•™์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
      • baseline: Flex Item์„ ์ฒซ ๋ฒˆ์งธ ์ค„์˜ ๊ธฐ์ค€์„ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

css3 flexboxcss3 flexbox
css3 flexboxcss3 flexbox

align-content

๊ต์ฐจ ์ถ•์„ ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ์ค„์˜ Flex Item์„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. Flex Item์ด ํ•œ ์ค„์ผ ๊ฒฝ์šฐ ์ด ์†์„ฑ์€ ํšจ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

  • stretch: ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ, ์ค„์„ Flex Container์˜ ๋†’์ด์— ๋งž๊ฒŒ ๋Š˜๋ฆฝ๋‹ˆ๋‹ค.
  • flex-start: ์ค„์„ ๊ต์ฐจ ์ถ•์˜ ์‹œ์ž‘ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • flex-end: ์ค„์„ ๊ต์ฐจ ์ถ•์˜ ๋ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • center: ์ค„์„ ๊ต์ฐจ ์ถ•์˜ ์ค‘์•™์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • space-between: ์ค„ ์‚ฌ์ด์— ๊ท ๋“ฑํ•œ ๊ฐ„๊ฒฉ์„ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • space-around: ์ค„ ์ฃผ์œ„์— ๊ท ๋“ฑํ•œ ๊ฐ„๊ฒฉ์„ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.

Flex Item ์†์„ฑ๐Ÿงฉ

order

Flex Item์˜ ๋ฐฐ์น˜ ์ˆœ์„œ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 0์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ์ž‘์„์ˆ˜๋ก ์•ž์— ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.

.item {
    order: 1;
}

flex-grow

Flex Item์˜ ์ฆ๊ฐ€ ๊ฐ€๋Šฅ ๋น„์œจ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 0์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ํด์ˆ˜๋ก ๋” ๋งŽ์€ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค.

.item {
    flex-grow: 1;
}

flex-shrink

Flex Item์˜ ์ถ•์†Œ ๊ฐ€๋Šฅ ๋น„์œจ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 1์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ํด์ˆ˜๋ก ๋” ๋งŽ์ด ์ถ•์†Œ๋ฉ๋‹ˆ๋‹ค.

.item {
    flex-shrink: 1;
}

flex-basis

Flex Item์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ auto์ด๋ฉฐ, ํฌ๊ธฐ ๋‹จ์œ„(px, %, em ๋“ฑ)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

.item {
    flex-basis: 100px;
}

flex

flex-grow, flex-shrink, flex-basis๋ฅผ ๊ฒฐํ•ฉํ•œ ๋‹จ์ถ• ์†์„ฑ์ž…๋‹ˆ๋‹ค.

.item {
    flex: 1 1 100px;
}

align-self

ํŠน์ • Flex Item์˜ ๊ต์ฐจ ์ถ• ์ •๋ ฌ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. align-items ์†์„ฑ์„ ๋ฎ์–ด์”๋‹ˆ๋‹ค.

.item {
    align-self: flex-start; /* auto, flex-start, flex-end, center, baseline, stretch */
}
  • auto: ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ, ๋ถ€๋ชจ์˜ align-items ์†์„ฑ์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.
  • flex-start: ๊ต์ฐจ ์ถ•์˜ ์‹œ์ž‘ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • flex-end: ๊ต์ฐจ ์ถ•์˜ ๋ ์ง€์ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • center: ๊ต์ฐจ ์ถ•์˜ ์ค‘์•™์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • baseline: ์ฒซ ๋ฒˆ์งธ ์ค„์˜ ๊ธฐ์ค€์„ ์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • stretch: Flex Container์˜ ๋†’์ด์— ๋งž๊ฒŒ ๋Š˜๋ฆฝ๋‹ˆ๋‹ค.

์‹ค์ „ ์˜ˆ์ œ๐Ÿ”

๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ ๊ตฌ์„ฑ ์˜ˆ์ œ

<div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
</div>

<style>
.container {
    display: flex;
}
.item {
    flex: 1;
    padding: 10px;
    background: lightblue;
    margin: 5px;
}
</style>

css3 flexbox


์ •๋ ฌ ๋ฐ ๋ฐฐ์น˜ ์˜ˆ์ œ

<div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
</div>

<style>
.container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.item {
    padding: 20px;
    background: lightcoral;
    margin: 10px;
}
</style>

css3 flexbox


๋ฐ˜์‘ํ˜• ๋ ˆ์ด์•„์›ƒ ์˜ˆ์ œ

<div class="container">
    <div class="item">Item 1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
</div>

<style>
   body {
        margin: 0;
        font-family: Arial, sans-serif;
    }
    .container {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
    }
    .item {
        background-color: #4CAF50;
        color: white;
        padding: 20px;
        margin: 10px;
        flex: 1 1 calc(33.333% - 40px); /* 3 items per row, accounting for margin */
        box-sizing: border-box;
    }
    @media (max-width: 800px) {
        .item {
            flex: 1 1 calc(50% - 40px); /* 2 items per row on smaller screens */
         }
    }
    @media (max-width: 500px) {
        .item {
            flex: 1 1 100%; /* 1 item per row on very small screens */
        }
    }
</style>

css flex

๊ณ ๊ธ‰ ํ™œ์šฉ ๋ฐฉ๋ฒ•๐Ÿš€

์ค‘์ฒฉ๋œ Flexbox ์‚ฌ์šฉ

Flexbox ์ปจํ…Œ์ด๋„ˆ ๋‚ด์— ๋˜ ๋‹ค๋ฅธ Flexbox ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŒ๋“ค์–ด ๋ณต์žกํ•œ ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<div class="outer-container">
    <div class="inner-container">
        <div class="item">1</div>
        <div class="item">2</div>
    </div>
    <div class="item">3</div>
</div>

<style>
.outer-container {
    display: flex;
    justify-content: space-between;
}
.inner-container {
    display: flex;
    flex-direction: column;
}
.item {
    background: lightpink;
    padding: 10px;
    margin: 5px;
}
</style>

css3 flexbox


Flexbox์™€ Grid์˜ ์ฐจ์ด์  ๋ฐ ๊ฒฐํ•ฉ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

Flexbox๋Š” 1์ฐจ์› ๋ ˆ์ด์•„์›ƒ(ํ–‰ ๋˜๋Š” ์—ด) ์„ค๊ณ„์— ์ ํ•ฉํ•˜๊ณ , Grid๋Š” 2์ฐจ์› ๋ ˆ์ด์•„์›ƒ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋‘˜์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋” ๋ณต์žกํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ ๋ฐ ํด๋ฆฌํ•„ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

Flexbox๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์ตœ์‹  ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. ๊ตฌํ˜• ๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•ด Autoprefixer์™€ ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•ด CSS ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ ํ•ด๊ฒฐ ํŒ๐Ÿ› ๏ธ

์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ ๋ฐ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

  • Flex Item์˜ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ํฌ๊ธฐ ๋ณ€ํ™”: flex-shrink์™€ flex-grow ๊ฐ’์„ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
  • Flex Container์˜ ๋†’์ด ๋ฌธ์ œ: align-items: stretch ์†์„ฑ์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋””๋ฒ„๊น… ํŒ

  • ๋ธŒ๋ผ์šฐ์ € ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•ด Flex Container์™€ Flex Item์˜ ์†์„ฑ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • Flexbox Inspector ํ™•์žฅ ๋„๊ตฌ๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํ•ต์‹ฌ ๋‚ด์šฉ๐Ÿ‘€

css3 flexbox
css3 flexbox
css3 flexbox

728x90
๋ฐ˜์‘ํ˜•