Movatterモバイル変換


[0]ホーム

URL:


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

CSS Tutorial

CSS HOMECSS IntroductionCSS SyntaxCSS SelectorsCSS How ToCSS CommentsCSS ErrorsCSS ColorsCSS BackgroundsCSS BordersCSS MarginsCSS PaddingCSS Height/WidthCSS Box ModelCSS OutlineCSS TextCSS FontsCSS IconsCSS LinksCSS ListsCSS TablesCSS DisplayCSS Max-widthCSS PositionCSS Z-indexCSS OverflowCSS FloatCSS Inline-blockCSS AlignCSS CombinatorsCSS Pseudo-classesCSS Pseudo-elementsCSS OpacityCSS Navigation BarsCSS DropdownsCSS Image GalleryCSS Image SpritesCSS Attr SelectorsCSS FormsCSS CountersCSS UnitsCSS InheritanceCSS SpecificityCSS !importantCSS Math FunctionsCSS OptimizationCSS AccessibilityCSS Website Layout

CSS Advanced

CSS Rounded CornersCSS Border ImagesCSS BackgroundsCSS ColorsCSS Color KeywordsCSS GradientsCSS ShadowsCSS Text EffectsCSS Custom FontsCSS 2D TransformsCSS 3D TransformsCSS TransitionsCSS AnimationsCSS TooltipsCSS Image StylingCSS Image ModalCSS Image CenteringCSS Image FiltersCSS Image ShapesCSS object-fitCSS object-positionCSS MaskingCSS ButtonsCSS PaginationCSS Multiple ColumnsCSS User InterfaceCSS VariablesCSS @propertyCSS Box SizingCSS Media QueriesCSS MQ Examples

CSS Flexbox

Flexbox IntroFlex ContainerFlex ItemsFlex Responsive

CSS Grid

Grid IntroGrid ContainerGrid ItemsGrid 12-column LayoutCSS @supports

CSS Responsive

RWD IntroRWD ViewportRWD Grid ViewRWD Media QueriesRWD ImagesRWD VideosRWD FrameworksRWD Templates

CSS SASS

SASS Tutorial

CSS Examples

CSS TemplatesCSS ExamplesCSS EditorCSS SnippetsCSS QuizCSS ExercisesCSS WebsiteCSS SyllabusCSS Study PlanCSS Interview PrepCSS BootcampCSS Certificate

CSS References

CSS ReferenceCSS SelectorsCSS CombinatorsCSS Pseudo-classesCSS Pseudo-elementsCSS At-rulesCSS FunctionsCSS Reference AuralCSS Web Safe FontsCSS AnimatableCSS UnitsCSS PX-EM ConverterCSS ColorsCSS Color ValuesCSS Default ValuesCSS Browser Support

CSSFlex Container


CSS Flex Container Properties

The flex container element can have the following properties:

  • display - Must be set toflex orinline-flex
  • flex-direction - Sets the display-direction of flex items
  • flex-wrap - Specifies whether the flex items should wrap or not
  • flex-flow - Shorthand property forflex-direction andflex-wrap
  • justify-content - Aligns the flex items when they do not use all available space on the main-axis (horizontally)
  • align-items - Aligns the flex items when they do not use all available space on the cross-axis (vertically)
  • align-content - Aligns the flex lines when there is extra space in the cross axis and flex items wrap

CSS flex-direction Property

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

This property can have one of the following values:

  • row (default)
  • column
  • row-reverse
  • column-reverse

Example

Therow value is the default value, and it displays the flex items horizontally (from left to right):

.flex-container {
  display: flex;
  flex-direction: row;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  flex-direction: column;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  flex-direction: row-reverse;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  flex-direction: column-reverse;
}

Result:

1
2
3

Try it Yourself »



CSS flex-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.

This property can have one of the following values:

  • nowrap (default)
  • wrap
  • wrap-reverse

Example

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

.flex-container {
  display: flex;
  flex-wrap: nowrap;
}

Result:

1
2
3
4
5
6
7
8
9

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  flex-wrap: wrap;
}

Result:

1
2
3
4
5
6
7
8
9

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  flex-wrap: wrap-reverse;
}

Result:

1
2
3
4
5
6
7
8
9

Try it Yourself »


CSS flex-flow Property

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

Example

.flex-container {
  display: flex;
  flex-flow: row wrap;
}

Try it Yourself »


CSS justify-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).

This property can have one of the following values:

  • center
  • flex-start (default)
  • flex-end
  • space-around
  • space-between
  • space-evenly

Example

Thecenter value aligns the flex items in the center of the container:

.flex-container {
  display: flex;
  justify-content: center;
}

Result:

1
2
3

Try it Yourself »

Example

Theflex-start value aligns the flex items at the beginning of the container (this is default):

.flex-container {
  display: flex;
  justify-content: flex-start;
}

Result:

1
2
3

Try it Yourself »

Example

Theflex-end value aligns the flex items at the end of the container:

.flex-container {
  display: flex;
  justify-content: flex-end;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  justify-content: space-around;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  justify-content: space-between;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  justify-content: space-evenly;
}

Result:

1
2
3

Try it Yourself »


CSS align-items Property

Thealign-items property is used to align the flex items when they do not use all available space on the cross-axis (vertically).

This property can have one of the following values:

  • center
  • flex-start
  • flex-end
  • stretch
  • baseline
  • normal (default)

In the following examples we use a 200 pixels high container, to better demonstrate thealign-items property.

Example

Thecenter value aligns the flex items in the middle of the container:

.flex-container {
  display: flex;
  height: 200px;
  align-items: center;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  height: 200px;
  align-items: flex-start;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  height: 200px;
  align-items: flex-end;
}

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

.flex-container {
  display: flex;
  height: 200px;
  align-items: stretch;
}

Result:

1
2
3

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  height: 200px;
  align-items: baseline;
}

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


1

2

3

4

Try it Yourself »


CSS align-content Property

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

This property can have one of the following values:

  • center
  • stretch (default)
  • flex-start
  • flex-end
  • space-around
  • space-between
  • space-evenly

In the following examples we use a 400 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:

.flex-container {
  display: flex;
  height: 400px;
  flex-wrap: wrap;
  align-content: center;
}

Result:

1
2
3
4
5
6
7
8
9

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  height: 400px;
  flex-wrap: wrap;
  align-content: stretch;
}

Result:

1
2
3
4
5
6
7
8
9

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  height: 400px;
  flex-wrap: wrap;
  align-content: flex-start;
}

Result:

1
2
3
4
5
6
7
8
9

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  height: 400px;
  flex-wrap: wrap;
  align-content: flex-end;
}

Result:

1
2
3
4
5
6
7
8
9

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:

.flex-container {
  display: flex;
  height: 400px;
  flex-wrap: wrap;
  align-content: space-between;
}

Result:

1
2
3
4
5
6
7
8
9

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:

.flex-container {
  display: flex;
  height: 400px;
  flex-wrap: wrap;
  align-content: space-around;
}

Result:

1
2
3
4
5
6
7
8
9

Try it Yourself »

Example

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

.flex-container {
  display: flex;
  height: 400px;
  flex-wrap: wrap;
  align-content: space-evenly;
}

Result:

1
2
3
4
5
6
7
8
9

Try it Yourself »


True Centering

The following example shows how to solve a common style problem: true centering.

To achive true centering, set both thejustify-content and thealign-items properties tocenter for the flex container, and the flex item will be perfectly centered both horizontally and vertically:

Centered
flex item

Example

.flex-container {
  display: flex;
  height: 400px;
  justify-content: center;
  align-items: center;
}

Try it Yourself »



The CSS Flex Container Properties

The following table lists all the CSS Flex Container 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