Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮     
     ❯   

W3.CSS Flexbox


Flexbox Layout (w3-flex)

Flexbox is a layout system for arranging items in rows or columns.

Flexbox makes it easier to design complex responsive web layouts.


Thew3-flex Class

Thew3-flex class creates a container for flexbox items.

The children of the flexbox container automatically become flexbox items.

1

2

3

Example

<div class="w3-flex">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

Try it Yourself »

Note

w3-grid andw3-flex is new inW3.CSS 5.0.

w3-grid vs w3-flex

w3-grid is fortwo-dimensional layout, with rows AND columns.

w3-flex is forone-dimensional layout, with rows OR columns.

Standard CSS Properties

Many standard CSS properties can be used for a flexbox container:

  • gap
  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

Thegap Property

Thegap property specifies the gap between of flex items.

Examples

Therow value (default) displays the flex items horizontally from left to right:

<div>

Try it Yourself »


Theflex-direction Property

Theflex-direction property specifies the display-direction of flex items.

It can have one of the following values:

  • row
  • column
  • row-reverse
  • column-reverse

Examples

Therow value (default) displays the flex items horizontally from left to right:

<div>

Try it Yourself »

Thecolumn value displays the flex items vertically from top to bottom:

<div>

Try it Yourself »

Therow-reverse value displays the flex items horizontally (from right to left):

<div>

Try it Yourself »

Thecolumn-reverse value displays the flex items vertically (from bottom to top):

<div>

Try it Yourself »



Theflex-wrap Property

Theflex-wrap property specifies whether the flex items should wrap or not, if there is not enough room for them on one flex line.

It can have one of the following values:

  • nowrap
  • wrap
  • wrap-reverse

Examples

Thenowrap value (default) specifies that the flex items will not wrap:

<div>

Try it Yourself »

Thewrap value specifies that the flex items will wrap if necessary:

<div>

Try it Yourself »

Thewrap-reverse value specifies that the flex items will wrap in reverse order:

<div>

Try it Yourself »


Theflex-flow Property

Theflex-flow property is a shorthand for setting both theflex-direction andflex-wrap properties.

Example

<div>

Try it Yourself »


Thejustify-content Property

Thejustify-content property is used to align the flex items when they do not use all available space on the main-axis (horizontally).

it can have one of the following values:

  • center
  • flex-start
  • flex-end
  • space-around
  • space-between
  • space-evenly

Examples

center positions the flex items in the center of the container:

<div>

Try it Yourself »

flex-start value (default) positions the flex items at thebeginning of the container:

<div>

Try it Yourself »

flex-end positions the flex items at the end of the container:

<div>

Try it Yourself »

space-around value displays the flex items with space around them:

<div>

Try it Yourself »

space-between displays the flex items with space between them:

<div>

Try it Yourself »

space-evenly displays the flex items with equal space around them:

<div>

Try it Yourself »


Thealign-items Property

Thealign-items property is used to align the flex items when they do not use all available vertical space.

It can have one of the following values:

  • center
  • flex-start
  • flex-end
  • stretch
  • baseline
  • normal

Example

center positions the flex items in the middle of the container:

<div>

Result:

1

2

3

Try it Yourself »

Example

Theflex-start value positions the flex items at the top of the container:

<div>

Result:

1

2

3

Try it Yourself »

Example

Theflex-end value positions the flex items at the bottom of the container:

<div>

Result:

1

2

3

Try it Yourself »

Example

Thestretch value stretches the flex items to fill the container (this is equal to "normal" which is default):

<div>

Result:

1

2

3

Try it Yourself »

Example

Thebaseline value positions the flex items at the baseline of the container:

<div>

Note: The example uses different font-size to demonstrate that the items gets aligned by the text baseline:

1

2

3

4

Try it Yourself »


Thealign-content Property

Thealign-content property is used to align the flex lines.

Thealign-content property is similar toalign-items, but instead of aligning flex items, it aligns the flex lines.

It can have one of the following values:

  • center
  • stretch
  • flex-start
  • flex-end
  • space-around
  • space-between
  • space-evenly

In the following examples we use a 300 pixels high container, with theflex-wrap property set towrap, to better demonstrate thealign-content property.

Example

Withcenter, the flex lines are packed toward the center of the container:

<div>

Try it Yourself »

Example

Withstretch, the flex lines stretch to take up the remaining space of the container (this is default):

<div>

Try it Yourself »

Example

Withflex-start, the flex lines are packed toward the start of the container:

<div>

Try it Yourself »

Example

Withflex-end, the flex lines are packed toward the end of the container: 

<div>

Try it Yourself »

Example

Withspace-between, the space between the flex lines are equal, but the first item is flush with the start edge of the container, and the last item is flush with the end edge of the container:

<div>

Try it Yourself »

Example

Withspace-around, the space between the flex lines are equal, but the space before the first item and after the last item is set to half of the space between the flex lines:

<div>

Try it Yourself »

Example

Withspace-evenly, the flex lines are evenly distributed in the flex container, with equal space on top, bottom and between:

<div>

Try it Yourself »


Perfect Centering

In the following example we will solve a common style problem: perfect centering.

1

2

3

SOLUTION: Set both thejustify-content andalign-items properties tocenter, and the flex item will be perfectly centered:

Example

<div>

Try it Yourself »


General CSS Properties

PropertyDescription
align-contentModifies the behavior of the flex-wrap property. It is similar to align-items, but instead of aligning flex items, it aligns flex lines
align-itemsVertically aligns the flex items when the items do not use all available space on the cross-axis
displaySpecifies the display behavior (the type of rendering box) for an element
flex-directionSpecifies the direction of the flex items inside a flex container
flex-flowA shorthand property for flex-direction and flex-wrap
flex-wrapSpecifies whether the flex items should wrap or not, if there is not enough room for them on one flex line
justify-contentHorizontally aligns the flex items when the items do not use all available space on the main-axis


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp