Bootstrap 4 Flex


Bootstrap 4 Flex

Użyj klas flex, aby kontrolować układ komponentów Bootstrap 4.


Flexbox

Największą różnicą między Bootstrap 3 i Bootstrap 4 jest to, że Bootstrap 4 używa teraz flexbox zamiast pływaków do obsługi układu.

Moduł Flexible Box Layout ułatwia projektowanie elastycznej, responsywnej struktury układu bez użycia pływaka lub pozycjonowania. Jeśli jesteś nowy we flexie, możesz przeczytać o tym w naszym samouczku CSS Flexbox .

Uwaga: Flexbox nie jest obsługiwany w IE9 i wcześniejszych wersjach.

Jeśli potrzebujesz obsługi IE8-9, użyj Bootstrap 3. Jest to najbardziej stabilna wersja Bootstrap i nadal jest wspierana przez zespół w zakresie krytycznych poprawek błędów i zmian w dokumentacji. Jednak nie zostaną do niego dodane żadne nowe funkcje.

Aby utworzyć kontener flexbox i przekształcić bezpośrednie dzieci w elementy flex, użyj d-flexklasy:

Przykład

Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-flex p-3 bg-secondary text-white">
  <div class="p-2 bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 bg-primary">Flex item 3</div>
</div>

Aby utworzyć wbudowany kontener flexbox, użyj d-inline-flexklasy:

Przykład

Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-inline-flex p-3 bg-secondary text-white">
  <div class="p-2 bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 bg-primary">Flex item 3</div>
</div>

Kierunek poziomy

Użyj .flex-row, aby wyświetlić elementy elastyczne w poziomie (obok siebie). To jest domyślne.

Wskazówka: użyj .flex-row-reverse , aby wyrównać do prawej kierunek poziomy:

Przykład

Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-flex flex-row bg-secondary">
  <div class="p-2 bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 bg-primary">Flex item 3</div>
</div>

<div class="d-flex flex-row-reverse bg-secondary">
  <div class="p-2 bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 bg-primary">Flex item 3</div>
</div>

Kierunek pionowy

Użyj .flex-column, aby wyświetlić elementy elastyczne w pionie (na drugim) lub .flex-column-reverse odwrócić kierunek pionowy:

Przykład

Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-flex flex-column">
  <div class="p-2 bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 bg-primary">Flex item 3</div>
</div>

<div class="d-flex flex-column-reverse">
  <div class="p-2 bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 bg-primary">Flex item 3</div>
</div>


Uzasadnij treść

Użyj .justify-content-*klas, aby zmienić wyrównanie elementów elastycznych. Prawidłowe klasy to start(domyślne) end, center, betweenlub around:

Przykład

Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3

Przykład

<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>

Wypełnienie / równe szerokości

Użyj .flex-fillna elementach elastycznych, aby zmusić je do równej szerokości:

Przykład

Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-flex">
  <div class="p-2 bg-info flex-fill">Flex item 1</div>
  <div class="p-2 bg-warning flex-fill">Flex item 2</div>
  <div class="p-2 bg-primary flex-fill">Flex item 3</div>
</div>

Rosnąć

Użyj .flex-grow-1na elastycznym przedmiocie, aby zająć resztę miejsca. W poniższym przykładzie pierwsze dwa elementy elastyczne zajmują niezbędną przestrzeń, podczas gdy ostatni element zajmuje resztę dostępnego miejsca:

Przykład

Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-flex">
  <div class="p-2 bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 bg-primary flex-grow-1">Flex item 3</div>
</div>

Wskazówka: użyj .flex-shrink-1na przedmiocie elastycznym, aby w razie potrzeby się skurczył.


Zamówienie

Zmień kolejność wizualną określonego elementu(ów) elastycznego(ych) za pomocą .orderklas. Prawidłowe klasy to od 0 do 12, gdzie najniższy numer ma najwyższy priorytet (zamówienie-1 jest wyświetlane przed zamówieniem-2, itd.):

Przykład

Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-flex bg-secondary">
  <div class="p-2 bg-info order-3">Flex item 1</div>
  <div class="p-2 bg-warning order-2">Flex item 2</div>
  <div class="p-2 bg-primary order-1">Flex item 3</div>
</div>

Marginesy automatyczne

Łatwe dodawanie automatycznych marginesów do elementów elastycznych za pomocą .mr-auto(przesuń elementy w prawo) lub za pomocą .ml-auto(przesuń elementy w lewo):

Przykład

Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-flex bg-secondary">
  <div class="p-2 mr-auto bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 bg-primary">Flex item 3</div>
</div>

<div class="d-flex bg-secondary">
  <div class="p-2 bg-info">Flex item 1</div>
  <div class="p-2 bg-warning">Flex item 2</div>
  <div class="p-2 ml-auto bg-primary">Flex item 3</div>
</div>


Zawinąć

Kontroluj sposób zawijania elementów elastycznych w kontenerze elastycznym za pomocą .flex-nowrap(domyślnie) .flex-wraplub .flex-wrap-reverse.

Kliknij poniższe przyciski, aby zobaczyć różnicę między trzema klasami, zmieniając opakowanie elementów flex w przykładowym polu:

Przykład

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7
Flex item 8
Flex item 9
Flex item 10
Flex item 11
Flex item 12
Flex item 13
Flex item 14
Flex item 15
Flex item 16
Flex item 17
Flex item 18
Flex item 19
Flex item 20
Flex item 21
Flex item 22
Flex item 23
Flex item 24
Flex item 25

Przykład

<div class="d-flex flex-wrap">..</div>

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

<div class="d-flex flex-nowrap">..</div>

Wyrównaj zawartość

Kontroluj pionowe wyrównanie zebranych elementów elastycznych z .align-content-* klasami. Prawidłowe klasy to .align-content-start(domyślne) , .align-content-end, .align-content-center, i ..align-content-between.align-content-around.align-content-stretch

Uwaga: Te klasy nie mają wpływu na pojedyncze wiersze elementów flex.

Kliknij poniższe przyciski, aby zobaczyć różnicę między pięcioma klasami, zmieniając pionowe wyrównanie elementów elastycznych w przykładowym polu:

Przykład

Flex item 1
Flex item 2
Flex item 3
Flex item 4
Flex item 5
Flex item 6
Flex item 7
Flex item 8
Flex item 9
Flex item 10
Flex item 11
Flex item 12
Flex item 13
Flex item 14
Flex item 15
Flex item 16
Flex item 17
Flex item 18
Flex item 19
Flex item 20
Flex item 21
Flex item 22
Flex item 23
Flex item 24
Flex item 25

Przykład

<div class="d-flex flex-wrap align-content-start">..</div>

<div class="d-flex flex-wrap align-content-end">..</div>

<div class="d-flex flex-wrap align-content-center">..</div>

<div class="d-flex flex-wrap align-content-around">..</div>

<div class="d-flex flex-wrap align-content-stretch">..</div>

Wyrównaj przedmioty

Kontroluj pionowe wyrównanie pojedynczych rzędów elementów elastycznych z .align-items-* klasami. Prawidłowe klasy to .align-items-start, .align-items-end, .align-items-center, .align-items-baselinei .align-items-stretch (domyślne).

Kliknij poniższe przyciski, aby zobaczyć różnicę między pięcioma klasami:

Przykład

Flex item 1
Flex item 2
Flex item 3

Przykład

<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>

Wyrównaj siebie

Kontroluj pionowe wyrównanie określonego elementu elastycznego z .align-self-* klasami. Prawidłowe klasy to .align-self-start, .align-self-end, .align-self-center, .align-self-baselinei .align-self-stretch (domyślne).

Kliknij poniższe przyciski, aby zobaczyć różnicę między pięcioma klasami:

Przykład

Flex item 1
Flex item 2
Flex item 3

Przykład

<div class="d-flex bg-light" style="height:150px">
  <div class="p-2 border">Flex item 1</div>
  <div class="p-2 border align-self-start">Flex item 2</div>
  <div class="p-2 border">Flex item 3</div>
</div>

Responsywne zajęcia Flex

All flex classes comes with additional responsive classes, which makes it easy to set a specific flex class on a specific screen size.

The * symbol can be replaced with sm, md, lg or xl, which represents small, medium, large or xlarge screens.

Class Description Example
Flex Container    
.d-*-flex Creates a flexbox container for different screens
.d-*-inline-flex Creates an inline flexbox container for different screens
Direction    
.flex-*-row Display flex items horizontally on different screens
.flex-*-row-reverse Display flex items horizontally, and right-aligned, on different screens
.flex-*-column Display flex items vertically on different screens
.flex-*-column-reverse Display flex items vertically, with reversed order, on different screens screens
Justified Content    
.justify-content-*-start Display flex items from the start (left-aligned) on different screens
.justify-content-*-end Display flex items at the end (right-aligned) on different screens
.justify-content-*-center Display flex items in the center of a flex container on different screens
.justify-content-*-between Display flex items in "between" on different screens
.justify-content-*-around Display flex items "around" on different screens
Fill / Equal Width    
.flex-*-fill Force flex items into equal widths on different screens
Grow    
.flex-*-grow-0 Don't make the items grow on different screens  
.flex-*-grow-1 Make items grow on different screens  
Shrink    
.flex-*-shrink-0 Don't make the items shrink on diferent screens  
.flex-*-shrink-1 Make items shrink on different screens  
Order    
.order-*-0-12 Change the order from 0 to 12 on small screens
Wrap    
.flex-*-nowrap Don't wrap items on different screens
.flex-*-wrap Wrap items on different screens
.flex-*-wrap-reverse Reverse the wrapping of items on different screens
Align Content    
.align-content-*-start Align gathered items from the start on different screens
.align-content-*-end Align gathered items at the end on different screens
.align-content-*-center Align gathered items in the center on different screens
.align-content-*-around Align gathered items "around" on different screens
.align-content-*-stretch Stretch gathered items on different screens
Align Items    
.align-items-*-start Align single rows of items from the start on different screens
.align-items-*-end Align single rows of items at the end on different screens
.align-items-*-center Align single rows of items in the center on different screens
.align-items-*-baseline Align single rows of items on the baseline on different screens
.align-items-*-stretch Stretch single rows of items on different screens
Align Self    
.align-self-*-start Align a flex item from the start on different screens
.align-self-*-end Align a flex item at the end on different screens
.align-self-*-center Align a flex item in the center on different screens
.align-self-*-baseline Align a flex item on the baseline on different screens
.align-self-*-stretch Stretch a flex item on different screens