Ceev ceev tswj qhov kev teeb tsa, kev sib dhos, thiab qhov loj ntawm kab kab ke, kev taw qhia, cov khoom siv, thiab ntau ntxiv nrog rau tag nrho cov suite ntawm cov khoom siv flexbox teb. Rau kev siv ntau dua, kev cai CSS tej zaum yuav tsim nyog.

Pab kom flex cwj pwm

Siv displaycov khoom siv hluav taws xob los tsim lub thawv flexbox thiab hloov cov ntsiab lus ntawm cov menyuam yaus rau hauv cov khoom flex. Flex ntim thiab cov khoom tuaj yeem hloov kho ntxiv nrog cov khoom flex ntxiv.

Kuv yog flexbox thawv!
<div class="d-flex p-2 bd-highlight">I'm a flexbox container!</div>
Kuv yog inline flexbox thawv!
<div class="d-inline-flex p-2 bd-highlight">I'm an inline flexbox container!</div>

Teb variations kuj muaj rau .d-flexthiab .d-inline-flex.

  • .d-flex
  • .d-inline-flex
  • .d-sm-flex
  • .d-sm-inline-flex
  • .d-md-flex
  • .d-md-inline-flex
  • .d-lg-flex
  • .d-lg-inline-flex
  • .d-xl-flex
  • .d-xl-inline-flex

Kev taw qhia

Teem cov kev taw qhia ntawm cov khoom flex nyob rau hauv ib lub thawv flex nrog cov khoom siv kev taw qhia. Feem ntau koj tuaj yeem tshem tawm cov chav kawm kab rov tav ntawm no raws li qhov browser default yog row. Txawm li cas los xij, koj tuaj yeem ntsib cov xwm txheej uas koj xav tau los teeb tsa tus nqi no (xws li cov lus teb layouts).

Siv .flex-rowlos teeb tsa kab rov tav kev taw qhia (qhov browser default), lossis .flex-row-reversepib txoj kab rov tav los ntawm sab nraud.

Flex khoom 1
Flex khoom 2
Flex khoom 3
Flex khoom 1
Flex khoom 2
Flex khoom 3
<div class="d-flex flex-row bd-highlight mb-3">
  <div class="p-2 bd-highlight">Flex item 1</div>
  <div class="p-2 bd-highlight">Flex item 2</div>
  <div class="p-2 bd-highlight">Flex item 3</div>
</div>
<div class="d-flex flex-row-reverse bd-highlight">
  <div class="p-2 bd-highlight">Flex item 1</div>
  <div class="p-2 bd-highlight">Flex item 2</div>
  <div class="p-2 bd-highlight">Flex item 3</div>
</div>

Siv .flex-columnlos teeb tsa txoj kab ntsug, lossis .flex-column-reversepib txoj kab ntsug ntawm sab nraud.

Flex khoom 1
Flex khoom 2
Flex khoom 3
Flex khoom 1
Flex khoom 2
Flex khoom 3
<div class="d-flex flex-column bd-highlight mb-3">
  <div class="p-2 bd-highlight">Flex item 1</div>
  <div class="p-2 bd-highlight">Flex item 2</div>
  <div class="p-2 bd-highlight">Flex item 3</div>
</div>
<div class="d-flex flex-column-reverse bd-highlight">
  <div class="p-2 bd-highlight">Flex item 1</div>
  <div class="p-2 bd-highlight">Flex item 2</div>
  <div class="p-2 bd-highlight">Flex item 3</div>
</div>

Teb variations kuj muaj rau flex-direction.

  • .flex-row
  • .flex-row-reverse
  • .flex-column
  • .flex-column-reverse
  • .flex-sm-row
  • .flex-sm-row-reverse
  • .flex-sm-column
  • .flex-sm-column-reverse
  • .flex-md-row
  • .flex-md-row-reverse
  • .flex-md-column
  • .flex-md-column-reverse
  • .flex-lg-row
  • .flex-lg-row-reverse
  • .flex-lg-column
  • .flex-lg-column-reverse
  • .flex-xl-row
  • .flex-xl-row-reverse
  • .flex-xl-column
  • .flex-xl-column-reverse

Txhim kho cov ntsiab lus

Siv justify-contentcov khoom siv hluav taws xob ntawm flexbox ntim los hloov cov kev sib tw ntawm cov khoom flex ntawm lub ntsiab axis (x-axis pib, y-axis yog flex-direction: column). Xaiv los ntawm start(browser default), end, center, between, lossis around.

Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex justify-content-start">...</div>
<div class="d-flex justify-content-end">...</div>
<div class="d-flex justify-content-center">...</div>
<div class="d-flex justify-content-between">...</div>
<div class="d-flex justify-content-around">...</div>

Teb variations kuj muaj rau justify-content.

  • .justify-content-start
  • .justify-content-end
  • .justify-content-center
  • .justify-content-between
  • .justify-content-around
  • .justify-content-sm-start
  • .justify-content-sm-end
  • .justify-content-sm-center
  • .justify-content-sm-between
  • .justify-content-sm-around
  • .justify-content-md-start
  • .justify-content-md-end
  • .justify-content-md-center
  • .justify-content-md-between
  • .justify-content-md-around
  • .justify-content-lg-start
  • .justify-content-lg-end
  • .justify-content-lg-center
  • .justify-content-lg-between
  • .justify-content-lg-around
  • .justify-content-xl-start
  • .justify-content-xl-end
  • .justify-content-xl-center
  • .justify-content-xl-between
  • .justify-content-xl-around

Kho cov khoom

Siv align-itemscov khoom siv hluav taws xob ntawm cov thawv flexbox los hloov cov kev sib tw ntawm cov khoom flex ntawm tus ntoo khaub lig axis (y-axis pib, x-axis yog flex-direction: column). Xaiv los ntawm start, end, center, baseline, lossis stretch(browser default).

Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex align-items-start">...</div>
<div class="d-flex align-items-end">...</div>
<div class="d-flex align-items-center">...</div>
<div class="d-flex align-items-baseline">...</div>
<div class="d-flex align-items-stretch">...</div>

Teb variations kuj muaj rau align-items.

  • .align-items-start
  • .align-items-end
  • .align-items-center
  • .align-items-baseline
  • .align-items-stretch
  • .align-items-sm-start
  • .align-items-sm-end
  • .align-items-sm-center
  • .align-items-sm-baseline
  • .align-items-sm-stretch
  • .align-items-md-start
  • .align-items-md-end
  • .align-items-md-center
  • .align-items-md-baseline
  • .align-items-md-stretch
  • .align-items-lg-start
  • .align-items-lg-end
  • .align-items-lg-center
  • .align-items-lg-baseline
  • .align-items-lg-stretch
  • .align-items-xl-start
  • .align-items-xl-end
  • .align-items-xl-center
  • .align-items-xl-baseline
  • .align-items-xl-stretch

Kho tus kheej

Siv align-selfcov khoom siv hluav taws xob ntawm cov khoom flexbox los hloov lawv cov kev sib tw ntawm tus ntoo khaub lig axis (y-axis pib, x-axis yog flex-direction: column). Xaiv los ntawm tib cov kev xaiv xws li align-items: start, end, , center, baseline, lossis stretch(browser default).

Flex khoom
Aligned flex khoom
Flex khoom
Flex khoom
Aligned flex khoom
Flex khoom
Flex khoom
Aligned flex khoom
Flex khoom
Flex khoom
Aligned flex khoom
Flex khoom
Flex khoom
Aligned flex khoom
Flex khoom
<div class="align-self-start">Aligned flex item</div>
<div class="align-self-end">Aligned flex item</div>
<div class="align-self-center">Aligned flex item</div>
<div class="align-self-baseline">Aligned flex item</div>
<div class="align-self-stretch">Aligned flex item</div>

Teb variations kuj muaj rau align-self.

  • .align-self-start
  • .align-self-end
  • .align-self-center
  • .align-self-baseline
  • .align-self-stretch
  • .align-self-sm-start
  • .align-self-sm-end
  • .align-self-sm-center
  • .align-self-sm-baseline
  • .align-self-sm-stretch
  • .align-self-md-start
  • .align-self-md-end
  • .align-self-md-center
  • .align-self-md-baseline
  • .align-self-md-stretch
  • .align-self-lg-start
  • .align-self-lg-end
  • .align-self-lg-center
  • .align-self-lg-baseline
  • .align-self-lg-stretch
  • .align-self-xl-start
  • .align-self-xl-end
  • .align-self-xl-center
  • .align-self-xl-baseline
  • .align-self-xl-stretch

Sau

Siv cov .flex-fillchav kawm ntawm cov nus muag cov ntsiab lus kom yuam lawv mus rau hauv qhov dav sib npaug ntawm lawv cov ntsiab lus (lossis qhov dav sib npaug yog tias lawv cov ntsiab lus tsis dhau lawv cov thawv ciam teb) thaum noj tag nrho cov chaw kab rov tav.

Flex yam khoom nrog ntau cov ntsiab lus
Flex khoom
Flex khoom
<div class="d-flex bd-highlight">
  <div class="p-2 flex-fill bd-highlight">Flex item with a lot of content</div>
  <div class="p-2 flex-fill bd-highlight">Flex item</div>
  <div class="p-2 flex-fill bd-highlight">Flex item</div>
</div>

Teb variations kuj muaj rau flex-fill.

  • .flex-fill
  • .flex-sm-fill
  • .flex-md-fill
  • .flex-lg-fill
  • .flex-xl-fill

Loj hlob thiab ntsws

Siv .flex-grow-*cov khoom siv hluav taws xob los hloov cov khoom flex lub peev xwm loj hlob los sau qhov chaw muaj. Hauv qhov piv txwv hauv qab no, cov .flex-grow-1ntsiab lus siv txhua qhov chaw uas nws tuaj yeem ua tau, thaum tso cai rau ob qho khoom flex ntxiv lawv qhov chaw tsim nyog.

Flex khoom
Flex khoom
Yam thib peb flex
<div class="d-flex bd-highlight">
  <div class="p-2 flex-grow-1 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Third flex item</div>
</div>

Siv .flex-shrink-*cov khoom siv hluav taws xob los hloov cov khoom flex lub peev xwm kom ntsws yog tias tsim nyog. Hauv qhov piv txwv hauv qab no, qhov thib ob flex khoom nrog .flex-shrink-1raug yuam kom qhwv nws cov ntsiab lus rau ib kab tshiab, "shrinking" kom tso cai rau ntau qhov chaw rau cov khoom flex yav dhau los nrog .w-100.

Flex khoom
Flex khoom
<div class="d-flex bd-highlight">
  <div class="p-2 w-100 bd-highlight">Flex item</div>
  <div class="p-2 flex-shrink-1 bd-highlight">Flex item</div>
</div>

Teb variations kuj muaj rau flex-growthiab flex-shrink.

  • .flex-{grow|shrink}-0
  • .flex-{grow|shrink}-1
  • .flex-sm-{grow|shrink}-0
  • .flex-sm-{grow|shrink}-1
  • .flex-md-{grow|shrink}-0
  • .flex-md-{grow|shrink}-1
  • .flex-lg-{grow|shrink}-0
  • .flex-lg-{grow|shrink}-1
  • .flex-xl-{grow|shrink}-0
  • .flex-xl-{grow|shrink}-1

Nws pib margins

Flexbox tuaj yeem ua qee yam zoo nkauj heev thaum koj sib xyaw flex kev sib raug zoo nrog cov npoo pib. Hauv qab no yog peb qhov piv txwv ntawm kev tswj cov khoom flex ntawm nws pib margins: lub neej ntawd (tsis muaj qhov pib pib), thawb ob yam khoom mus rau sab xis ( .mr-auto), thiab thawb ob yam khoom mus rau sab laug ( .ml-auto).

Hmoov tsis zoo, IE10 thiab IE11 tsis zoo txhawb kev pib pib ntawm cov khoom flex uas nws niam txiv tsis justify-contentmuaj nuj nqis. Saib cov lus teb StackOverflow no kom paub meej ntxiv.

Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex bd-highlight mb-3">
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
</div>

<div class="d-flex bd-highlight mb-3">
  <div class="mr-auto p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
</div>

<div class="d-flex bd-highlight mb-3">
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="ml-auto p-2 bd-highlight">Flex item</div>
</div>

Nrog cov khoom sib dhos

Vertically txav ib yam khoom flex mus rau saum lossis hauv qab ntawm lub thawv los ntawm kev sib xyaw align-items, flex-direction: column, thiab margin-top: autolossis margin-bottom: auto.

Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex align-items-start flex-column bd-highlight mb-3" style="height: 200px;">
  <div class="mb-auto p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
</div>

<div class="d-flex align-items-end flex-column bd-highlight mb-3" style="height: 200px;">
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="p-2 bd-highlight">Flex item</div>
  <div class="mt-auto p-2 bd-highlight">Flex item</div>
</div>

Qhwv

Hloov cov khoom flex li cas qhwv hauv lub thawv flex. Xaiv los ntawm tsis muaj wrapping txhua (qhov browser default) nrog .flex-nowrap, wrapping nrog .flex-wrap, lossis thim rov qab wrapping nrog .flex-wrap-reverse.

Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex flex-nowrap">
  ...
</div>
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex flex-wrap">
  ...
</div>
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex flex-wrap-reverse">
  ...
</div>

Teb variations kuj muaj rau flex-wrap.

  • .flex-nowrap
  • .flex-wrap
  • .flex-wrap-reverse
  • .flex-sm-nowrap
  • .flex-sm-wrap
  • .flex-sm-wrap-reverse
  • .flex-md-nowrap
  • .flex-md-wrap
  • .flex-md-wrap-reverse
  • .flex-lg-nowrap
  • .flex-lg-wrap
  • .flex-lg-wrap-reverse
  • .flex-xl-nowrap
  • .flex-xl-wrap
  • .flex-xl-wrap-reverse

Kev txiav txim

Hloov qhov pom kev txiav txim ntawm cov khoom flex tshwj xeeb nrog ib txhais tes ntawm ordercov khoom siv. Peb tsuas yog muab kev xaiv rau kev ua ib yam khoom ua ntej lossis zaum kawg, nrog rau kev rov pib siv DOM xaj. Raws li orderyuav siv tus nqi sib npaug (piv txwv li, 5), ntxiv kev cai CSS rau ib qho txiaj ntsig ntxiv uas xav tau.

Thawj yam khoom flex
Qhov thib ob flex khoom
Yam thib peb flex
<div class="d-flex flex-nowrap bd-highlight">
  <div class="order-3 p-2 bd-highlight">First flex item</div>
  <div class="order-2 p-2 bd-highlight">Second flex item</div>
  <div class="order-1 p-2 bd-highlight">Third flex item</div>
</div>

Teb variations kuj muaj rau order.

  • .order-0
  • .order-1
  • .order-2
  • .order-3
  • .order-4
  • .order-5
  • .order-6
  • .order-7
  • .order-8
  • .order-9
  • .order-10
  • .order-11
  • .order-12
  • .order-sm-0
  • .order-sm-1
  • .order-sm-2
  • .order-sm-3
  • .order-sm-4
  • .order-sm-5
  • .order-sm-6
  • .order-sm-7
  • .order-sm-8
  • .order-sm-9
  • .order-sm-10
  • .order-sm-11
  • .order-sm-12
  • .order-md-0
  • .order-md-1
  • .order-md-2
  • .order-md-3
  • .order-md-4
  • .order-md-5
  • .order-md-6
  • .order-md-7
  • .order-md-8
  • .order-md-9
  • .order-md-10
  • .order-md-11
  • .order-md-12
  • .order-lg-0
  • .order-lg-1
  • .order-lg-2
  • .order-lg-3
  • .order-lg-4
  • .order-lg-5
  • .order-lg-6
  • .order-lg-7
  • .order-lg-8
  • .order-lg-9
  • .order-lg-10
  • .order-lg-11
  • .order-lg-12
  • .order-xl-0
  • .order-xl-1
  • .order-xl-2
  • .order-xl-3
  • .order-xl-4
  • .order-xl-5
  • .order-xl-6
  • .order-xl-7
  • .order-xl-8
  • .order-xl-9
  • .order-xl-10
  • .order-xl-11
  • .order-xl-12

Hloov cov ntsiab lus

Siv align-contentcov khoom siv hluav taws xob ntawm cov thawv flexbox txhawm rau kho cov khoom flex ua ke ntawm tus ntoo khaub lig. Xaiv los ntawm start(browser default), end, center, between, around, lossis stretch. Txhawm rau ua kom pom cov khoom siv hluav taws xob no, peb tau tswj hwm flex-wrap: wrapthiab nce tus naj npawb ntawm cov khoom flex.

Tau taub hau! Cov cuab yeej no tsis cuam tshuam rau ib kab ntawm cov khoom flex.

Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex align-content-start flex-wrap">
  ...
</div>
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex align-content-end flex-wrap">...</div>
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex align-content-center flex-wrap">...</div>
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex align-content-between flex-wrap">...</div>
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex align-content-around flex-wrap">...</div>
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
Flex khoom
<div class="d-flex align-content-stretch flex-wrap">...</div>

Teb variations kuj muaj rau align-content.

  • .align-content-start
  • .align-content-end
  • .align-content-center
  • .align-content-between
  • .align-content-around
  • .align-content-stretch
  • .align-content-sm-start
  • .align-content-sm-end
  • .align-content-sm-center
  • .align-content-sm-between
  • .align-content-sm-around
  • .align-content-sm-stretch
  • .align-content-md-start
  • .align-content-md-end
  • .align-content-md-center
  • .align-content-md-between
  • .align-content-md-around
  • .align-content-md-stretch
  • .align-content-lg-start
  • .align-content-lg-end
  • .align-content-lg-center
  • .align-content-lg-between
  • .align-content-lg-around
  • .align-content-lg-stretch
  • .align-content-xl-start
  • .align-content-xl-end
  • .align-content-xl-center
  • .align-content-xl-between
  • .align-content-xl-around
  • .align-content-xl-stretch