Flexbox in CSS
Basic Concepts of Flexbox
To understand how Flexbox works, it is essential to familiarize yourself with some key concepts and terminology. Flexbox introduces several specific terms and properties that will allow you to control the arrangement of elements more precisely.
Key Terminology
-
Flex Container: The parent component that has
display: flex;
ordisplay: inline-flex;
. This container establishes the Flex context for its children. -
Flex Items: The direct children of the Flex container. These elements are arranged using Flexbox properties.
-
Main Axis: The main axis in which the Flex items are placed. By default, this axis is horizontal (left to right).
-
Cross Axis: The axis perpendicular to the main axis. By default, this axis is vertical (top to bottom).
-
Main Start and Main End: The starting and ending positions of the main axis.
-
Cross Start and Cross End: The starting and ending positions of the cross axis.
Fundamental Properties
Flex Container Properties
-
display: Defines a flex container. It can be
flex
orinline-flex
.css -
flex-direction: Sets the direction of the main axis (default is
row
).css -
flex-wrap: Decides if the flex items should wrap when they do not fit in a single line.
css -
flex-flow: A shorthand for the
flex-direction
andflex-wrap
properties.css -
justify-content: Aligns the items along the main axis.
css -
align-items: Aligns the items along the cross axis.
css -
align-content: Aligns the flex lines along the cross axis when there is extra space.
css
Flex Item Properties
-
order: Modifies the order of the items within the container.
css -
flex-grow: Defines the ability of an item to grow if necessary.
css -
flex-shrink: Defines the ability of an item to shrink if necessary.
css -
flex-basis: The flex basis of an item before the remaining space is distributed.
css -
flex: A shorthand for
flex-grow
,flex-shrink
, andflex-basis
.css -
align-self: Allows the alignment of a single item along the cross axis, overriding
align-items
.css
Explanatory Images
- [Placeholder: Image showing a container with
display: flex;
and several flex items arranged in a horizontal row.] - [Placeholder: Diagram illustrating the concepts of main axis and cross axis, along with main start, main end, cross start, and cross end.]
These concepts are fundamental to making the most of Flexbox's capabilities. In the following chapters, we will dive deeper into each of these properties and their use in various scenarios.
- Introduction to Flexbox in CSS
- Basic Concepts of Flexbox
- Flex Container and Flex Items
- Direction of Main Axis and Cross Axis
- Flex-direction Property
- Property flex-wrap
- Justify-content Property
- Align-items Property
- align-content Property
- Property flex-grow, flex-shrink, and flex-basis
- Flex Property and Shorthand
- Ordering Elements with `order`
- Individual Alignment with align-self
- Flexbox and Responsive Design
- Conclusions and Best Practices with Flexbox