{"id":1661,"date":"2018-03-01T15:48:06","date_gmt":"2018-03-01T20:48:06","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1661"},"modified":"2019-04-29T08:49:46","modified_gmt":"2019-04-29T13:49:46","slug":"html-css-create-square-grid-using-css-grid","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/03\/01\/html-css-create-square-grid-using-css-grid\/","title":{"rendered":"HTML &#038; CSS: How to create a square grid using CSS grid"},"content":{"rendered":"<p>In order to create a game of tic-tac-toe using React.js, I first needed to figure out how to create a 3&#215;3 grid of squares using HTML and CSS. Turns out, with CSS grid it is fairly straightforward.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1662\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/03\/3x3grid.png\" alt=\"\" width=\"290\" height=\"292\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/03\/3x3grid.png 290w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/03\/3x3grid-150x150.png 150w\" sizes=\"auto, (max-width: 290px) 100vw, 290px\" \/><\/p>\n<p>All we need is three CSS classes:<\/p>\n<ol>\n<li><strong>container<\/strong>: This is the highest level div that will contain the grid. It needs to have a background color that will be used as the border color between cells.<\/li>\n<li>\u00a0<strong>grid<\/strong>: This is where the grid magic happens. grid-gap is used to &#8220;reveal&#8221; the background color and therefore create the cell borders.<\/li>\n<li><strong>\u00a0cell<\/strong>: The cell class defines the size and placement of the font.<\/li>\n<\/ol>\n<p>Here is what the HTML looks like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"container\"&gt;\r\n  &lt;div class=\"grid\"&gt;\r\n    &lt;div class=\"cell\" id=\"1\"&gt;1&lt;\/div&gt;\r\n    &lt;div class=\"cell\" id=\"2\"&gt;2&lt;\/div&gt;\r\n    &lt;div class=\"cell\" id=\"3\"&gt;3&lt;\/div&gt;\r\n    &lt;div class=\"cell\" id=\"4\"&gt;4&lt;\/div&gt;\r\n    &lt;div class=\"cell\" id=\"5\"&gt;5&lt;\/div&gt;\r\n    &lt;div class=\"cell\" id=\"6\"&gt;6&lt;\/div&gt;\r\n    &lt;div class=\"cell\" id=\"7\"&gt;7&lt;\/div&gt;\r\n    &lt;div class=\"cell\" id=\"8\"&gt;8&lt;\/div&gt;\r\n    &lt;div class=\"cell\" id=\"9\"&gt;9&lt;\/div&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>Here is the CSS:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.container {\r\n  background: black;\r\n  display: inline-block;\r\n  border: 5px solid black;\r\n}\r\n\r\n.grid {\r\n  display: grid;\r\n  grid-template-columns: repeat(3, 80px);\r\n  grid-template-rows: repeat(3, 80px);\r\n  grid-gap: 5px;\r\n}\r\n\r\n.cell {\r\n   \/* center the cell content *\/\r\n  justify-content: center;\r\n  align-items: center;\r\n  display: flex;\r\n  font-family: Arial;\r\n  font-size: 3rem;\r\n  font-weight: bold;\r\n  background: white;\r\n}<\/pre>\n<p>Important CSS details that make this work:<\/p>\n<ul>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">display: inline-block;<\/code>in the container class.<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">display: grid;<\/code>in the grid class.<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">grid-template-columns: repeat(3, 80px);<\/code>in the grid class sets up the columns.<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">grid-template-rows: repeat(3, 80px);<\/code>in the grid class sets up the rows.<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">grid-gap: 5px;<\/code>in the grid class sets up gaps (borders) between the cells.<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">display: flex;<\/code>in the cell class.<\/li>\n<\/ul>\n<p>If any one of the above details are missing, then the grid won&#8217;t work.<\/p>\n<p>Here is what the grid looks like on Codepen:<\/p>\n<p class='codepen'  data-height='360' data-theme-id='0' data-slug-hash='vdPLvo' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen CSS 3\u00d73 square grid flexbox solution by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>In order to create a game of tic-tac-toe using React.js, I first needed to figure out how to create a 3&#215;3 grid of squares using HTML and CSS. Turns out, with CSS grid it is fairly straightforward. All we need is three CSS classes: container: This is the highest level div that will contain the &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/03\/01\/html-css-create-square-grid-using-css-grid\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HTML &#038; CSS: How to create a square grid using CSS 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,105],"class_list":["post-1661","post","type-post","status-publish","format-standard","hentry","category-html-css","tag-css","tag-grid","tag-html"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1661","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=1661"}],"version-history":[{"count":8,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1661\/revisions"}],"predecessor-version":[{"id":2567,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1661\/revisions\/2567"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}