{"id":2487,"date":"2018-11-07T16:46:25","date_gmt":"2018-11-07T21:46:25","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2487"},"modified":"2018-11-07T16:46:25","modified_gmt":"2018-11-07T21:46:25","slug":"html-css-four-different-ways-to-expand-a-div-to-100-width-using-grid","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/11\/07\/html-css-four-different-ways-to-expand-a-div-to-100-width-using-grid\/","title":{"rendered":"HTML &#038; CSS: Four different ways to expand a div to 100% width using Grid"},"content":{"rendered":"<p>Let&#8217;s say we set up a basic web page layout that has 5 sections: Title, Header, Sidebar, Content, and Footer.\u00a0 This can be easily laid out using Grid, where we have an overall container that has five child divs. For example:<\/p>\n<p class='codepen'  data-height='300' data-theme-id='0' data-slug-hash='xQZXRv' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Grid row expand by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<p>The HTML looks like this:<\/p>\n<p>Note that each div in the grid has already been given its own class name corresponding to its section of the page.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"grid\"&gt;\r\n  &lt;div class=\"title\"&gt;title&lt;\/div&gt;\r\n  &lt;div class=\"header\"&gt;header&lt;\/div&gt;\r\n  &lt;div class=\"sidebar\"&gt;sidebar&lt;\/div&gt;\r\n  &lt;div class=\"content\"&gt;content&lt;\/div&gt;\r\n  &lt;div class=\"footer\"&gt;footer&lt;\/div&gt;  \r\n&lt;\/div&gt;<\/pre>\n<p>And I have already set up the basic grid, with 2 columns (see line 3 in the code). There are two columns, but 5 sections, so you can see that there are five distinct boxes in this grid. Here is the set up Grid:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.grid {\r\n  display: grid;\r\n  grid-template-columns: 1fr 1fr;\r\n  grid-gap: 15px;\r\n}\r\n\r\n.grid div {\r\n  color: white;\r\n  font-family: Roboto;\r\n  font-size: 18px;\r\n  text-align: center;\r\n  align-self: center;\r\n  padding: 10px;\r\n}\r\n\r\n.grid div:nth-child(even){\r\n  background-color: #2196F3;\r\n}\r\n\r\n.grid div:nth-child(odd){\r\n  background-color: #90A4AE;\r\n}<\/pre>\n<p>Note about the set up: I used &#8220;.grid div&#8221; to add styling to the divs that are part of the grid, and I used &#8220;.grid div:nth-child(even)&#8221; and &#8220;.grid div:nth-child(odd)&#8221; to color the boxes.<\/p>\n<p>In this article I will detail four different ways we can make the child divs expand to cover two columns.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Method #1: grid-column-start and grid-column-end<\/strong><\/h4>\n<p>Create a style for the title class and give it a grid-column-start of 1 and a grid-column-end of 3. The 1 means start at the very left edge of the grid, and the 3 means the very right edge of the grid (because the grid only has two columns).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.title {\r\n  grid-column-start: 1;\r\n  grid-column-end: 3;\r\n}<\/pre>\n<p class='codepen'  data-height='300' data-theme-id='0' data-slug-hash='MzKEmz' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Grid row expand \u2013 1 by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<h4><strong>Method #2: grid-column: 1 \/ 3;<br \/>\n<\/strong><\/h4>\n<p>For this second method, I am going to create a style for the footer class so that it also expands to cover both columns.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.footer {\r\n  grid-column: 1 \/ 3;\r\n}<\/pre>\n<p>This method does the same thing as the first method, but with only 1 line of code.<\/p>\n<p class='codepen'  data-height='300' data-theme-id='0' data-slug-hash='yQezbR' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Grid row expand \u2013 2 by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<h4><strong>Method #3: grid-column-end: span 2;<br \/>\n<\/strong><\/h4>\n<p>For this third method, I am going to create a style for the header class so that it also expands to cover both columns. This method uses &#8220;grid-column-end again, but just sets a span of 2 columns:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.header {\r\n  grid-column-end: span 2;\r\n}<\/pre>\n<p class='codepen'  data-height='300' data-theme-id='0' data-slug-hash='XyXzVN' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Grid row expand \u2013 3 by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<h4><strong>Method #4: grid-area &amp; grid-template-areas<\/strong><\/h4>\n<p>This fourth method takes advantage of the class names that were assigned to each child element of the grid. For this method, I will undo the code used in the previous methods and create a CSS section for each of the child classes and define their grid areas. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.title {\r\n  grid-area: title;\r\n}\r\n\r\n.footer {\r\n  grid-area: footer;\r\n}\r\n\r\n.header {\r\n  grid-area: header;\r\n}\r\n\r\n.sidebar {\r\n  grid-area: sidebar;\r\n}\r\n\r\n.content {\r\n  grid-area: content;\r\n}<\/pre>\n<p>Notice that each of these grid areas are defined without quotes.<\/p>\n<p>Then in the grid section of the CSS, I will add a new line called &#8220;grid-template-areas&#8221; where I use the area names to define the layout of the grid. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.grid {\r\n  display: grid;\r\n  grid-template-columns: 1fr 1fr;\r\n  grid-gap: 15px;\r\n  grid-template-areas: \r\n    \"title title\"\r\n    \"header header\"\r\n    \"content sidebar\"\r\n    \"footer footer\"\r\n }<\/pre>\n<p class='codepen'  data-height='300' data-theme-id='0' data-slug-hash='rQxYqj' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Grid row expand \u2013 4 by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<p>This is perhaps the most powerful method of the four. Basically, after the colon, you can use a new line and within double quotes that represent each row, you can set the area names that you want to appear, as you would like to lay them out.<\/p>\n<p>Notice that I changed the order of the content and sidebar sections from how they are laid out in the HTML simply by specifying &#8220;content&#8221; before &#8220;sidebar&#8221; in the third row. This is very powerful because I could use this to totally rearrange a page simply by reordering the areas in the double quotes. For example, footer could be moved to the top and header to the bottom this way.<\/p>\n<p>&nbsp;<\/p>\n<p>For more about CSS Grids, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/cssref\/pr_grid.asp\">https:\/\/www.w3schools.com\/cssref\/pr_grid.asp<\/a><br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/CSS_Grid_Layout\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/CSS_Grid_Layout<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say we set up a basic web page layout that has 5 sections: Title, Header, Sidebar, Content, and Footer.\u00a0 This can be easily laid out using Grid, where we have an overall container that has five child divs. For example: &nbsp; The HTML looks like this: Note that each div in the grid has &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/11\/07\/html-css-four-different-ways-to-expand-a-div-to-100-width-using-grid\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HTML &#038; CSS: Four different ways to expand a div to 100% width using Grid<\/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":[10,140,187,186,105],"class_list":["post-2487","post","type-post","status-publish","format-standard","hentry","category-html-css","tag-css","tag-grid","tag-grid-area","tag-grid-template-areas","tag-html"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2487","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=2487"}],"version-history":[{"count":17,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2487\/revisions"}],"predecessor-version":[{"id":2565,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2487\/revisions\/2565"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}