Showing posts with label Web Development. Show all posts
Showing posts with label Web Development. Show all posts

Tuesday, March 22, 2022

Content Sniffing in Web Development

While I was testing a software, I needed to refresh JavaScript files based on branch on GitHub. What I did is to include the raw data of the file using raw.gethubcontent.com.

What I noticed is that the console started to display the error which reads :

The resource from “https://raw.githubusercontent.com/usernane/AJAXRequestJs/master/AJAXRequest.js” was blocked due to MIME type (“text/plain”) mismatch (X-Content-Type-Options: nosniff).

What does this error means and why it appeared? After doing a small research, I learned about new concept which is called "Content Sniffing"

In a nutshell, content sniffing happens when a web browser change the content type of server response to a content type that it think is correct. For example, a server might send a JavaScript file with content type "text/plain". In this case, the browser will change content type to "text/JavaScript" because the file smells like JavaScript and make it executable. This can lead to security issues if the file is user generated and can lead to attacks such as .

To make sure that the browser does not change content type based on its smell, the server can send http header. The header has the name "". The error that was appearing in the console was caused by this header. GitHub server always sends this header whenever someone tries to get files using raw.githubcontent.com.

To resolve my issue, I reverted back to jsDeliver. The CDN actually has a nice feature that I just got to know. It is possible to get files using commit hash. Instead of using a branch name or tag after the @, simply add the hash of the commit.

Wednesday, June 23, 2021

Thursday, January 14, 2021

WebFiori Framework Version 2.0.0

Hello everyone. Today is a big day for me. The reason for that is version 2.0.0 of WebFiori Framework was just released.

This release is considered as a major release since it is a whole refactor of version 1.0 of the framework. For more information about this release, you can check GitHub Repo of the project.

If you don't know what is WebFiori framework, it is a web development framework which was built in top of PHP language. The framework makes it easy for web developers to build simple web applications. For more information about the project, chech this page: https://webfiori.com/learn/introduction

Tuesday, December 8, 2020

Apache NetBeans IDE Setup for PHP Development #4

The forth video on a set of videos that explains how to setup Apache NetBeans IDE for PHP development.

Sunday, December 6, 2020

Apache NetBeans IDE Setup for PHP Development #3

The third video on a set of videos that explains how to setup Apache NetBeans IDE for PHP development.

Saturday, December 5, 2020

Apache NetBeans IDE Setup for PHP Development #2

The second video on a set of videos that explains how to setup Apache NetBeans IDE for PHP development.

Apache NetBeans IDE Setup for PHP Development #1

The first video on a set of videos that explains how to setup Apache NetBeans IDE for PHP development.

Saturday, March 9, 2019

WebFiori, New Web Development Framework

WebFiori Framework is new web development framework which was built in top of PHP scripting language. It was designed from scratch to provide the developer with needed tools to setup new website or create a very complex web application using minimum tools.

Basic features (Taken from official website):

  • Theming and the ability to create multiple UIs for the same web page using any CSS or JavaScript framework.
  • Support for routing that makes the ability of creating search-engine-friendly links an easy task.
  • Creation of web APIs that supports JSON, data filtering and validation.
  • Basic support for MySQL schema and query building.
  • Lightweight. The total size of framework core files is less than 3 megabytes.
  • Access management by assigning system user a set of privileges.
  • The ability to create and manage multiple sessions at once.
  • Support for creating and sending nice-looking emails in a simple way by using SMTP protocol.
  • Autoloading of user defined classes.
  • The ability to create automatic tasks and let them run in specific time using CRON.
  • Support for logging of system events.
  • Well-defined file upload and file handling sub-system.

The project is currently hosted in my personal website and it can be accessed through the URL: https://programmingacademia.com/webfiori . You can find more information about the project there.

Another thing is that the project is open source and is licensed under MIT license. You can grab the source code of the framework from my GitHub repo at https://github.com/usernane/webfiori.

Your Contribution is Appreciated

One of the things that I need your help in is your feedback (Negative or positive). You can help me by trying the framework your self and provide me with your feedback. You check https://programmingacademia.com/webfiori/learn to get started.

Another thing is that you might find hundreds of issues in the source code of the framework since I'm not an experienced PHP developer (Design issues, Security issues, Bad practices, etc...). What you can possibly do is to review the source code and report any issue that you might find regarding how things are done.

Also, if you need any help regarding how to use the framework or you are just interested, please join telegram group using the following link: https://t.me/webfiori

Thank you for your time.

Saturday, September 15, 2018

JsonX APIs

Note: This library is now part of WebFiori Framework. Updated API Docs can be found at https://programmingacademia.com/webfiori/docs/jsonx/.

This page contain APIs descriptions of the library JsonX, A library that can be used to create JSON strings in PHP. The library consist of two files only, JsonX.php and JsonI.php. The first file contains the class 'JsonX' which is the core component. The other file contains an interface which can be implemented by other classes to create a custom JSON strings.

Basic Usage Example

The next lines of code shows how to use the library in very simple way.

<?php
include 'JsonX.php';
include 'JsonI.php';

//create new instance of JsonX
$json = new JsonX();

//add attributes to JSON object.
$json->add('a-string', 'This is a string.');
$json->add('a-number', 44);
$json->add('a-boolean', FALSE);
$json->add('null-value', NULL);

//send Json object as response
header('content-type:application/json');
echo $json;

Class JsonX

Version: 1.2

Author: Ibrahim BinAlshikh

const TYPES

  • Type: Array
  • Description: An array of supported JOSN data types. The array has 7 values: 'integer', 'string', 'double', 'boolean', 'NULL' and 'object'.
  • Since: 1.0

const SPECIAL_CHARS

  • Type: Array
  • Description: An array which contains JSON special characters that can be a part of a string. The array has the following characters: \\, /, ", \t, \r, \n and \f.
  • Since: 1.0

const SPECIAL_CHARS_ESC

'
  • Type: Array
  • Description: An array which contains escaped JSON special characters that can be a part of a string.
  • Since: 1.0

public function add($key, $value, $options=array())

Adds a new value to include in the generated JSON string.

Parameters:

  • $key:
    • Type: string
    • Description: The name of the attribute that the given value will be stored in. Usually called the "name" of the "value".
  • $value:
    • Type: mixed
    • Description: The value of the key. It can be an integer, a double, a string, an array or an object. If NULL is given, the method will set the value at the given key to 'null'.
  • $options: [Optional]
    • Type: Array
    • Description: An associative array of options. Currently, the array has the following options:
      • string-as-boolean: A boolean value. If set to TRUE and the given string represents a boolean value (like 'yes' or 'no'), the string will be added as a boolean value. Default is FALSE.
      • array-as-object: A boolean value. If set to TRUE, the array will be added as an object. Default is FALSE.

Returns:

  • boolean: TRUE if the value is set. If the given value or key is invalid, the function will return FALSE.

Since: 1.1

public function addNumber($key, $value)

Adds a number to include in the generated JSON string.

Parameters:

  • $key:
    • Type: string
    • Description: The name of the attribute that the given value will be stored in. Usually called the "name" of the "value"
  • $value:
    • Type: integer | double
    • Description: The number which will be added. Note that if the given number is INF or NAN, The method will add them as a string.

Returns:

  • boolean: TRUE if the value is set. If the given value or key is invalid, the function will return FALSE.

Since: 1.0

public function addBoolean($key, $value)

Adds a boolean value (true or false) to include in the generated JSON string.

Parameters:

  • $key:
    • Type: string
    • Description: The name of the attribute that the given value will be stored in. Usually called the "name" of the "value"
  • $value: [Optional]
    • Type: boolean
    • Description: TRUE or FALSE. If not specified, The default will be TRUE.

Returns:

  • boolean: TRUE if the value is set. If the given value or key is invalid, the function will return FALSE.

Since: 1.0

public function addString($key, $value, $asBoolean)

Adds a string to include in the generated JSON string.

Parameters:

  • $key:
    • Type: string
    • Description: The name of the attribute that the given value will be stored in. Usually called the "name" of the "value"
  • $value:
    • Type: string
    • Description: The value of the string. Note that if the given string is one of the following and the parameter $asBoolean is set to TRUE, it will be converted into boolean (case insensitive).
      • yes => TRUE
      • no => FALSE
      • y => TRUE
      • n => FALSE
      • t => TRUE
      • f => FALSE
      • true => TRUE
      • false => FALSE
      • on => TRUE
      • off => FALSE
      • ok => TRUE
  • $asBoolean: [Optional]
    • Type: boolean
    • Description: If set to TRUE and the string represents a boolean value, then the string will be added as a boolean. Default is FALSE.

Returns:

  • boolean: TRUE if the value is set. If the given value or key is invalid, the function will return FALSE.

Since: 1.0

public function addArray($key, $value, $asObject)

Adds an array to include in the generated JSON string.

Parameters:

  • $key:
    • Type: string
    • Description: The name of the attribute that the given value will be stored in. Usually called the "name" of the "value"
  • $value:
    • Type: Array
    • Description: The array that will be added. If the given array is indexed array, all values will be added as single entity (e.g. [1, 2, 3]). If the array is associative, the values of the array will be added as objects.
  • $asObject: [Optional]
    • Type:
    • Description: If this parameter is set to TRUE, the array will be added as an object in JSON string. Default is FALSE.

Returns:

  • boolean: TRUE if the value is set. If the given value or key is invalid, the function will return FALSE.

Since: 1.0

public function addObject($key, $value)

Adds a PHP object to include in the generated JSON string.

Parameters:

  • $key:
    • Type: string
    • Description: The name of the attribute that the given value will be stored in. Usually called the "name" of the "value">
  • $value:
    • Type: JsonI | Object
    • Description: The parameter can be a PHP object or an object that implements the interface 'JsonI'. If the object does not implement the interface 'JsonI', then the function will try to extract object information based on its public functions and the generated JSON will be on the format {"prop-0":"something","prop-1":"something else","prop-n":"xxx"}.

Returns:

  • boolean: TRUE if the value is set. If the given value or key is invalid, the function will return FALSE.

Since: 1.0

public function get($key)

Returns a string that represents the value at the given key.

Parameters:

  • $key:
    • Type: string
    • Description: The name of the attribute that the value is stored in. Usually called the "name" of the "value"

Returns:

  • string | NULL: A string that represents the value at the given key. If the key does not exists, the function will return NULL.

Since: 1.2

public function hasKey($key)

Checks if a key is exist or not.

Parameters:

  • $key:
    • Type: string
    • Description: The name of the key. Usually called the "name".

Returns:

  • boolean: The function will return TRUE if the key exists. FALSE if not.

Since: 1.2

public static function escapeJSONSpecialChars($str)

Escape JSON special characters from a string.

Parameters:

  • $str:
    • Type: string
    • Description: A value of one of JSON object properties. If it is NULL,the method will return empty string.

Returns:

  • string: The same string with JSON special characters escaped.
  • Since: 1.0

Interface JsonI

Version: 1.0

Author: Ibrahim BinAlshikh

public function toJSON()

This function can be implemented by any class that will be added as an attribute to any JsonX instance.It is used to customize the generated JSON string.

Parameters:

  • NONE

Returns:

  • JsonX: An instance of JsonX

Since: 1.0


Tuesday, October 31, 2017

Building a Responsive Grid System Using CSS: Part 4

Recap

In the last post, we started working on the grid system after we understood the idea behind it. The work that we did so far is only for two selectors, the row and the container. Now that we have the container and the row setup, we can start working on the columns. Note that we might need to add more rules for the row and container later.

Working on The Columns Selectors

The first thing that we have to specify is the padding within each column. To do that, we have to add new selector as follows:

CSS Code

1 2 3 4 5
[class*='col'] {
    padding:0.5%;
}

The selector [class*=”a_sub_string”] is used to apply rules to a set of class selectors with the name of the class containing the string “a_sub_string”. When we add this rule, each column will have 0.5% padding. After specifying the padding, we will apply a coloring to all the columns to make them visible.

CSS Code

1 2 3 4 5 5
[class*='col'] {
    padding:0.5%;
    background-color: #b3ffb3;
}

Now, let’s modify our HTML document to include two columns in the body. Both columns will have the same width. Since we divide the row into two columns, each one will take 50% of the total row width. This means we will use the ‘col-6’ class selector that we wrote in part 3.

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">
                <!--col-1 will have the minimum width-->
                <!--col-12 will have the maximum width-->
                <div class="col-6">Column 1</div>
                <div class="col-6">Column 2</div>
            </div>
            <!--Page Footer-->
            <div class="row">
                Footer
            </div>
        </div>
    </body>
</html>

Before we continue with how to specify the width of each column in the grid, let’s see the result of our work till now.

Hmm…, something is not correct. The first problem is that each column is in top of the other like a row. The other problem is that the body row has disappeared. To make the column act as a column, we have to specify the value of a property called ‘float’. This property is used to specify where to place the element in its parent container. Depending on the language the website uses, the column might float left or right. Since English language is left to right, we will make the columns float left.

CSS Code

1 2 3 4 5 6 7
[class*='col'] {
    padding:0.5%;
    background-color: #b3ffb3;
    float:left;
}

After setting the value of this property, the HTML document will look like this:

Well, the columns ‘look like’ columns but we still have other issues. As we can see, the columns are connected with each other and the body row is still not visible. Also the footer is in the same place as the columns. In order to separate the columns, we have to specify the margin for each column, and in order to make the row visible, we have to set the property padding of the row. Let’s set the row padding to 0.5% and the margin of the columns to 0.5%.

CSS Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
.row{
    background-color: #92B558;
    padding:0.5%;
    margin:0.5%;
    width:98%;
}

[class*='col'] {
    padding:0.5%;
    margin:0.5%;
    background-color: #b3ffb3;
    float:left;
}

After adding the given properties to our CSS file, the HTML document should be look like this in the browser:

We have got new problems but fixed the previous ones. The first one is that the columns are outside the row and the second one is the columns have incorrect widths. To fix the first issue we need to set the value for the property “float” for the row and the container. We make both float to the left. Once we do that, the columns will fit within the row.

The final step before we test what we did is to specify the width of each column in the grid. As we have said before, the grid that we will be building will have 12 columns. Assuming that the total available width is 100%, each column will have a width of 100/12 = 8.333333333% which is the minimum possible width which will be col-1 width. The next column (which is col-2) will have width of col-1 + 8.333333333%. The idea is to increase the width by 8.3333333333% each time we move to the next column.

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 35 36 37 38 39
.col-1{
    width:8.333333333%;
}
.col-2{
    width:16.66666667%;
}
.col-3{
    width:25%;
}
.col-4{
    width:33.33333333%;
}
.col-5{
    width:41.66666667%;
}
.col-6{
    width:50%;
}
.col-7{
    width:58.33333333%;
}
.col-8{
    width:66.66666666%;
}
.col-9{
    width:75%;
}
.col-10{
    width:83.33333333%;
}
.col-11{
    width:91.66666666%;
}
.col-12{
    width:100%;
}

When we test the result in the web browser, we will see a problem as follows:

As we can see, the second column has moved down. The issue we have here is related to margin of each column. Remember that the margin of the columns is set to 0.5%. This means that 1% of the total width has been used for the margin. In order to fix this problem, we subtract 1% from the width of each column to add space for the margin. So, the correct width of each column will be as follows:

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48

.col-1{
    width:7.333333333%;
}

.col-2{
    width:15.66666667%;
}

.col-3{
    width:24%;
}

.col-4{
    width:32.33333333%;
}

.col-5{
    width:40.66666667%;
}

.col-6{
    width:49%;
}

.col-7{
    width:57.33333333%;
}

.col-8{
    width:65.66666666%;
}

.col-9{
    width:74%;
}

.col-10{
    width:82.33333333%;
}

.col-11{
    width:90.66666666%;
}

.col-12{
    width:99%;
}

Once we update the width of the columns, the two columns will fit within the body row.

To make sure that what we did so far is 100% correct, we will be creating multiple rows within the body and add columns inside each row. Note that it is possible to add columns inside rows and rows inside columns (sub-dividing).

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 47 48 49 20 51 52 53 54 55 56 57 58 59
<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">
                <div class="row">
                    <div class="col-12">100%</div>
                </div>                 <div class="row" >                     <div class="col-6">50%</div>                     <div class="col-6">50%</div>                 </div>                 <div class="row" >                     <div class="col-3">25%</div>                     <div class="col-3">25%</div>                     <div class="col-3">25%</div>                     <div class="col-3">25%</div>                 </div>                 <div class="row" >                     <div class="col-1">8.33%</div>                     <div class="col-1">8.33%</div>                     <div class="col-1">8.33%</div>                     <div class="col-3">25%</div>                     <div class="col-3">25%</div>                     <div class="col-3">25%</div>                 </div>                 <div class="row" >                     <div class="col-11">90%</div>                     <div class="col-1">10%</div>                 </div>             </div>             <!--Page Footer-->             <div class="row">                 Footer             </div>         </div>     </body> </html>

The result of the given HTML code will be like the next image if we view it in FireFox web browser for desktop.

What we just did is a fixed grid system. If we would build a website using the given grid, it will look 100% perfect in a big screen. In the next post, we will make the grid fluid (or responsive).

The full CSS Code that we wrote thus far can be downloaded from here.

Tuesday, October 24, 2017

CSS Syntax Highlighter and Formatter

Introduction

This week, instead of working on new part of my series in how to create a responsive grid system in CSS, I have decided to do other thing. What I did is I created a tool that can be used to create a nicely formatted CSS code snippets that can be added to any website.

The Idea Behind The Tool

If you was following with me for the last few weeks, you will for sure know that I was writing blog posts about how to build a responsive grid system using CSS. One issue that I had is writing CSS code snippets in my posts. What I used to do is to write the CSS code and format it manually which takes a lot of effort and time.

After realizing that, I decided to create a tool that I can use to create the code snippets automatically. By doing that, I will save a lot of time and effort. After working on the tool for the last two weeks, the tool is ready for public use and available in my official website, Programming Academia Under the name CSS Syntax Highlighter and Formatter.

Using The Tool

In order to insert the code snippet in your web site, there are only 4 steps to do:

Including CSS File

The first step is to include the following <link> tag inside the <head> tag.

Code

1 2 3

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

This CSS file is basically the theme that will be applied to the different keywords within your CSS code. Also it contains some formatting parameters to make the code snippet looks good in big and small screens.

Copying and Pasting Your CSS Code

In this step, All what you need to do is to copy your CSS code and past it within the tool. Suppose that we have the following CSS code that we would like to add to our blog:

/*For mobile*/
@media screen and (max-width:760px){.col{width:100%}}
/*For Big Screens*/
.col-1{width:50%}
.col-2{width:100%}

As we can see, the code is kinda messy and not beautiful to add in a web page. Simply we copy it and past it in the tool.

CSS Highlighter

Past your CSS code in the shown area.

Click 'Highlight'

Once you click the button that is labeled as 'Highlight', you will get two things, The first thing is an editable HTML code that you can copy and past in your web page and secondly, A preview of how the code snippet will look like.

The Generated Code Snippet as HTML with Preview.

Copying The Generated HTML Code

The last and final step is to copy the generated HTML code and past it in the web page that you would like to show the code snippet in. Also it is possible to edit the code if you would like to add or remove extra stuff.

And this is how you can use this simple tool. Please give me your opinion about it and provide me with your feedback. Also I will be more than happy if you share it with your friends and any one in need for such as tool 😄.

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.