[CSS #4] Flexbox๋กœ flexํ•˜๊ธฐ๐Ÿ˜Ž

Kayoung Kimยท2021๋…„ 6์›” 22์ผ
1

CSS

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

์ •๋ ฌ์˜ ๋ํŒ์™•! ํ”Œ๋ ‰์Šค ํ•ด๋ฒ„๋ฆฐ ํ”Œ๋ ‰์Šค๋ฐ•์Šค๋Š” ๊ทธ๊ฐ„ CSS ์ •๋ ฌ์˜ ์–ด๋ ค์›€์„ ๊นจ๋ฒ„๋ ธ๋‹ค. ์•Œ์•„๋‘๋ฉด ์“ธ๋ชจ ๋งŽ์€ flex. 'How to use it?'๋ฅผ ์ž˜ ์•Œ๊ณ  ์ž˜ ํ™œ์šฉํ•˜์ž

flexbox๋ฅผ ์“ฐ๋Š” 4 Steps

  • ์„ค์ •
  1. ๋‚˜, ํ”Œ๋ ‰์Šค๋ฐ•์Šค ์“ธ๊ฑฐ์ž„! (๋‹จํ˜ธํ•˜๊ฒŒ ์„ ์–ธํ•˜๊ธฐ)
  2. ๊ฐ€๋กœ ์ •๋ ฌํ• ๊นŒ? ์„ธ๋กœ ์ •๋ ฌํ• ๊นŒ?
  3. ๋ฌด์กฐ๊ฑด ํ•œ ์ค„ ์•ˆ์— ๋‹ค ์ •๋ ฌํ• ๊นŒ? ๋ง๊นŒ?
  • ์ •๋ ฌ
  1. ํ”Œ๋ ‰์Šค๋ฐ•์Šค ์“ฐ๊ธฐ

1. ํ”Œ๋ ‰์Šค๋ฐ•์Šค ์„ ์–ธ

display: flex; / inline-flex;

  • ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ๋ฅผ ๊ฐ์‹ธ๋Š” ๋ถ€๋ชจ ์š”์†Œ์—๊ฒŒ ์ค€๋‹ค.

2. flex-direction

  • flex๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด์ด์ง€ ์•Š๋Š” 2๊ฐœ์˜ ์ถ•(main axis, cross axis)์ด ์ƒ๊ธฐ๋Š”๋ฐ, flex-direction ๊ฐ’์— ๋”ฐ๋ผ axis๊ฐ€ ๋‹ฌ๋ผ์ง

  • row : ๊ฐ€๋กœ๋ฐฉํ–ฅ (์™ผ์ชฝ->์˜ค๋ฅธ์ชฝ, *๊ธฐ๋ณธ๊ฐ’)
  • row-reverse : ์˜ค๋ฅธ์ชฝ->์™ผ์ชฝ
  • column : ์„ธ๋กœ๋ฐฉํ–ฅ (์œ„->์•„๋ž˜)
  • column-reverse : ์•„๋ž˜ -> ์œ„

    'reverse'๋Š” ์‹œ์ž‘ ๋ฐฉํ–ฅ ๋ฟ ์•„๋‹ˆ๋ผ ์†์„ฑ์˜ ์ˆœ์„œ๋„ ๋ฐ”๊พผ๋‹ค.

3. flex-wrap

  • nowrap: flex๋œ ์ปจํ…์ธ ๋ฅผ ๊ฐ์‹ธ์ง€(wrap) ์•Š๊ณ , ์ž์‹์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์—ฌ์„œ๋ผ๋„ ํ•œ ์ค„๋กœ ์ •๋ ฌํ•ด๋ฒ„๋ฆผ (*๊ธฐ๋ณธ๊ฐ’)
  • wrap: ํ•œ ์ค„์— ๋ชจ๋‘ ์ •๋ ฌํ•˜๊ธฐ์— ๊ณต๊ฐ„์ด ๋„‰๋„‰ํ•˜์ง€ ์•Š์œผ๋ฉด ์—ฌ๋Ÿฌ ์ค„๋กœ ๋‚˜๋ˆ„์–ด๋ฒ„๋ฆผ (์‚ฌ์ด์ฆˆ ๋ณ€๊ฒฝ ์—†์Œ)

4. flexbox

  • main axis ๊ธฐ์ค€ ์•„์ดํ…œ ์ •๋ ฌ
    - justify-content: flex-start(์‹œ์ž‘์  *๊ธฐ๋ณธ๊ฐ’), flex-end(๋ ์ ), ์„ผํ„ฐ(๊ฐ€์šด๋ฐ ์ •๋ ฌ), space-between(๊ฐ„๊ฒฉ ๋„“ํžˆ๊ธฐ), space-around(๊ท ์ผํ•˜๊ฒŒ ์–‘ ์˜†์œผ๋กœ ๋„“ํžˆ๊ธฐ)

  • cross axis ๊ธฐ์ค€ ์•„์ดํ…œ ์ •๋ ฌ
    - align-items: flex-start(*๊ธฐ๋ณธ๊ฐ’), flex-end, ์„ผํ„ฐ
    - align-content

    • flexwrap: wrap; ์ผ ๊ฒฝ์šฐ flex line์ด 2๊ฐœ๊ฐ€ ์ƒ๊ธฐ๋Š”๋ฐ, ์š”์†Œ์™€ ์š”์†Œ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์กฐ์ •ํ•  ๋•Œ ์“ด๋‹ค.
    • flex-start, flex-end, ์„ผํ„ฐ, space-between, space-around

flex Items

  • order : order: 1; order: 2;์™€ ๊ฐ™์ด ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๋ฉด ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด order๊ฐ’์œผ๋กœ ๋ฐฐ์—ด๋จ
  • flex-grow : flex item์˜ ์ฆ๊ฐ€ ๋„ˆ๋น„ ๋น„์œจ ์„ค์ •. ๋ฐ•์Šค ๋‚ด์—์„œ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์“ฐ๊ณ , ์ •์ˆ˜(1,2,3..)๋กœ ํ‘œ๊ธฐํ•œ๋‹ค.
  • flex-shrink: flex item์˜ ๊ฐ์†Œ ๋„ˆ๋น„ ๋น„์œจ ์„ค์ •
  • align-self: ํŠน์ • ์•„์ดํ…œ์˜ ์ •๋ ฌ์„ ๋”ฐ๋กœ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ (๊ต์ฐจ์ถ• ๊ธฐ์ค€) * align-items ๋ณด๋‹ค ์šฐ์„  ์ ์šฉ๋จ
  • flex-flow : flex-direction + flex wrap์˜ ๋‹จ์ถ• ์†์„ฑ(์›์ƒทํˆฌํ‚ฌ!)
    row wrap, column wrap column-reverse wrap-reverse

์˜ˆ์ œ (user card)

CSS source code

* {
  box-sizing: border-box;
  margin: 0;
}
 
body {
  font-family: "Muli", sans-serif;
  color: #273444;
  background-color: blue;
}
 
.profile-name {
  font-size: 18px;
  font-weight: 600;
  line-height: 1.3333333333;
}
 
.profile-location {
  font-size: 14px;
  line-height: 1.4285714286;
  color: #8492a6;
}
 
.profile-detail dt {
  font-size: 12px;
  font-weight: 600;
  line-height: 1.6666666667;
  color: #8492a6;
}
 
.profile-detail dd {
  font-size: 32px;
  font-weight: 300;
  line-height: 1.25;
  color: #0066ff;
}

body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: cente;
  align-items: cente;
}
 
.profile {
  display: flex;
  flex-direction: column;  
  align-items: cente;
  width: 386px;
  padding: 32px 40px;
  border-radius: 16px;
  text-align: cente;
  background-color: #FFFFFF;
}
 
.profile-image {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-bottom: 16px;
}
 
.profile-name {
  margin-bottom: 4px;
}
 
.profile-location {
  margin-bottom: 32px;
}
 
.profile-detail {
  display: flex;
  justify-content: space-between;
  align-items: cente;
}

HTML source code

<!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-3</title>
    <link href="https://fonts.googleapis.com/css?family=Muli:300,400,600&display=swap" rel="stylesheet" />
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <section class="profile">
        <img src="./assets/user.jpg" alt="Noah Mdsen" class="profile-image"/>
        <h1 class="profile-name">Noah madsen</h1>
        <p class="profile-location">Copenhagen, Denmark</p>
 
        <dl class="profile-detail">
            <div class="profile-detail-item">
                <dt>Friends</dt>
                <dd>730</dd>
            </div>    
            <div class="profile-detail-item">
                <dt>Projects</dt>
                <dd>3</dd>
            </div>
            <div class="profile-detail-item">
                <dt>Reviews</dt>
                <dd>243</dd>
            </div>    
        </dl>
    </section>    
</body>
</html>

Questions & Learned

  • ์ „์ฒด ์š”์†Œ๋ฅผ ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•  ๋•Œ ๊ทธ ์š”์†Œ๋ฅผ ๊ฐ์‹ธ๋Š” ๋ถ€๋ชจ์—๊ฒŒ flex๋ฅผ ์ ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ปจํ…์ธ  ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ธฐ : body์— flex ์ฃผ๊ธฐ
body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: cente;
  align-items: cente;
}

์ถœ์ฒ˜: '๊น€๋ฒ„๊ทธ์˜ HTML&CSS๋Š” ์žฌ๋ฐŒ๋‹ค'๋ฅผ ํ•™์Šตํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

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