GiantStepDEV
article thumbnail

  • ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒμ€ 1차원 λ°©μ‹μœΌλ‘œ 효과적인 λ ˆμ΄μ•„μ›ƒμ„ 섀계할 수 μžˆλ„λ‘ κ³ μ•ˆλœ μŠ€νƒ€μΌ μž…λ‹ˆλ‹€.
  • 1차원 λ°©μ‹μ΄λž€, κ°€λ‘œ(row) ν˜Ήμ€ μ„Έλ‘œ(column)쀑 ν•œ λ°©ν–₯으둜만 λ ˆμ΄μ•„μ›ƒμ„ μ„€κ³„ν•˜λŠ” 방식을 λ§ν•©λ‹ˆλ‹€.

ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒ

κ΅¬μ„±μš”μ†Œ

  • μ£ΌμΆ•(main axis) : ν”Œλ ‰μŠ€ λ°•μŠ€μ˜ μ§„ν–‰ λ°©ν–₯κ³Ό μˆ˜ν‰ν•œ 좕을 의미
  • ꡐ차좕(cross axis) : μ£ΌμΆ•κ³Ό μˆ˜μ§ν•œ 좕을 의미
  • ν”Œλ ‰μŠ€ μ»¨ν…Œμ΄λ„ˆ : display μ†μ„±κ°’μœΌλ‘œ flexλ‚˜ inline-flexκ°€ 적용된 μš”μ†Œλ₯Ό 의미
  • ν”Œλ ‰μŠ€ μ•„μ΄ν…œ : ν”Œλ ‰μŠ€ μ»¨ν…Œμ΄λ„ˆμ™€ μžμ‹ 관계λ₯Ό μ΄λ£¨λŠ” νƒœκ·Έ ꡬ성 μš”μ†Œλ₯Ό 의미

λ ˆμ΄μ•„μ›ƒ 확인 방법

ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒμ˜ κΈ°λ³Έ 속성

display 속성

display: flex; / inline-flex;

πŸ‘‰ flexλŠ” 적용된 μš”μ†Œμ˜ λ‹€μŒ μš”μ†Œκ°€ 항상 쀄 λ°”κΏˆλ˜κ³ , inline-flexλŠ” λ‹€μŒ μš”μ†Œκ°€ 주변에 배치

πŸ‘‰ display 속성이 μ§€μ •λœ μš”μ†ŒλŠ” ν”Œλ ‰μŠ€ μ»¨ν…Œμ΄λ„ˆκ°€ 되고, μžμ‹ μš”μ†ŒλŠ” ν”Œλ ‰μŠ€ μ•„μ΄ν…œμ΄ λ©λ‹ˆλ‹€.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
    <title>flex box</title>
    <style>
     .flex-container {
     width: 300px;
     height: 200px;
     background-color:bisque;
     border: 1px solid black;
     display: flex;
    }

     .flex-item {
     width: 80px;
     height: 50px;
     margin: 2px;
     text-align: center;
     border: 1px solid darkblue;
     color: white;
     background-color: orange;
    }
     .flex-item:nth-child(2n) {
     background-color: orangered;
    }
    </style>
</head>
<body>
    <div class="flex-container">
        <div class="flex-item">item1</div>
        <div class="flex-item">item2</div>
        <div class="flex-item">item3</div>
        <div class="flex-item">item4</div>
    </div>
</body>
</html>

flex-direction 속성

 πŸ‘‰ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒμ˜ μ£ΌμΆ• λ°©ν–₯(μ§„ν–‰ λ°©ν–₯)을 κ²°μ • ν•©λ‹ˆλ‹€.

flex-direction: 속성값;

flex-wrap 속성

πŸ‘‰ ν”Œλ ‰μŠ€ μ•„μ΄ν…œμ΄ μ»¨ν…Œμ΄λ„ˆ μ˜μ—­μ„ λ²—μ–΄λ‚  λ•Œ μ–΄λ–»κ²Œ 처리 ν• μ§€ κ²°μ • ν•©λ‹ˆλ‹€.

flex-wrap: 속성값;

πŸ‘‰ κΈ°λ³Έ 값은 nowrap μž…λ‹ˆλ‹€. (μΌμ •ν•œ ν¬κΈ°κΉŒμ§€ μ°Œκ·ΈλŸ¬μ§€λ‹€κ°€ 더 이상 곡간이 μ—†μœΌλ©΄ λ„˜μ–΄κ°)

 

nowrap

μ•„μ΄ν…œ 간격이 쀄어듦.
곡간이 μ—†μœΌλ©΄ λ„˜μ–΄κ°

 

wrap

곡간 μ—†μœΌλ©΄ 쀄 λ°”κΏˆ

flex-flow 속성

πŸ‘‰ flex-direction 속성과 flex-wrap 속성을 ν•œ λ²ˆμ— μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

flex-flow: [flex-direction 속성] [flex-wrap 속성];

flex-flow: column nowrap;

ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒμ˜ μ •λ ¬ 속성

justify-content 속성

πŸ‘‰ ν”Œλ ‰μŠ€ μ•„μ΄ν…œμ„ μ£ΌμΆ• λ°©ν–₯으둜 μ •λ ¬ν•  λ•Œ μ‚¬μš©ν•˜λŠ” 속성

justify-content: 속성값;

 

CSS

.flex-container {
    width: 300px;
    height: 200px;
    background-color:bisque;
    border: 1px solid black;
    display: flex;
    flex-flow: row wrap;
    justify-content: flex-start;
}

.flex-item {
    width: 80px;
    border: 1px solid darkblue;
    color: white;
    background-color: orange;
    display: flex;
    justify-content: center;
    align-items: center;
}
.flex-item:nth-child(2n) {
    background-color: orangered;
}

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
    <title>flex box</title>
</head>
<body>
    <div class="flex-container">
        <div class="flex-item">item1</div>
        <div class="flex-item">item2</div>
        <div class="flex-item">item3</div>
        <div class="flex-item">item4</div>
        <div class="flex-item">item5</div>
        <div class="flex-item">item6</div>
        <div class="flex-item">item7</div>
        <div class="flex-item">item8</div>
        <div class="flex-item">item9</div>
    </div>
    
</body>
</html>

flex-start

center

space-between

πŸ‘‰ μ•„μ΄ν…œ μ‚¬μ΄μ˜ 간격을 μΌμ •ν•˜κ²Œ μœ μ§€ν•˜μ§€λ§Œ μ•„μ΄ν…œκ³Ό μ»¨ν…Œμ΄λ„ˆ μ‚¬μ΄λŠ” κ³ λ €ν•˜μ§€ μ•ŠμŒ

space-evenly

align-items, align-content, align-self 속성

πŸ‘‰ align-items 속성은 ν”Œλ ‰μŠ€ μ•„μ΄ν…œ ꡐ차좕 λ°©ν–₯으둜 μ •λ ¬ν•  λ•Œ μ‚¬μš©

πŸ‘‰ ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒμ€ 기본으둜 align-items 속성에 stretch 값이 μ μš©λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

πŸ‘‰ κ·Έλž˜μ„œ λ‹¨μˆœν•˜κ²Œ 속성에 flexλ‚˜ inline-flex만 μ μš©ν•΄λ„ μ•„μ΄ν…œμ΄ μ»¨ν…Œμ΄λ„ˆ λ†’μ΄λ§ŒνΌ 가득 μ°Ήλ‹ˆλ‹€.

stretch

flex-start

center


μ—°μŠ΅λ¬Έμ œ - navi λ°” λ§Œλ“€κΈ°

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
       /* κΈ°λ³Έ μŠ€νƒ€μΌ μ΄ˆκΈ°ν™” */
        * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        }

        /* λ„€λΉ„κ²Œμ΄μ…˜λ°” μŠ€νƒ€μΌ */
        .navbar {
        display: flex;
        justify-content: space-around;
        align-items: center;
        background-color: #333;
        padding: 1rem;
        }

        .navbar a {
        text-decoration: none;
        color: white;
        font-size: 1.2rem;
        }

        .navbar a:hover {
        color: #aaa
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <a href="#">메뉴1</a>
        <a href="#">메뉴2</a>
        <a href="#">메뉴3</a>
        <a href="#">메뉴4</a>
      </nav>
    
</body>
</html>

λΆ€λͺ¨ 속성(container)

container(λΆ€λͺ¨) 속성

  • display: flex κ°€λ‘œλ‘œ 배치됨 (μ΅œλŒ€ν™” νŠΉμ„±)
  • display: inline-flex κ°€λ‘œλ‘œ λ°°μΉ˜λ˜μ§€λ§Œ inline νŠΉμ„±μ„ 가짐 (μ΅œμ†Œν™” νŠΉμ„±)
  • flex-direction: μˆ˜ν‰, 수직 그리고 μ‹œμž‘ μœ„μΉ˜μ™€ λ°©ν–₯을 결정함.
    row, row-reverse, column, column-reverse
  • flex-wrap: 묢음 μ—¬λΆ€λ₯Ό κ²°μ •(μ€„λ°”κΏˆ), 기본값은 nowrap(곡간 μ—†μœΌλ©΄ λ„˜μΉ¨), wrap은 μ€„λ°”κΏˆ
  • justify-content: μ£ΌμΆ•μ˜ μ •λ ¬ 방법을 결정함
    flex-start, flex-end, center, space-between, space-evenly
  • align-content: ꡐ차좕에 λŒ€ν•œ μ—¬λŸ¬μ€„μ— λŒ€ν•œ μ •λ ¬ 방법
    flex-start, flex-end, center, space-between, space-around, space-evenly
  • align-items: ꡐ차좕에 λŒ€ν•œ ν•œ 쀄 μ •λ ¬ 방법
    flex-start, flex-end, center
  • flex-flow: flex-directionκ³Ό flex-wrap을 ν•œκΊΌλ²ˆμ— μ§€μ •ν•  수 μžˆλŠ” 단좕 속성(row wrap)

items(μžμ‹) 속성

  • flex-grow: 증가 λΉ„μœ¨μ„ μ‘°μ • 함. 기본값은 0
  • order: μ•„μ΄ν…œμ˜ μˆœμ„œ μ§€μ • κ°€λŠ₯, 기본값은 0, μˆ«μžκ°€ μž‘μ„ 수둝 μ•žμ— ν‘œμ‹œ 됨.
  • flex-shrink: κ°μ†Œ λ„ˆλΉ„ λΉ„μœ¨ μ§€μ •, 기본값은 1, 0으둜 μ§€μ •ν•˜λ©΄ containerκ°€ 쀄어 듀더라도 μ°Œκ·ΈλŸ¬μ§€μ§€ μ•ŠμŒ.
  • flex-basis: 기본값은 auto, content의 크기에 영ν–₯을 λ°›μŒ, μ•ˆλ°›κ²Œ ν• λ €λ©΄ 0으둜 μ§€μ •

flex-grow μ‚¬μš©

body {
  width: 500px;
}

.container {
  background-color: royalblue;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
}

.container .item {
  width: 100px;
  height: 100px;
  border: 3px dashed red;
  margin: 10px;
  background-color: orange;
  display: flex;
  justify-content:center;
  align-items:center;
}

.container .item:nth-child(3) {
  flex-grow: 1;
  background-color: red;
  border: 3px dashed black;
}

.container .item:nth-child(6) {
  flex-grow: 1;
}
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
  <div class="item">6</div>
</div>

항상 쀑앙에 ν‘œμ‹œν•˜κΈ°

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>μ„Έλ‘œλ‘œ 쀑앙에 λ°°μΉ˜ν•˜κΈ°</title>
    <style>
        * {
            margin: 0;
            box-sizing: border-box;
        }
        body {
            background-image: url(./images/bg5.jpg);
            background-repeat: no-repeat;
            background-position: left top;
            background-size: cover;
            background-attachment: fixed;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh; 
        }
        button {
            background-color: #ccc;
            font-size: 1.2em;
            padding: 1em 2em;
            border: none;
            border-radius: 5px;
            box-shadow: 1px 1px 2px #fff;
        }
    </style>
</head>
<body>
    <button>클릭</button>
</body>
</html>


μš©μ–΄ 정리

ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒ

ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒ κΈ°λ³Έ 속성

ν”Œλ ‰μŠ€ λ°•μŠ€ λ ˆμ΄μ•„μ›ƒμ˜ μ •λ ¬ 속성

profile

GiantStepDEV

@kongmi

ν¬μŠ€νŒ…μ΄ μ’‹μ•˜λ‹€λ©΄ "μ’‹μ•„μš”β€οΈ" λ˜λŠ” "κ΅¬λ…πŸ‘πŸ»" ν•΄μ£Όμ„Έμš”!