CSS Flexible Box Layout makes it easier to design flexible responsive layout structure without using float or positioning. A flexible box aligns all its child elements from left to right of the viewport unless the child elements have some width set. To use the flexbox model, you should set a flexible container first. Set the child items width as 100% which will force the child items take up as much width as possible.
Here is an example of a flexbox
<div class="flex"> <div>Flexbox 1</div> <div>Flexbox 2</div> <div>Flexbox 3</div> </div> <style> .flex{ display: flex; margin: 0 -15px; } .flex div{ padding: 100px 0; text-align: center; color: #fff; background: #151f28; margin: 0 15px; width: 100%; } </style>
You can use a number of CSS properties along with CSS flex which are as follows.
CSS property flex-direction defines the direction in which flex items are aligned to each other.
.flex{ display: flex; flex-direction: row; }
CSS property flex-wrap defines whether the flex items should be forced to be resized and aligned within a row or take up its defined width.
.flex{ display: flex; flex-wrap: wrap-reverse; }
CSS property flex-flow is a shorthand property for flex-direction and flex-wrap combined.
.flex{ display: flex; flex-flow: row wrap; }
CSS property justify-content is used to align flex items inside the flex container horizontally. This only works when the total width of the flex items is less than the total width of the container.
.flex{ display: flex; justify-content: center; }
CSS property align-items is used to align flex items vertically. The height of the flex container must be larger than the height of the flex items to use this property.
.flex{ display: flex; height: 200px; align-items: baseline; }
CSS property align-content is used to align the flex lines. It is highly useful to split vertical spacing between flex items when flex container has more than one row of flex items.
.flex{ display: flex; height: 600px; flex-wrap: wrap; align-content: flex-end; }
Flex items support a few flex properties which are listed below.
CSS order property defines the order of the flex items. The order value must be a number and the default value is 0.
<div class="flex"> <div>1</div> <div>2</div> <div style="order: 2">3</div> </div>
CSS flex-grow property defines the width a flex item will take up in comparision to the rest of the flex items. The value must be a number and the default value is 0.
<div class="flex"> <div style="flex-grow: 1">1</div> <div style="flex-grow: 1">2</div> <div style="flex-grow: 4">3</div> </div>
CSS flex-shrink property defines if a flex item should shrink in comparision to the rest of the flex items. The value must be a number and the default value is 1.
<div class="flex"> <div>1</div> <div>2</div> <div style="flex-shrink: 0">3</div> </div>
CSS property flex-basis defines the initial length of a flex item.
<div class="flex"> <div>1</div> <div>2</div> <div style="flex-basis: 200px">3</div> </div>
CSS property flex is a shorthand property for CSS properties flex-grow, flex-shrink and flex-basis combined.
<div class="flex"> <div>1</div> <div>2</div> <div style="flex: 0 0 200px">3</div> <div>4</div> </div>
CSS property align-self defines the alignment for the flex item. It overrides the default alignment set by the container's align-items property.
<div class="flex"> <div>1</div> <div>2</div> <div style="align-self: center">3</div> <div>4</div> </div>
Leave a comment