FLEX |Flexbox | In CSS

FLEX |Flexbox | In CSS

Flexbox is a layout model in CSS that is designed to make it easier to create responsive and flexible web layouts. The flex property controls how items within a flex container are laid out.

Here are some examples of how Flexbox can be used:

  1. Creating a navigation bar: Flexbox can be used to create a responsive navigation bar that adjusts to different screen sizes. By setting the flex-direction property to "row", the navigation items can be aligned horizontally, and the justify-content property can be used to space the items evenly across the container.

  2. Building a flexible grid: Flexbox can be used to create a flexible grid that adjusts to the size of the container. By setting the flex-wrap property to "wrap", items will automatically wrap to the next line if they cannot fit in a single row. The flex property can be used to control the width of individual grid items.

  3. Centering content vertically and horizontally: Flexbox can be used to center content both vertically and horizontally within a container. By setting the align-items and justify-content properties to "center", content will be centered both vertically and horizontally.

  4. Building a card layout: Flexbox can be used to create a responsive card layout that adjusts to different screen sizes. By setting the flex-direction property to "column", cards can be stacked vertically, and the align-items and justify-content properties can be used to center the cards within the container.

  5. Creating a flexible form layout: Flexbox can be used to create a flexible form layout that adjusts to the size of the container. By setting the flex-direction property to "column", form fields can be stacked vertically, and the align-items property can be used to align the fields to the left or right of the container.

<Shorthands of FLEXbox>

Flexbox shorthands are a convenient way to write CSS rules for Flexbox layouts without having to specify each individual property separately. Here are the most commonly used Flexbox shorthands:

  1. display: flex; - This shorthand sets the display property to "flex", which turns an element into a flex container.

  2. flex-direction: row; - This shorthand sets the direction of the main axis of a flex container to "row", which means that items are laid out horizontally.

  3. flex-wrap: wrap; - This shorthand sets the wrapping behavior of a flex container to "wrap", which means that items will wrap to the next line if they cannot fit in a single line.

  4. justify-content: center; - This shorthand aligns items along the main axis of a flex container. In this example, the items are centered horizontally.

  5. align-items: center; - This shorthand aligns items along the cross-axis of a flex container. In this example, the items are centered vertically.

  6. flex: 1 0 auto; - This shorthand sets the flex-grow, flex-shrink, and flex-basis properties of an item in one line. In this example, the item will grow to fill any available space, will not shrink, and will use its natural size as the basis for calculation.