{"id":2437,"date":"2018-11-02T20:25:27","date_gmt":"2018-11-03T01:25:27","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2437"},"modified":"2018-11-02T21:25:51","modified_gmt":"2018-11-03T02:25:51","slug":"html-css-how-to-understand-the-basics-flexbox-for-responsive-web-design","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/11\/02\/html-css-how-to-understand-the-basics-flexbox-for-responsive-web-design\/","title":{"rendered":"HTML &#038; CSS: How to understand the basics of Flexbox for responsive web design"},"content":{"rendered":"<p>The CSS3 Flexbox Grid system is based on the idea of a parent\/child container relationship. The parent, if we call it &#8220;row&#8221; needs to have &#8220;display: flex;&#8221;. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">\/* Parent *\/\r\n.row { display: flex; }<\/pre>\n<p>The child, if we give it a class name of &#8220;col&#8221;, should have &#8220;flex: 1;&#8221;. The 1 here means 100% width of the container. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">\/* Child *\/\r\n.col { flex: 1; }<\/pre>\n<p>Once those values are set in the CSS, we can set the following in the HTML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"row\"&gt;\r\n  &lt;div class=\"col\"&gt;col&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>This will produce one column that spans the entire width of the row container. All we need to do to get more columns is add more cols tags. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"row\"&gt;\r\n  &lt;div class=\"col\"&gt;1 column 100% width&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div class=\"row\"&gt;\r\n  &lt;div class=\"col\"&gt;2 columns 50% each&lt;\/div&gt;\r\n  &lt;div class=\"col\"&gt;2 columns 50% each&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div class=\"row\"&gt;\r\n  &lt;div class=\"col\"&gt;3 columns 33% each&lt;\/div&gt;\r\n  &lt;div class=\"col\"&gt;3 columns 33% each&lt;\/div&gt;\r\n  &lt;div class=\"col\"&gt;3 columns 33% each&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div class=\"row\"&gt;\r\n  &lt;div class=\"col\"&gt;4 columns 25% each&lt;\/div&gt;\r\n  &lt;div class=\"col\"&gt;4 columns 25% each&lt;\/div&gt;\r\n  &lt;div class=\"col\"&gt;4 columns 25% each&lt;\/div&gt;\r\n  &lt;div class=\"col\"&gt;4 columns 25% each&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>The results can be seen in this codepen:<\/p>\n<p class='codepen'  data-height='362' data-theme-id='0' data-slug-hash='rQNebY' data-default-tab='html,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Flexbox 1 by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p><strong>Note:<\/strong><br \/>\nIf you expand or contract the width of the html browser window, you will see that each column expands or contracts responsively.<\/p>\n<p>Now where Flexbox really shines is how it easily allows you to take care of mobile screens. This can be accomplished by using @media queries. For example, we can have small screens that are only 768 pixels wide show our content in a stacked row fashion instead of the complex layout we designed for a larger screen. Here is what the @media rule would look like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">\/* iPad screen size *\/\r\n@media screen and (min-width: 768px){\r\n  .row {\r\n\tdisplay: flex;\r\n\tflex-direction: row;\r\n  }\r\n}<\/pre>\n<p>Then in our CSS .row definition, add &#8220;flex-direction: column;&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">\/* Parent *\/\r\n.row { display: flex; flex-direction: column; }<\/pre>\n<p>Here is an example of what it looks like if we use a min-width of 500px and our screen is resized to less than 500 pixels:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2446\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-500px.png\" alt=\"\" width=\"438\" height=\"553\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-500px.png 820w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-500px-237x300.png 237w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-500px-768x970.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-500px-811x1024.png 811w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-500px-676x854.png 676w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/p>\n<p>And here is what that same Flexbox grid looks like when it is 501 pixels wide:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2447\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-501px.png\" alt=\"\" width=\"439\" height=\"277\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-501px.png 826w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-501px-300x190.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-501px-768x485.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/media-501px-676x427.png 676w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/><\/p>\n<p>For more information about Flexbox, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/css\/css3_flexbox.asp\">https:\/\/www.w3schools.com\/css\/css3_flexbox.asp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The CSS3 Flexbox Grid system is based on the idea of a parent\/child container relationship. The parent, if we call it &#8220;row&#8221; needs to have &#8220;display: flex;&#8221;. For example: \/* Parent *\/ .row { display: flex; } The child, if we give it a class name of &#8220;col&#8221;, should have &#8220;flex: 1;&#8221;. The 1 here &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/11\/02\/html-css-how-to-understand-the-basics-flexbox-for-responsive-web-design\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HTML &#038; CSS: How to understand the basics of Flexbox for responsive web design<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[181,10,113,105,182],"class_list":["post-2437","post","type-post","status-publish","format-standard","hentry","category-html-css","tag-media","tag-css","tag-flexbox","tag-html","tag-responsive"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2437"}],"version-history":[{"count":13,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2437\/revisions"}],"predecessor-version":[{"id":2457,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2437\/revisions\/2457"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}