Flexbox 패턴
Flexbox 패턴 (Flexbox Patterns)
Flexbox를 사용하는 몇 가지 일반적인 패턴이에요. 더 많은 정보는 Tobias Ahlin 블로그에서 찾을 수 있어요.
출처: 문서
본문
Flex 행 / 열 줄바꿈 (Flex row / column break)
적용된 요소가 flex 레이아웃에서 행/열 줄바꿈을 만들도록 강제하는 CSS 클래스를 정의할 수 있어요.
.flex-break
flex: 1 0 100% !important
.row
.flex-break
height: 0 !important
.column
.flex-break
width: 0 !important
flex 컨테이너를 정의할 때 no-wrap을 쓰지 않도록 주의하고, 필요한 곳에 flex-break 클래스의 div를 삽입하세요.
[!TIP] 행을 끊는 요소에는
q-py-##을, 열을 끊는 요소에는q-px-##을 사용해 공간을 늘릴 수 있어요.
<div class="row">
<div>Col 1 / Row 1</div>
<div>Col 2 / Row 1</div>
<div class="flex-break"></div>
<div>Col 1 / Row 2</div>
<div class="flex-break q-py-md"></div>
<div>Col 1 / Row 3</div>
<div>Col 2 / Row 3</div>
<div>Col 3 / Row 3</div>
</div>
예시 "Row break":
<template>
<div class="example-break-row">
<div class="row items-start example-container">
<div class="example-cell" tabindex="0">Col 1 / Row 1</div>
<div class="example-cell col-6" tabindex="0"
>Col 2 / Row 1 - 1<br />Col 2 / Row 1 - 2</div
>
<div class="flex-break"></div>
<div class="example-cell" tabindex="0">Col 1 / Row 2</div>
<div class="flex-break q-py-md"></div>
<div class="example-cell col-4" tabindex="0">Col 1 / Row 3</div>
<div class="example-cell" tabindex="0">Col 2 / Row 3</div>
<div class="example-cell" tabindex="0">Col 3 / Row 3</div>
</div>
</div>
</template>
<style lang="sass">
.example-break-row
.flex-break
flex: 1 0 100% !important
height: 0 !important
.example-container
.example-cell
margin: 1px
padding: 4px 8px
box-shadow: inset 0 0 0 2px #9e9e9e
</style>
[!IMPORTANT]
column타입 flex를 사용할 때는 컨테이너에 높이를 정의해야 해요. 높이가 가장 긴 컬럼을 담을 만큼 충분히 커야 해요.
예시 "Column break":
<template>
<div class="example-break-column">
<div class="column inline items-start example-container">
<div class="example-cell" tabindex="0">Col 1 / Row 1</div>
<div class="example-cell" tabindex="0"
>Col 2 / Row 1 - 1<br />Col 2 / Row 1 - 2</div
>
<div class="flex-break"></div>
<div class="example-cell" tabindex="0">Col 1 / Row 2</div>
<div class="flex-break q-px-md"></div>
<div class="example-cell" tabindex="0">Col 1 / Row 3</div>
<div class="example-cell" tabindex="0">Col 2 / Row 3</div>
<div class="example-cell" tabindex="0">Col 3 / Row 3</div>
</div>
</div>
</template>
<style lang="sass">
.example-break-column
.flex-break
flex: 1 0 100% !important
width: 0 !important
.example-container
height: 100px
.example-cell
margin: 1px
padding: 4px 8px
box-shadow: inset 0 0 0 2px #9e9e9e
</style>
Masonry 스타일 레이아웃 (Masonry-like layout)
여러 컬럼이 있는 column 타입 flex를 사용하면 요소의 시각적 순서가 세로 컬럼을 따르게 돼요. 때로는 레이아웃의 행을 따르도록 순서를 원할 때가 있는데, 이를 위해 커스텀 order CSS 스타일과 컬럼 break 요소의 조합을 사용할 수 있어요.
[!IMPORTANT] 레이아웃에 사용할 컬럼 수를 알아야 해요. 또한 최상의 시각적 효과를 위해 레이아웃 안의 요소 높이는 서로 비슷해야 해요.
$x개의 컬럼에 대한 일반 CSS 공식은:
$x: 3;
@for $i from 1 through ($x - 1) {
.item:nth-child(#{$x}n + #{$i}) {
order: #{$i};
}
}
.item:nth-child(#{$x}n) {
order: #{$x};
}
예시로, 4컬럼 레이아웃을 원한다고 가정해요:
.item:nth-child(4n+1)
order: 1
.item:nth-child(4n+2)
order: 2
.item:nth-child(4n+3)
order: 3
.item:nth-child(4n)
order: 4
HTML에는 따라야 할 몇 가지 요구사항이 있어요:
- flex column 컨테이너에 높이가 정의되어 있어야 함
- 컬럼 break 요소는 시작 부분에 배치되어야 함
- 컬럼 break 요소는 컬럼 수만큼 있어야 함
- 첫 번째 컬럼 break 요소는 숨겨져야 함(
hidden클래스 또는display: none스타일)
예시로, 4컬럼 레이아웃을 원한다고 가정해요:
<div class="column">
<div class="flex-break hidden"></div>
<div class="flex-break"></div>
<div class="flex-break"></div>
<div class="flex-break"></div>
<div>Cell 1</div>
<div>Cell 2</div>
...
<div>Cell last</div>
</div>
예시 "Masonry":
<template>
<div class="example-masonry">
<q-btn
class="q-mb-md"
color="primary"
label="Regenerate layout"
@click="onClick"
/>
<div class="column example-container">
<div class="flex-break hidden"></div>
<div class="flex-break"></div>
<div class="flex-break"></div>
<div class="flex-break"></div>
<div
v-for="(cell, i) in cells"
:key="i"
class="example-cell"
tabindex="0"
>
<div>
<div v-for="(text, j) in cell" :key="j">
{{ text }}
</div>
</div>
</div>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
const generateCells = () =>
Array.from({ length: 24 }, (item, cell) =>
Array.from(
{ length: 2 + Math.ceil(3 * Math.random()) },
(entry, text) => `Cell ${cell + 1} - ${text + 1}`
)
)
const cells = ref(generateCells())
function onClick() {
cells.value = generateCells()
}
</script>
<style lang="sass">
.example-masonry
.flex-break
flex: 1 0 100% !important
width: 0 !important
$x: 4
@for $i from 1 through ($x - 1)
.example-container > div:nth-child(#{$x}n + #{$i})
order: #{$i}
.example-container > div:nth-child(#{$x}n)
order: #{$x}
.example-container
height: 700px
.example-cell
width: 25%
padding: 1px
> div
padding: 4px 8px
box-shadow: inset 0 0 0 2px #9e9e9e
</style>
Pseudo 선택자로 행/열 끊기 (Masonry with pseudo selectors to break rows / columns)
행/열 break 요소를 삽입하기 어렵거나 불가능하고 2~3개 행/열이 필요할 때는 pseudo 선택자를 사용할 수 있어요.
.container-class
&--2-rows
:before
flex: 1 0 100% !important
height: 0 !important
order: 1
&--2-columns
:before
flex: 1 0 100% !important
width: 0 !important
order: 1
&--3-rows
:before,
:after
flex: 1 0 100% !important
height: 0 !important
order: 2
&--3-columns
:before,
:after
flex: 1 0 100% !important
width: 0 !important
order: 2
예시 "Masonry like table grid":
<template>
<q-table
grid
:card-container-class="cardContainerClass"
title="Treats"
:rows="rows"
:columns="columns"
row-key="name"
:filter="filter"
hide-header
v-model:pagination="pagination"
:rows-per-page-options="rowsPerPageOptions"
>
<template #top-right>
<q-input
borderless
dense
debounce="300"
v-model="filter"
placeholder="Search"
>
<template #append>
<q-icon name="search" />
</template>
</q-input>
</template>
<template #item="props">
<div class="q-pa-xs col-xs-12 col-sm-6 col-md-4">
<q-card>
<q-card-section class="text-center">
Calories for
<br />
<strong>{{ props.row.name }}</strong>
</q-card-section>
<q-separator />
<q-card-section
class="flex flex-center"
:style="{ fontSize: props.row.calories / 2 + 'px' }"
>
<div>{{ props.row.calories }} g</div>
</q-card-section>
</q-card>
</div>
</template>
</q-table>
</template>
<script setup>
import { useQuasar } from 'quasar'
import { computed, ref, watch } from 'vue'
const deserts = [
// ...
]
const rows = []
deserts.forEach(name => {
for (let i = 0; i < 24; i++) {
rows.push({
name: name + ' (' + i + ')',
calories: 20 + Math.ceil(50 * Math.random())
})
}
})
rows.sort(() => -1 + Math.floor(3 * Math.random()))
const $q = useQuasar()
function getItemsPerPage() {
if ($q.screen.lt.sm) {
return 3
}
if ($q.screen.lt.md) {
return 6
}
return 9
}
const filter = ref('')
const pagination = ref({
page: 1,
rowsPerPage: getItemsPerPage()
})
watch(
() => $q.screen.name,
() => {
pagination.value.rowsPerPage = getItemsPerPage()
}
)
const columns = [
{ name: 'name', label: 'Name', field: 'name' },
{ name: 'calories', label: 'Calories (g)', field: 'calories' }
]
const cardContainerClass = computed(() =>
$q.screen.gt.xs
? 'example-masonry-table-grid example-masonry-table-grid--' +
($q.screen.gt.sm ? '3' : '2')
: null
)
const rowsPerPageOptions = computed(() =>
$q.screen.gt.xs ? ($q.screen.gt.sm ? [3, 6, 9] : [3, 6]) : [3]
)
</script>
<style lang="sass">
.example-masonry-table-grid
flex-direction: column
height: 700px
&--2
> div
&:nth-child(2n + 1)
order: 1
&:nth-child(2n)
order: 2
&:before
content: ''
flex: 1 0 100% !important
width: 0 !important
order: 1
&--3
> div
&:nth-child(3n + 1)
order: 1
&:nth-child(3n + 2)
order: 2
&:nth-child(3n)
order: 3
&:before,
&:after
content: ''
flex: 1 0 100% !important
width: 0 !important
order: 2
</style>
더 알아보기 (Learn more)
Flexbox 패턴으로 flex-break 요소로 행/열 줄바꿈을 만들고, 커스텀 order 스타일과 컬럼 break 요소(또는 pseudo 선택자)를 조합해 masonry 스타일 레이아웃을 구현할 수 있어요.