Client/CSS

[CSS] flexbox ๊ธฐ๋ณธ ๊ฐœ๋…

meeeeejin 2021. 5. 5. 23:12

 

flexbox๋ž€?

๊ธฐ์กด์˜ display๋‚˜ positon ๋“ฑ์„ ์ด์šฉํ•œ ์›น์˜ ๋ ˆ์ด์•„์›ƒ ๊ฐœ๋ฐœ์€ ์ƒ๊ฐํ•  ๋ถ€๋ถ„์ด ๋งŽ๊ณ  ์–ด๋Š ์ •๋„ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•ด์„œ ๊ฐœ๋ฐœํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ flexbox ๊ฐœ๋…์ด ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค. flexbox๋ฅผ ์ด์šฉํ•˜๋ฉด ํŠน๋ณ„ํ•œ ๊ณ„์‚ฐ ์—†์ด ์‰ฝ๊ฒŒ ์ •๋ ฌ์„ ํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋ฅผ ํ†ตํ•ด ๋ ˆ์ด์•„์›ƒ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

 

 

 

flexbox ๊ตฌ์„ฑ

์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://d2.naver.com/helloworld/8540176

flex box๋Š” flex container(๋ถ€๋ชจ ์š”์†Œ)์™€ flex item(์ž์‹ ์š”์†Œ)๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. flexbox๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ถ€๋ชจ ์š”์†Œ์— display: flex;๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค. flex ์†์„ฑ์„ ์ถ”๊ฐ€ํ•œ ์š”์†Œ๊ฐ€ flex container๊ฐ€ ๋˜๊ณ , ๊ทธ ์ž์‹ ์š”์†Œ๋“ค์ด flex item์ด ๋ฉ๋‹ˆ๋‹ค. 

.flex_container { 
  display: flex;
}

 

 

 

 

๋ถ€๋ชจ ์š”์†Œ์™€ ์ž์‹ ์š”์†Œ์˜ ์†์„ฑ

flex container์™€ item์—์„œ ์“ธ ์ˆ˜ ์žˆ๋Š” ์†์„ฑ์˜ ์ข…๋ฅ˜๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค. 

์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://d2.naver.com/helloworld/8540176

 

 

flex box๋Š” ๊ฐ€๋กœ ํ˜น์€ ์„ธ๋กœ์˜ ์ •ํ•ด๋‘” ๋ฐฉํ–ฅ์œผ๋กœ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์†์„ฑ์˜ ์—ญํ• ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

 

 

โœ” flex container์˜ ์†์„ฑ

flex-direction

: flex container ์•ˆ์˜ item๋“ค์˜ ๋ฐฉํ–ฅ์„ ์ •ํ•จ

์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

  • row(default): ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌ →
  • row-reverse: ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌ ←
  • column: ์œ„์ชฝ์—์„œ ์•„๋ž˜์ชฝ์œผ๋กœ ์ •๋ ฌ ↓
  • column-reverse: ์•„๋ž˜์ชฝ์—์„œ ์œ„์ชฝ์œผ๋กœ ์ •๋ ฌ ↑
.container {
  flex-direction: row | row-reverse | column | column-reverse; /* default row */
}

 

 

flex-wrap

: flex item์ด flex container๋ฅผ ๋ฒ—์–ด๋‚ฌ์„ ๋•Œ ์ค„์„ ๋ฐ”๊พธ๋Š” ์†์„ฑ

์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

  • nowrap(default): ๋ชจ๋“  itme์„ ํ•œ ์ค„์— ์ •๋ ฌ
  • wrap: item์ด container๋ฅผ ๋ฒ—์–ด๋‚  ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ์ •๋ ฌ
  • wrap-reverse: item์ด container๋ฅผ ๋ฒ—์–ด๋‚  ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ
.container {
  flex-wrap: nowrap | wrap | wrap-reverse; /* default nowrap */
}

 

 

justify-content

: flex-direction์œผ๋กœ ์ •ํ•ด์ง„ ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ์ˆ˜ํ‰์œผ๋กœ item์„ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •ํ•จ

์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

 

  • flex-start(default): item์„ ์™ผ์ชฝ์„ ์‹œ์ž‘์ ์œผ๋กœ ์ •๋ ฌ
  • flex-end: item์„ ์˜ค๋ฅธ์ชฝ์„ ์‹œ์ž‘์ ์œผ๋กœ ์ •๋ ฌ
  • center: item์„ ๊ฐ€์šด๋ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
  • space-between: ์ฒซ ๋ฒˆ์งธ item์€ ์‹œ์ž‘์ ์—, ๋งˆ์ง€๋ง‰ item์€ ๋์ ์— ๋ถ™๊ณ  ๋‚˜๋จธ์ง€ item ์‚ฌ์ด์˜ ๊ณต๊ฐ„์€ ๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„๋ฐฐ
  • space-around: ๋ชจ๋“  item ์‚ฌ์ด์— ๊ท ๋“ฑํ•œ ๊ณต๊ฐ„ ๋ถ„๋ฐฐ
    ๊ทธ๋ฆผ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ์ฒซ ๋ฒˆ์งธ item๊ณผ ์‹œ์ž‘์  ์‚ฌ์ด์—๋Š” space๊ฐ€ 1์นธ์ด๊ณ , ์ฒซ ๋ฒˆ์งธ item๊ณผ ๋‘ ๋ฒˆ์งธ item ์‚ฌ์ด์—๋Š” space๊ฐ€ 2์นธ์ด์–ด์„œ space๊ฐ€ ๋‹ฌ๋ผ ๋ณด์ž…๋‹ˆ๋‹ค. 
  • space-evenly: ๋ชจ๋“  item ์‚ฌ์ด์— ๊ท ๋“ฑํ•œ ๊ณต๊ฐ„ ๋ถ„๋ฐฐ
.container {
  justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly; /* default flex-start */
}

 

 

align-items

: flex-direction์œผ๋กœ ์ •ํ•ด์ง„ ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ์ˆ˜์ง์œผ๋กœ item์„ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •ํ•จ

์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://css-tricks.com/wp-content/uploads/2018/10/align-items.svg

  • flex-start: item์„ ์œ„์ชฝ์„ ์‹œ์ž‘์ ์œผ๋กœ ์ •๋ ฌ
  • flex-end: item์„ ์•„๋ž˜์ชฝ์„ ์‹œ์ž‘์ ์œผ๋กœ ์ •๋ ฌ
  • center: item์„ ๊ฐ€์šด๋ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
  • stretch(default): item์„ ๋Š˜๋ ค์„œ ์„ธ๋กœ ํฌ๊ธฐ์— ๋งž์ถค
  • baseline: baseline(๊ทธ๋ฆผ ์ฐธ์กฐ)์— ๋งž์ถฐ item ์ •๋ ฌ
.container {
  align-items: stretch | flex-start | flex-end | center | baseline; /* default stretch */
}

 

 

align-content

: flex-direction์œผ๋กœ ์ •ํ•ด์ง„ ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ์ˆ˜์ง์œผ๋กœ ์—ฌ๋Ÿฌ ์ค„์ธ item์„ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •ํ•จ

์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://css-tricks.com/wp-content/uploads/2018/10/align-content.svg

justify-content์™€ ๊ธฐ๋Šฅ์ด ์œ ์‚ฌํ•˜๋ฏ€๋กœ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…์„ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. 

.container {
  align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch | start | end | baseline; /* default stretch */
}

 

 

 

โœ” flex item์˜ ์†์„ฑ

flex-grow

: flex item์˜ ํ™•์žฅ๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ, default 0

์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://css-tricks.com/wp-content/uploads/2018/10/flex-grow.svg

ํ•ด๋‹น flex item์ด flex container ๋‚ด๋ถ€์—์„œ ์–ผ๋งˆํผ์˜ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•ด์•ผ ํ•˜๋Š”์ง€ ๋น„์œจ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ชจ๋“  item์˜ flex-grow ์†์„ฑ์ด 1๋กœ ๋˜์–ด ์žˆ๋‹ค๋ฉด, container์˜ ๊ณต๊ฐ„์€ ๋ชจ๋“  item์— ๋˜‘๊ฐ™์ด ๋ถ„๋ฐฐ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์˜ ๋‘ ๋ฒˆ์งธ ์˜ˆ์‹œ์™€ ๊ฐ™์ด flex-grow๋ฅผ ์ง€์ •ํ•˜๋ฉด flex-grow ๊ฐ’์ด 2์ธ item์ด ๋‹ค๋ฅธ item๋ณด๋‹ค ๊ณต๊ฐ„์„ 2๋ฐฐ ๋” ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’์—๋Š” ์Œ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

.item {
  flex-grow: 4; /* default 0 */
}

 

 

flex-shrink

: flex item์˜ ์ถ•์†Œ์™€ ๊ด€๋ จ๋œ ์†์„ฑ, default 1

 

flex item์ด ์–ผ๋งˆ๋‚˜ ๊ณต๊ฐ„์„ ์ ๊ฒŒ ์ฐจ์ง€ํ•˜๊ณ  ์‹ถ์€์ง€๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. flex-grow์˜ ๋ฐ˜๋Œ€ ๊ฐœ๋…์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์Œ์ˆ˜๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

.item {
  flex-shrink: 3; /* default 1 */
}

 

 

flex-basis

: flex item์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•จ, defaul auto

 

๋‚จ์€ ๊ณต๊ฐ„์ด ๋ถ„๋ฐฐ๋˜๊ธฐ ์ „์— item์˜ ๊ธฐ๋ณธ ์‚ฌ์ด์ฆˆ๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธธ์ด(%, rem, px) ๋˜๋Š” ํ‚ค์›Œ๋“œ(auto, content)๋กœ ์„ค์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

.item {
  flex-basis:  | auto; /* default auto */
}

 

 

flex

: flex-grow, flex-shrink, flex-basis์˜ ์ถ•์•ฝํ˜•

 

flex-grow, flex-shrink, flex-basis๋ฅผ ํ•œ ๋ฒˆ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค. 

.item {
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}

 

 

 

Flex box๋กœ ๋ ˆ์ด์•„์›ƒ ๋งŒ๋“ค๊ธฐ

โœ” HTML ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flexbox</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header class="header">
        <h1>My Webpage</h1>
    </header>
    <nav class="nav">
        <a href="">Home</a>
        <a href="">About</a>
        <a href="">Q&A</a>
    </nav>
    <div id="main">
        <article class="content">
            <h2>About me</h2>
            <h3>Introduce: </h3>
            <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Possimus sed ea dignissimos, reprehenderit quam voluptatum esse facere sit fuga dolores eaque, enim, aperiam doloribus ad eius nobis? Repellat, aliquid nulla?</p>
        </article>
        <aside class="aside">
            <h2>AD</h2>
            <p>Lorem, ipsum dolor sit ametetur adipisicing eue, suscipit exorum exc!</p>
        </aside>
    </div>
    <footer class="footer">
        <h2>Footer</h2>
    </footer>
</body>
</html>

 

โœ” CSS ์ฝ”๋“œ

* { box-sizing: border-box; }
/* footer๋ฅผ ์›น์‚ฌ์ดํŠธ ์•„๋ž˜์ชฝ์— ์œ„์น˜ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์„ค์ • */
html, body {
    height: 100%;
}
body {
    margin: 0;
    display: flex;
    flex-direction: column;
}
a {text-decoration: none;}
a:link, a:visited, a:active { color: inherit; }

.header {
    background-color: burlywood;
    padding: 30px;
    text-align: center;
}
.nav {
    background-color: coral;
    display: flex;
    justify-content: flex-end;
}
.nav a {
    padding: 8px;
    margin-right: 20px;
}
#main {
    display: flex;
    flex-grow: 1;       /* footer๋ฅผ ์›น์‚ฌ์ดํŠธ ์•„๋ž˜์ชฝ์— ์œ„์น˜ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์„ค์ • */
}
.content {
    flex: 70%;
    padding: 40px;
}
.aside {
    background-color: lemonchiffon;
    flex: 30%;
    padding: 40px;
}
.footer {
    background-color: lightskyblue;
    padding: 30px;
    text-align: center;
    flex-shrink: 0;     /* ์›น์‚ฌ์ดํŠธ๋ฅผ ์ถ•์†Œํ•ด๋„ footer๊ฐ€ ๊นจ์ง€์ง€์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์„ค์ • */
}

 

โœ” ์‹คํ–‰ ๊ฒฐ๊ณผ

 

 

 

 

 

โœ” ์ฐธ๊ณ  ์‚ฌ์ดํŠธ

flexbox๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” 10๊ฐ€์ง€ ๋ ˆ์ด์•„์›ƒ

A Complete Guide to Flexbox | CSS - Tricks

 

 

 

 

 

728x90