{"id":96,"date":"2025-03-21T17:59:20","date_gmt":"2025-03-21T17:59:20","guid":{"rendered":"https:\/\/kingstatue.com\/?p=96"},"modified":"2025-03-21T17:59:20","modified_gmt":"2025-03-21T17:59:20","slug":"css-property-display-flex","status":"publish","type":"post","link":"https:\/\/www.kingstatue.com\/?p=96","title":{"rendered":"CSS &#8211; Property display: flex"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Flex box layout<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.container{<br>    <code>display: flex;<br>    justify-content: center;<br>    align-items: center;<\/code><br><code>}<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. <code>display: flex;<\/code><\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Enables <strong>Flexbox<\/strong> for the element.<\/li>\n\n\n\n<li>Makes child elements (flex items) automatically adjust based on the flex container&#8217;s rules.<\/li>\n\n\n\n<li>Allows better alignment and spacing control compared to traditional methods like <code>float<\/code> or <code>inline-block<\/code>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. <code>justify-content: center;<\/code><\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Aligns child elements <strong>horizontally<\/strong> in the <strong>center<\/strong> of the flex container.<\/li>\n\n\n\n<li>Works along the <strong>main axis<\/strong> (default is left to right).<\/li>\n\n\n\n<li>Example of different values:\n<ul class=\"wp-block-list\">\n<li><code>flex-start<\/code> \u2192 Aligns items to the <strong>left<\/strong>.<\/li>\n\n\n\n<li><code>flex-end<\/code> \u2192 Aligns items to the <strong>right<\/strong>.<\/li>\n\n\n\n<li><code>space-between<\/code> \u2192 Places items with <strong>equal space<\/strong> between them.<\/li>\n\n\n\n<li><code>space-around<\/code> \u2192 Gives <strong>equal space<\/strong> around items.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. <code>align-items: center;<\/code><\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Aligns child elements <strong>vertically<\/strong> in the <strong>center<\/strong> of the flex container.<\/li>\n\n\n\n<li>Works along the <strong>cross axis<\/strong> (default is top to bottom).<\/li>\n\n\n\n<li>Example of different values:\n<ul class=\"wp-block-list\">\n<li><code>flex-start<\/code> \u2192 Aligns items to the <strong>top<\/strong>.<\/li>\n\n\n\n<li><code>flex-end<\/code> \u2192 Aligns items to the <strong>bottom<\/strong>.<\/li>\n\n\n\n<li><code>stretch<\/code> \u2192 Stretches items to <strong>fill the container<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Centering a Box Inside a Container<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.container {<br>    display: flex;<br>    justify-content: center;<br>    align-items: center;<br>    width: 300px;<br>    height: 200px;<br>    background-color: lightgray;<br>}<br><br>.box {<br>    width: 100px;<br>    height: 50px;<br>    background-color: blue;<br>    color: white;<br>    text-align: center;<br>}<br><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;div class=\"container\"><br>    &lt;div class=\"box\">Centered&lt;\/div><br>&lt;\/div><br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How It Works?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.container<\/code> becomes a <strong>flexbox<\/strong>.<\/li>\n\n\n\n<li><code>.box<\/code> is positioned <strong>horizontally &amp; vertically centered<\/strong> inside <code>.container<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When you apply <code>display: flex;<\/code> to an element (e.g., <code>.container<\/code>), it <strong>turns into a flex container<\/strong>, and its <strong>direct child elements<\/strong> become <strong>flex items<\/strong>. This means:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The container <strong>gains control<\/strong> over how its child elements are arranged.<\/li>\n\n\n\n<li>The child elements <strong>align and distribute<\/strong> dynamically based on Flexbox properties.<\/li>\n\n\n\n<li>The container follows <strong>a main axis and a cross axis<\/strong> for positioning items.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Flex Container (<code>display: flex;<\/code>)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>display: flex;<\/code> is applied to <code>.container<\/code>, it becomes a <strong>flex container<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Before applying Flexbox:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Child elements behave as <strong>block<\/strong> or <strong>inline<\/strong> elements.<\/li>\n\n\n\n<li>They stack <strong>vertically<\/strong> or flow <strong>inline<\/strong> based on default behavior.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>After applying Flexbox:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Child elements (<code>.box<\/code>, etc.) behave as <strong>flex items<\/strong>.<\/li>\n\n\n\n<li>You can control <strong>alignment, spacing, wrapping, and ordering<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Flex Items (Child Elements)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Any direct children inside the <code>.container<\/code> automatically become <strong>flex items<\/strong>.<br>These items respond to <strong>flex properties<\/strong> such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>justify-content<\/code> (Horizontal alignment)<\/li>\n\n\n\n<li><code>align-items<\/code> (Vertical alignment)<\/li>\n\n\n\n<li><code>flex-wrap<\/code>, <code>flex-grow<\/code>, <code>order<\/code>, etc.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Flexbox Works on Two Axes<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>.container<\/code> is set to <code>display: flex;<\/code>, it follows <strong>two axes<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Main Axis (Primary Direction of Items)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>By default, it&#8217;s left to right (row-wise).<\/strong><\/li>\n\n\n\n<li>Controlled by <code>justify-content<\/code>.<\/li>\n\n\n\n<li>Can be changed using <code>flex-direction: column;<\/code> (to make items stack vertically).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Cross Axis (Perpendicular to Main Axis)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>By default, it&#8217;s top to bottom.<\/strong><\/li>\n\n\n\n<li>Controlled by <code>align-items<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example: Centering a Box Using Flexbox<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.container {<br>    display: flex;<br>    justify-content: center;<br>    align-items: center;<br>    width: 400px;<br>    height: 300px;<br>    background-color: lightgray;<br>}<br><br>.box {<br>    width: 100px;<br>    height: 50px;<br>    background-color: blue;<br>    color: white;<br>    text-align: center;<br>    line-height: 50px;<br>}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;div class=\"container\"><br>    &lt;div class=\"box\">Centered&lt;\/div><br>&lt;\/div><br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How It Works?<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>.container<\/code> <strong>becomes a flexbox<\/strong> (<code>display: flex;<\/code>).<\/li>\n\n\n\n<li><code>.box<\/code> <strong>becomes a flex item<\/strong>.<\/li>\n\n\n\n<li><code>justify-content: center;<\/code> moves <code>.box<\/code> <strong>horizontally<\/strong> to the center.<\/li>\n\n\n\n<li><code>align-items: center;<\/code> moves <code>.box<\/code> <strong>vertically<\/strong> to the center.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Before vs. After Flexbox<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd34 Without Flexbox (Default Behavior)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.box<\/code> follows normal document flow.<\/li>\n\n\n\n<li>To center it, you\u2019d need <strong>complex CSS (margin, positioning, etc.).<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udfe2 With Flexbox<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.box<\/code> automatically aligns to the center.<\/li>\n\n\n\n<li><strong>Less code, more flexibility.<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Benefits of Flexbox<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u2714 Simple way to align elements (centering, spacing, distribution).<br>\u2714 Responsive layouts without media queries.<br>\u2714 Works well for dynamic content.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>When to Use This?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Centering elements easily<\/strong> (e.g., buttons, text, images).<\/li>\n\n\n\n<li><strong>Creating responsive layouts<\/strong> without using complex positioning techniques.<\/li>\n\n\n\n<li><strong>Aligning multiple items<\/strong> inside a container.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Flex box layout. .container{ display: flex; justify-content: center; align-items: center;} 1. display: flex; 2. justify-content: center; 3. align-items: center; Example: Centering a Box Inside a Container .container { display: flex;&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-cascading-style-sheet"],"_links":{"self":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=96"}],"version-history":[{"count":0,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kingstatue.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}