<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: fivan18</title>
    <description>The latest articles on DEV Community by fivan18 (@fivan18).</description>
    <link>https://dev.to/fivan18</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F319032%2F95e2dd9c-c1a5-4e91-80da-d874ec6b8f55.jpeg</url>
      <title>DEV Community: fivan18</title>
      <link>https://dev.to/fivan18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fivan18"/>
    <language>en</language>
    <item>
      <title>How to build a Grid-based framework using the float property</title>
      <dc:creator>fivan18</dc:creator>
      <pubDate>Wed, 29 Jan 2020 20:41:24 +0000</pubDate>
      <link>https://dev.to/fivan18/how-to-build-a-grid-based-framework-using-the-float-property-2chf</link>
      <guid>https://dev.to/fivan18/how-to-build-a-grid-based-framework-using-the-float-property-2chf</guid>
      <description>&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/fivan18" rel="noopener noreferrer"&gt;
        fivan18
      &lt;/a&gt; / &lt;a href="https://github.com/fivan18/grid-based-framework" rel="noopener noreferrer"&gt;
        grid-based-framework
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This is a Bootstrap-like framework grid system. Built with CSS using the float technique and media queries.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Grid-based-framework&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;
    &lt;a rel="noopener noreferrer" href="https://github.com/fivan18/grid-based-frameworkscreenshots/grid.PNG"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Ffivan18%2Fgrid-based-frameworkscreenshots%2Fgrid.PNG"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;The framework's grid system uses rows and columns to layout content, it’s built with the float technique and uses media queries to gracefully degrade the site as the window size is reduced.&lt;/p&gt;

&lt;p&gt;It works like Twitter's Bootstrap framework with a litle difference: the breack points (col-breakPoint-cellsPerItem)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;col-0-4&lt;/li&gt;
&lt;li&gt;col-576-4&lt;/li&gt;
&lt;li&gt;col-720-4&lt;/li&gt;
&lt;li&gt;col-1024-4&lt;/li&gt;
&lt;li&gt;col-1200-4&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Live Demo&lt;/h2&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="https://fivan18.github.io/grid-based-framework/" rel="nofollow noopener noreferrer"&gt;https://fivan18.github.io/grid-based-framework/&lt;/a&gt;&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Page that is using the framework&lt;/h2&gt;
&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://fivan18.github.io/tnw-clone/" rel="nofollow noopener noreferrer"&gt;TNW-clone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/fivan18/tnw-clone" rel="noopener noreferrer"&gt;repository: https://github.com/fivan18/tnw-clone&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting Started&lt;/h2&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;Download &lt;a href="https://github.com/fivan18/grid-based-frameworkcss/framework.css" rel="noopener noreferrer"&gt;framework.css&lt;/a&gt; and &lt;a href="https://github.com/fivan18/grid-based-frameworkscripts/framework.js" rel="noopener noreferrer"&gt;framework.js&lt;/a&gt; files&lt;/li&gt;
&lt;li&gt;Add framework.css and framework.js files to your project&lt;/li&gt;
&lt;li&gt;Add a link tag inside the head that reference the framework.css file (&lt;a href="https://github.com/fivan18/grid-based-frameworkindex.html" rel="noopener noreferrer"&gt;like this&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Add two scritp tags rigth before the body-close tag (&lt;a href="https://github.com/fivan18/grid-based-frameworkindex.html" rel="noopener noreferrer"&gt;like this&lt;/a&gt;)
&lt;ol&gt;
&lt;li&gt;Set the src property of the first one to reference the framework.js file&lt;/li&gt;
&lt;li&gt;Add this code to the second one
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;
&lt;pre class="notranslate"&gt;&lt;code&gt;    function resize() {
        /*
            The code that you will put here will depend on the classes that
            you are using and the&lt;/code&gt;&lt;/pre&gt;…&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;
&lt;/div&gt;
&lt;br&gt;
  &lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/fivan18/grid-based-framework" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;As part of the fast track program at &lt;a href="https://www.microverse.org/" rel="noopener noreferrer"&gt;Microverse&lt;/a&gt; I started to learn HTML and CSS by cloning some pages. There was a project I learned about positioning and floating elements, it was no easy but approachable. In the subsequent projects, I decided to use the floating technique to position elements because that way I got a strong foundation of float property.&lt;/p&gt;

&lt;p&gt;The grid-based framework was one of those HTML and CSS's projects, it was actually the last one of this course. &lt;/p&gt;

&lt;p&gt;Well, I know that there are different techniques to position elements nowadays like Flexbox but I struggled the most using &lt;code&gt;float&lt;/code&gt; property, that's why I decided to use it to build this project. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to build the grid-based framework
&lt;/h2&gt;

&lt;p&gt;To figure out how to build the framework it was easier to use it in one of the projects that I worked with. So I chose the &lt;a href="https://github.com/fivan18/tnw-clone" rel="noopener noreferrer"&gt;TNW clone&lt;/a&gt;. It gave me the guideline.&lt;/p&gt;

&lt;p&gt;The next step was to refresh my knowledge about float and grids. These were the articles on which I relied:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://css-tricks.com/all-about-floats/" rel="noopener noreferrer"&gt;All About Floats&lt;/a&gt; by Chris Coyer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://css-tricks.com/dont-overthink-it-grids/" rel="noopener noreferrer"&gt;Don't Overthink It Grids&lt;/a&gt; by Chris Coyer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://alistapart.com/article/fluidgrids/" rel="noopener noreferrer"&gt;Fluid Grids&lt;/a&gt; by Ethan Marcotte&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Sections to be built with the framework
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://thenextweb.com/" rel="noopener noreferrer"&gt;TNW page&lt;/a&gt; has 5 sections with a similar behavior: each contains similar items that resize accordingly with the viewport (fluid grid) and expand in different proportions when the viewport reaches out a specific size (responsive design). Let's give it an example:&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://thenextweb.com/latest/" rel="noopener noreferrer"&gt;Latest news&lt;/a&gt; section has 8 similar items.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fepq97vad03tfmtjp7rau.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fepq97vad03tfmtjp7rau.png" alt="Latest News"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we resize the viewport, the size of each item will change according to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwrbtflimlpj7im05jdj5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwrbtflimlpj7im05jdj5.png" alt="Latest News Resize"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we reach out 1024 pixels (breakpoint), instead of four items per row it'll be 3 items per row (they expand in different proportions).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftpupuce5qgvroe1qishf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftpupuce5qgvroe1qishf.png" alt="Latest News Breakpoint"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Having all those things in mind I started to build the framework that fits my requirements. I thought about &lt;a href="https://getbootstrap.com/docs/4.4/layout/grid/" rel="noopener noreferrer"&gt;Bootstrap's Grid System&lt;/a&gt; and how easy it could be to clone the page using it. So, I decided to build a Bootstrap-like grid framework. &lt;/p&gt;

&lt;h3&gt;
  
  
  Basic grid-based framework
&lt;/h3&gt;

&lt;p&gt;Look at the code below and try to understand it. It illustrates the use of the grid-based framework. You can change the viewport size by clicking on the buttons that are at the bottom middle of the CodePen tool. The ones that have the next labels: 1x, 0.5x and 0.25x.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/fivan/embed/VwYVQXV?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As I said, it's a Bootstrap-like framework. It uses rows and columns to layout and aligns content. The main classes to layout the grid are &lt;code&gt;row&lt;/code&gt; and &lt;code&gt;col-{breakpoint}-{number of columns}&lt;/code&gt;. Where:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{breakpoint}&lt;/code&gt;: it's a number that represents the breakpoint in pixels. Grid breakpoints are based on minimum width media queries, meaning they apply to that one breakpoint and all those above it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{number of columns}&lt;/code&gt;: indicates the number of columns you’d like to use out of the possible 12 per row. &lt;/p&gt;

&lt;h4&gt;
  
  
  HTML layout
&lt;/h4&gt;

&lt;p&gt;As you saw, the example is basically a grid layout with six items:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="row well"&amp;gt;
  &amp;lt;div class="col-0-12 col-576-6 col-720-4 col-1024-3 col-1200-2"&amp;gt;
    &amp;lt;div class="well h"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;div class="col-0-12 col-576-6 col-720-4 col-1024-3 col-1200-2"&amp;gt;
    &amp;lt;div class="well h"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;div class="col-0-12 col-576-6 col-720-4 col-1024-3 col-1200-2"&amp;gt;
    &amp;lt;div class="well h"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;div class="col-0-12 col-576-6 col-720-4 col-1024-3 col-1200-2"&amp;gt;
    &amp;lt;div class="well h"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;div class="col-0-12 col-576-6 col-720-4 col-1024-3 col-1200-2"&amp;gt;
    &amp;lt;div class="well h"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;div class="col-0-12 col-576-6 col-720-4 col-1024-3 col-1200-2"&amp;gt;
    &amp;lt;div class="well h"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me explain a little bit about how the classes work in this case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;row&lt;/code&gt;: container of the columns. It is necessary to enclose the columns in a container with this class, I'll explain why later on in the article.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;col-0-12&lt;/code&gt;: the item will take 12 columns by default unless another column class is used.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;col-576-6&lt;/code&gt;: the item will take 6 columns when the viewport reaches out 576px and all the breakpoints above it unless another column class with a greater breakpoint is used.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;col-720-4&lt;/code&gt;: the item will take 4 columns when the viewport reaches out 720px and all the breakpoints above it unless another column class with a greater breakpoint is used.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;col-1024-3&lt;/code&gt;: the item will take 3 columns when the viewport reaches out 1024px and all the breakpoints above it unless another column class with a greater breakpoint is used.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;col-1200-2&lt;/code&gt;: the item will take 2 columns when the viewport reaches out 1200px and all the breakpoints above it unless another column class with a greater breakpoint is used.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  CSS stylesheet
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;.row&lt;/code&gt; is basically the container of the items. Due it has nothing but floated elements, the height of it literally collapses to nothing. You can read a really good explanation &lt;a href="https://css-tricks.com/all-about-floats/#article-header-id-3" rel="noopener noreferrer"&gt;here&lt;/a&gt;. So, we use the &lt;a href="https://css-tricks.com/all-about-floats/#article-header-id-4" rel="noopener noreferrer"&gt;Easy Clearing Method&lt;/a&gt; to deal with this situation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.row::after {
    content: ".";
    visibility: hidden;
    display: block;
    height: 0;
    clear: both;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.col-{breakpoints}-{number of columns}&lt;/code&gt; rule has nothing more than the &lt;code&gt;float: left&lt;/code&gt; and &lt;code&gt;width: XX.XX%&lt;/code&gt; properties setting in its body. That way we get a fluid-grid behavior.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  .col-XXXX-1 {
    float: left;
    width: 8.33%;
  }

  .col-XXXX-2 {
    float: left;
    width: 16.66%;
  }

  .col-XXXX-3 {
    float: left;
    width: 25%;
  }

  .col-XXXX-4 {
    float: left;
    width: 33.33%;
  }

  .col-XXXX-5 {
    float: left;
    width: 41.66%;
  }

  .col-XXXX-6 {
    float: left;
    width: 50%;
  }

  .col-XXXX-7 {
    float: left;
    width: 58.33%;
  }

  .col-XXXX-8 {
    float: left;
    width: 66.66%;
  }

  .col-XXXX-9 {
    float: left;
    width: 75%;
  }

  .col-XXXX-10 {
    float: left;
    width: 83.33%;
  }

  .col-XXXX-11 {
    float: left;
    width: 91.66%;
  }

  .col-XXXX-12 {
    float: left;
    width: 100%;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make the grid responsive we only need to put the code above inside a media query, replacing &lt;code&gt;XXXX&lt;/code&gt; with the corresponding breakpoint. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@media screen and (min-width: 576px) {
    .col-576-1 {
      float: left;
      width: 8.33%;
    }

    .col-576-2 {
      float: left;
      width: 16.66%;
    }

    .col-576-3 {
      float: left;
      width: 25%;
    }

    .col-576-4 {
      float: left;
      width: 33.33%;
    }

    .col-576-5 {
      float: left;
      width: 41.66%;
    }

    .col-576-6 {
      float: left;
      width: 50%;
    }

    .col-576-7 {
      float: left;
      width: 58.33%;
    }

    .col-576-8 {
      float: left;
      width: 66.66%;
    }

    .col-576-9 {
      float: left;
      width: 75%;
    }

    .col-576-10 {
      float: left;
      width: 83.33%;
    }

    .col-576-11 {
      float: left;
      width: 91.66%;
    }

    .col-576-12 {
      float: left;
      width: 100%;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding gutters
&lt;/h3&gt;

&lt;p&gt;I used &lt;code&gt;padding&lt;/code&gt; and &lt;code&gt;box-sizing&lt;/code&gt; properties to add the gutters. Let's take a look at the next example, it's almost the same as the example above with some rules added in the stylesheet.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/fivan/embed/xxbNXEN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Stylesheet
&lt;/h4&gt;

&lt;p&gt;The items (&lt;code&gt;col-{breakpoints}-{number of columns}&lt;/code&gt;) and its container (&lt;code&gt;row&lt;/code&gt;) need to include the &lt;code&gt;padding&lt;/code&gt; size in their width. We achieve this by using the next CSS rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* {
  box-sizing: border-box;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to set the &lt;code&gt;padding-right&lt;/code&gt; and &lt;code&gt;padding-top&lt;/code&gt; properties in each of the grid items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[class*="col-"] {
  padding-right: 20px;
  padding-top: 20px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, to include the gutters that are missing on the left and bottom of the container we set the corresponding &lt;code&gt;padding&lt;/code&gt; properties to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.row {
  padding-left: 20px;
  padding-bottom: 20px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, that's it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Well, this is a basic explanation to build a grid-based framework using the &lt;code&gt;float&lt;/code&gt; property. It helped a lot to understand this technique to position elements. &lt;/p&gt;

&lt;p&gt;In the example that I illustrated, the items in the grid have the same &lt;code&gt;height&lt;/code&gt; value and it works really well. But, what if we have different values for each item? Yes, there is a problem when it happens. There is a peculiar behavior with the floating elements that cause the grid layout breaks. I'll explain it in another article and the way how I solved it.&lt;/p&gt;

&lt;p&gt;To be continued...&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
