Monday, October 16, 2017

Building a Responsive Grid System Using CSS: Part 3

In the last post, we discussed the basic idea behind grid system. Also we talked about the concept of CSS at-rules and we have said that we will be using the '@media' rule to make the grid system responsive. In this post, we will start by building the grid system.

One thing to keep in mind that in all the rules that we will be creating, we will use percentage for content width, padding and margins. The grid system that we will create will have 12 columns. This means that the smallest column width will be 100/12 = 8.33% and the maximum will be 100%. The given values will change later when we apply margins to the columns and rows.

Creating Required Selectors

Our grid system will have the following components:
  • A Container
  • A Row
  • 12 columns
This means we have to define 14 main class selectors. In addition to the 14 selectors, we will include the one that we have used to change the property “box-sizing” of All HTML elements from part 1 of the series.

CSS Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

*, *::before,*::after{
    box-sizing:border-box;
}

.container{}

.row{}

.col-1{}

.col-2{}

.col-3{}

.col-4{}

.col-5{}

.col-6{}

.col-7{}

.col-8{}

.col-9{}

.col-10{}

.col-11{}

.col-12{}

Creating Test HTML Page

Now we have the needed selectors, we will start writing the needed rules inside each one. But before we do that, let’s create HTML document that we will be testing our grid system in. The page will have 3 sections, header, footer and a body. Each component of the 3 will be represented as a row within a container similar to the next image.


The HTML code for the given layout can be found below.

HTML Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<html>
    <head>
        <title>Page Title</title>
        <meta charset="UTF-8">
        <!--The CSS file that will contain the grid system-->
        <link href="grid-system.css" rel="stylesheet">
    </head>
    <body>
        <div class="container">
            <!--Page Header-->
            <div class="row">
                Header
            </div>
            <!--Page Body-->
            <div class="row">
                Body
            </div>
            <!--Page Footer-->
            <div class="row">
                Footer
            </div>
        </div>
    </body>
</html>

One thing we have included is the CSS file that we will be writing the code for the grid system in. In our case, we have given it the name "grid-system.css". When we view the given HTML document in a web browser, we will see the following:


Inside the header row, we will have other components such as search bar, breadcrumb and page title. The page body row will contain the content that the user will be interested in viewing. The footer usually contains links to pages such as “About” and “Terms of Service”.


Working With the Row and Container Selectors

As we have seen, When we view the test document in web browser we will see a blank page with only the text we have added. To make the container and row visible, we will add colors to them using CSS.

CSS Code

1 2 3 4 5 6 7 8

.container{
    background-color: #672E3B;
}

.row{
    background-color: #92B558;
}

After setting background colors, the only thing that we will see is the color of rows. The reason for that is the container will be behind the rows. To see the container, we have to specify the value of padding property within the container. Let’s set it to 1%.

CSS Code

1 2 3 4 5

.container{
    background-color: #672E3B;
    padding:1%;
}
When we apply the given CSS rules, we will be able to see the rows and the container.


One issue we can notice is that the rows look like as if they were connected. To fix that, we have to specify the value of the property margin for the row. Let’s make the margin 0.5%.

CSS Code

1 2 3 4 5

.row{
    background-color: #92B558;
    margin:0.5%;
}
Once we do that, the rows will be separated from each other.


When we look at the whole web page in the browser window, we can see that the page will fit without any issues. Even if we resize the whole window, we can see that the page will fit. If everything is fine, why do we have to bother our self with creating grid system? Well, a simple answer to that question is we will not see the difference unless we have many other elements in the page such as images, menus, search boxes and others. In the given page, we have only text which cannot affect the layout of the page that much. Also when we try the page on a smartphone, we might notice a difference.

Now let’s continue the work on our grid system. After specifying the padding, we will specify the width of the rows and container. We will set it to 100%.

CSS Code

1 2 3 4 5 6 7 8 9 10 11 12

.container{
    background-color: #672E3B;
    padding:1%;
    width:100%;
}

.row{
    background-color: #92B558;
    margin:0.5%;
    width:100%;
}

When we view the page in the web browser, we can notice something. The rows are off the container by some pixel.


Fixing that issue is simple. As we remember, the total width of an element is 100% including the content, border, padding and margin. Since the property “box-sizing” is set to “border-box”, the total width of each row will be 100% + margin = 100% + 1% = 101% which is greater than the container size (100%). To fix that, we reduce the width property of the row to 99% to add place for the margin. The correct width should be 99%. Keep this rule in mind, the total width of an element = 2*margin + width property value if the "box-sizing" property is set to "border-box". The 2 is to consider the margin on the left side and the right side. Since the container has no margin, we don’t have to change its width.

CSS Code

1 2 3 4 5 6 7 8 9 10 11 12

.container{
    background-color: #672E3B;
    padding:1%;
    width:100%;
}

.row{
    background-color: #92B558;
    margin:0.5%;
    width:99%;
}
Once we set the width of the row to 99%, every thing will look good as it should be.


This is the end of this part. In the next post, we will be working with the columns. Once we finish that, our rigid grid system will be fully ready for use in big screens.

Monday, October 9, 2017

Building a Responsive Grid System Using CSS: Part 2

Hello everyone. This is the second part on the series that will teach you how to build a responsive grid system for your website using CSS. In the previous post, we discussed the reasons that could lead to building a grid system. Also we reviewed some basic concepts related to CSS Box Model. In this post, we will be discussing the basic idea behind grid system. Also we will be talking about new concept in CSS which is the At-Rule

The Idea behind Grid System

For sure you know the meaning of the word ‘grid’. It is basically a set of intersecting vertical and horizontal lines. It is used to divide the page into rows and columns. Same idea applies in web development. We use CSS to divide the page into rows and columns. In web development, there are two types of grid systems, one is called ‘fixed’ and the other is ‘fluid’. The fixed does not change the width and height regardless of screen size. On the other hand, the fluid will adapt to different screen sizes. What we will be doing is to build a fluid grid system (Known as responsive grid). In order to build a fluid grid, we will have to use percentage to specify the content width and margins.

Web Page Grid System

As we have said before, the first step in building the responsive grid system is to divide the page into rows and columns. To illustrate this, let’s take a look at the next picture.

In the given picture, we have something called ‘container’. We can think of it as a simple ‘div’ element. Inside the container, we have a grid system that consists of 3 rows. Each row has 4 columns at most and 2 columns at least. If we would use percentage to represent the width of each column in the first row, each column will take 25% of the row width. In the second row, one column will take 75% of the width and the other one 25%. In the last row, each column will take 50% of the row width. We can notice that in each case, the percentage will sum up to 100% for each row. Similar to the container, we can think of each row and column as a ‘div’ element

If we would implement the given grid system in CSS, It will be as it is in all screens, 4 columns in the first row, 2 columns in the second and last column if it is fixed. But what if we would like from the grid to display well in small screens? The idea is simple. We have to make the width of the columns to adopt to different screen sizes. We have to think about large, medium and small screens. To do that, we have to learn about new concept called CSS At-rule.

CSS At-Rules

An At-rule is simply a CSS statement that starts with the character '@'. There are two types of At-rules, conditional (or nested) at-rules and non-conditional at-rules. The non-conditional at-rules end with a semi-colon. One example of such as rule is the '@charset' rule. The conditional at-rules will include a block of CSS statments that will only apply to HTML document if the condition is true. An example of such at-rule is the '@media' rule. The '@media' rule is one of the most important at-rules as it is used to check the size of the screen that the website is displayed on. We will learn later how to use it to make our grid system responsive.

CSS Code

1 2 3 4 5 6

@charset 'UTF-8';
/* The @charset rule must be the first thing appear in css file. */

In the next post, we will start by building a fixed grid system. Later on, we will use the @media rule to make the grid system responsive.

Monday, October 2, 2017

Building a Responsive Grid System Using CSS: Part 1

Starting today, I will be posting a series of posts in which I will be teaching how to build a responsive grid system from scratch using CSS. Every week, I will try to add one post. This is the first post and we will be reviewing some basic concepts related to CSS.

Introduction

In these days, many people are browsing the web using many other devices other than personal computers and laptops. For that reason, one factor to consider while developing a website is to make it adaptive to different screen sizes. One way would be to create variants of the website for each device which is not good idea. The best option is to go with creating a responsive grid system for the website. Once you have the grid ready, you can use it to build the UI of the web site.

Before we continue with the process of building the grid system, you must have the basic knowledge of the following topics:

  • HTML Basics.
  • CSS Basics.
  • Using Web Browser Inspector (optional but very helpful).

Review: Box Model

Before we start building the grid system, let’s review some basic concepts that we need. The one and most important concept is the “Box Model”. The idea of box model is that HTML elements are represented as a rectangle. The rectangle has 5 important properties. The 5 properties are used to specify the Real width and height of HTML element. The 5 properties are:

  • Width
  • Height
  • Border
  • Margin
  • Padding

The width and height properties are used to specify the width and height of the content area within the HTML element. The border is simply a visible line that is used to show the shape of HTML element. The margin is used to specify how far the given HTML element from the other elements. The padding is used to specify how far the content area of HTML element from the border.

The border, the margin and padding are represented as boxes that surround the content area as illustrated by the next image.

By default, web browsers use only the content area width and height to compute the total width and height of HTML element. Basically this means that width property of HTML element = content width. This means that if we add a border of size 2px and a padding of size 5px to an element with width property = 100px, the total visible width will be 114px. We can see that in the next example.

HTML Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <style type="text/css">
            .div1{
                border: 2px black solid;
                background-color: #2d8659;
                width: 200px
            }
            .childDiv1{
                border: 2px black solid;
                background-color: #99e6e6;
                width: 100px;
                padding: 5px
            }
            .childDiv2{
                border: 2px black solid;
                background-color: #EEEEFF;
                width: 100px;
                padding: 5px
            }
        </style>
    </head>
    <body>
        <div class="div1">
            <div class="childDiv1">
                Child Div1
            </div>
            <div class="childDiv1">
                Child Div2
            </div>
        </div>
    </body>
</html>

What we have in here is one DIV element with two DIV elements inside it. What we care about is the width of the inner two DIV elements. The “width” property is set to 100px. The border property is set to 2px and the padding is set to 5px. When we use the inspector of a web browser, we can see that the total element width is 114px.

This behavior can be overridden by specifying the value of the CSS property “box-sizing”. This property is used to tell the web browser how to calculate the width and height of HTML element. The default value for this property is “content-box” which means that the value of “width” property will be the width of content area. Another possible value for this property is “border-box”. If the value of the “box-sizing” property is set to “border-box”, the value of the property width will be content width + border + padding. For example, if we set the width property to be 100px with a border of size 2px and a padding of 5px, the total element width will be 100px. 10 of the 100 will be for the padding and 4 will be for the border and 86px for the content.

Let’s modify the code above and change the value of the property “box-sizing” and set it to “border box” and observe the effect.

HTML Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <style type="text/css">
            
            /*This selector is used to 
             *selects all HTML elements
             */

            *{
                box-sizing: border-box;
            }
            .div1{
                border: 2px black solid;
                background-color: #2d8659;
                width: 200px
            }
            .childDiv1{
                border: 2px black solid;
                background-color: #99e6e6;
                width: 100px;
                padding: 5px
            }
            .childDiv2{
                border: 2px black solid;
                background-color: #EEEEFF;
                width: 100px;
                padding: 5px
            }
        </style>
    </head>
    <body>
        <div class="div1">
            <div class="childDiv1">
                Child Div1
            </div>
            <div class="childDiv1">
                Child Div2
            </div>
        </div>
    </body>
</html>

As we can see, the width of the element is the same as we set even when we have border and padding. This will be useful as we build our grid system later. For that reason, we will set the “box-sizing” for all elements to “border-box” including pseudo -elements. To do that, we use the following CSS ruleset:

CSS Code

1 2 3 4 5

*, *::before,*::after{
    box-sizing:border-box;
}

Just a reminder, pseudo-elements uses double semi-colon and pseudo-classes uses single semi-colon. Next, we will start by understanding the idea behind the grid system.

One thing to note is that we did not talk about the margin. One thing to mention about the margin is that it will be added up to the element total width regardless of the property "box-sizing".

In the next post, we will be learning about the basic idea behind grids and we will be interdicting a new concept which is the CSS At-Rule.

Sunday, August 27, 2017

Java Syntax Highlighter

One of the things that was annoying for me is adding code to my web site and blog. What I do is I have to go to the code word by word and apply the styles manually. This task usually takes a lot of time to do it. During the last few days, I was working on creating a tool that can help me in doing that task in much faster way. For that reason, I created the tool "Java Syntax Highlighter". The tool can be found at https://www.programmingacademia.com/en/tools/java-syntax-highlighter

How to Use The Tool

The tool is very easy to use all of what you have to do is the following:

- Include the following CSS file in your web page:

Code

1 2 3

<link rel="stylesheet" href="https://www.programmingacademia.com/res/css/code-theme.css">

After that, copy your java code and past it within the tool. Once you do that, click the button 'Highlight' and you will get two things:

  • HTMl code.
  • A preview of how the code snippet will look like.

What you have to do is only to copy the generated HTML code and past it in your web site.

Reporting Errors

Note that the tool is still in beta which means it might have some issues. If you find any issue with the tool, please report it using the reporting form within the tool page.