CSS Flexbox Layout Last Updated : 26 Sep, 2024 Comments Improve Suggest changes Like Article Like Report CSS Flexbox is a modern layout system to simplifies the arrangement of elements within a container. It allows elements to adjust and distribute space dynamically. If we use Flexbox in our webpage then it will also help us with responsive designs.These are the following approaches for achieving CSS Flexbox Layout:Table of ContentCSS Flexbox PropertiesUsing Justify ContentUsing Align ItemsUsing Flex DirectionCSS Flexbox PropertiesValueDescriptiondisplay: flexConverts an element into a flex container.justify-contentAligns flex items along the main axis.align-itemsAlign the flex items along the cross-axis to create created container cross-axis container items.flex-directionDefines the direction of the main axis (row, column, reverse).flex-wrapControls whether items should wrap to the next line.align-selfAllows individual items to override align items, for vertical alignment.Using Justify ContentAt first,cross-axis create align a basic HTML structure.After that create a div with a class name "container" to wrap the flexbox layout. Inside this container, create three child div elements with the class name "item", which will represent the flexbox items.After that, inside the style tag, we will define the CSS needed for the Flexbox layout and use the "justify-content: space-between" property toon put the items in the main axis. Syntax:justify-content: space-between;Example: This example shows the implementation of the above approach. HTML <!-- index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>CSS Flexbox Layout Examples</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f4f4f4; } .example-container { width: 50%; background-color: #fff; border: 2px solid #ccc; margin-bottom: 30px; padding: 20px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .example-heading { text-align: center; margin-bottom: 20px; font-size: 24px; color: #333; } .container { display: flex; justify-content: space-between; border: 2px solid #ccc; padding: 10px; } .item { background-color: #4CAF50; color: white; padding: 20px; text-align: center; width: 100px; } </style> </head> <body> <div class="example-container"> <div class="example-heading">Justify Content: space-between</div> <div class="container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> </div> </div> </body> </html> Output:Using Justify ContentUsing Align ItemsFirst , create a basic HTML structure.Then inside the container, creates the three "div" elements (with the createsclass item) of different heights to showcase the effect of the "align-items" property.After that, inside the style tag, we will define the CSS needed for the Flexbox layout and use the "align-items: stretch" property to align the flex items along the cross-axis.Syntax:align-items: stretch;Example: This example shows the implementation of the using align-items, HTML <!-- index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>CSS Flexbox Layout Examples</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f4f4f4; } .example-container { width: 50%; background-color: #fff; border: 2px solid #ccc; margin-bottom: 30px; padding: 20px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .example-heading { text-align: center; margin-bottom: 20px; font-size: 24px; color: #333; } .container { display: flex; align-items: stretch; height: 200px; border: 2px solid #ccc; padding: 10px; } .item { background-color: #FF5722; color: white; padding: 10px; text-align: center; width: 100px; } .item-1 { height: 50px; } .item-2 { height: 100px; } .item-3 { height: 150px; } </style> </head> <body> <div class="example-container"> <div class="example-heading">Align Items: stretch</div> <div class="container"> <div class="item item-1">Item 1</div> <div class="item item-2">Item 2</div> <div class="item item-3">Item 3</div> </div> </div> </body> </html> Output:Using Align ItemsUsing Flex DirectionCreated container cross-axis container in a basic HTML structure.After that add three "div" elements inside the container to represent flex items. And put the class name "item" for all the divs.After that, inside the style tag, we will define the CSS needed for the Flexbox layout and use the "display: flex" property.Example: This example shows the implementation of the above approach. HTML <!-- index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>CSS Flexbox Layout Examples</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f4f4f4; } .example-container { width: 25%; background-color: #fff; border: 2px solid #ccc; margin-bottom: 30px; padding: 20px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .example-heading { text-align: center; margin-bottom: 20px; font-size: 24px; color: #333; } .container { display: flex; flex-direction: column; border: 2px solid #ccc; padding: 10px; } .item { background-color: #2196F3; color: white; padding: 10px; text-align: center; margin: 10px 0; } </style> </head> <body> <div class="example-container"> <div class="example-heading">Flex Direction: column</div> <div class="container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> </div> </div> </body> </html> Output:Using Flex Direction Comment More infoAdvertise with us Next Article CSS Flexbox Layout skaftafh Follow Improve Article Tags : Web Technologies CSS Similar Reads What is CSS flexbox ? CSS Flexible Layout Box, popularly known as Flexbox is a powerful one-dimensional layout model. It helps to lay, align and distribute items (children) efficiently inside a container (parent).  Important Features:One-dimensional layout model: Flex is a one-dimensional layout model as it can only deal 15+ min read Advance CSS Layout with Flexbox Flexbox is a powerful CSS layout model that simplifies the creation of flexible and responsive layouts. It allows you to align and distribute space among items within a container, making complex designs easier to manage.Flexbox is particularly useful for building responsive designs that adapt seamle 4 min read Flexbox-Layout in Android Before playing, let's learn about what FlexBox actually is. Hey, do you know the front-end also? If yes, then you should know what actually flexbox is in CSS. To learn more about CSS Flexible Box Layout Module click here. And if not, then don't worry; here is the explanation: Here the word "Flex" 5 min read Tachyons Layout Flexbox Tachyons is a toolkit that is used to create a responsive website. In this article, we will learn about how to make a flex layout using the Tachyons toolkit. Flexbox is used to achieve powerful horizontal or vertical layouts without JavaScript. Syntax: <element-name class="flex">...</elemen 3 min read Introduction to CSS Flexbox CSS Flexbox, short for the Flexible Box Layout module, is a powerful layout tool designed to simplify web page layouts by arranging items in rows or columns with ease.Flexbox eliminates the need for floats or complex positioning, enabling responsive and dynamic layouts.It aligns items efficiently, d 4 min read Primer CSS Flexbox Primer CSS is a free open-source CSS framework based on principles that set the foundation for basic design elements like spacing, typeface, and color. This rigorous approach ensures that our patterns are consistent and interoperable.Primer CSS Flexbox:Flex container: Flex Container is used to make 6 min read Foundation CSS Flexbox Mode Foundation CSS is an open-source and responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing & can be accessible to any device. It is used by many companies such as Facebook, eBay, M 8 min read Foundation CSS Flexbox Utilities Foundation CSS is an open-source and responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing and can be accessible to any device. Flexbox Utilities: In Foundation CSS, Flexbox classes a 3 min read CSS flex Property The CSS flex property is used for controlling the size and behavior of items within a flexible container. It combines three key propertiesâflex-grow, flex-shrink, and flex-basisâwhich determine how items grow, shrink, and set their initial size relative to the available space. This property is essen 4 min read Primer CSS Flexbox Order Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Primer CSS Flexbox Order classes are 3 min read Like