The Tech Platform

Nov 23, 20212 min

CSS Grid Layout Properties with Examples

Grid

The CSS Grid Layout Module offers a grid-based layout system, with rows and columns, making it easier to design web pages without having to use floats and positioning.

Example:

<!DOCTYPE html>
 
<html>
 
<head>
 
<style>
 
.grid-container {
 
display: grid;
 
grid-template-columns: auto auto auto;
 
background-color: pink;
 
padding: 15px;
 
}
 

 
.grid-item {
 
background-color: white;
 
border: 1px solid ;
 
padding: 20px;
 
font-size: 30px;
 
text-align: center;
 
}
 
</style>
 
</head>
 
<body>
 

 
<h1>CSS Grid Layout</h1>
 

 
<div class="grid-container">
 
<div class="grid-item">1</div>
 
<div class="grid-item">2</div>
 
<div class="grid-item">3</div>
 
<div class="grid-item">4</div>
 
<div class="grid-item">5</div>
 
<div class="grid-item">6</div>
 
<div class="grid-item">7</div>
 
<div class="grid-item">8</div>
 
<div class="grid-item">9</div>
 
</div>
 

 
</body>
 
</html>

Output:

Grid Column:

The grid-column property specifies a grid item's size and location in a grid layout, and is a shorthand property for the following properties:

Syntax:

grid-column: grid-column-start / grid-column-end;

Grid Row:

The grid-row property specifies a grid item's size and location in a grid layout, and is a shorthand property for the following properties::

Syntax:

grid-row: grid-row-start / grid-row-end;

CSS Grid Properties

Benefits

1. Reduced code bloat

Rather than creating extra HTML elements to contain your grid, columns, and rows, your grid tracks are created within your stylesheet.

2. Improved semantics

Frameworks like Bootstrap often compel developers to venture into divitis territory. What’s more, grid frameworks don’t always use the most semantically sound class names.

3. Reduced file sizes

As CSS Grid is native, there is no need to include large libraries like Bootstrap in your projects.

4. Speed of development

Once you learn the syntax, prototyping with CSS Grid is fast and efficient.

5. Style labels

It also became possible to style labels based on the state of their field using adjacent sibling selectors - for example, applying bold to a label when its associated checkbox is checked.

6. Two-dimensional

A grid is two-dimensional and respects both rows and columns. If an element is too big for its cell, the row and/or column will grow accordingly. A grid is ideal for page and form layout.
 

7. Nested Grids

The CSS Grid Layout is also useful for styling such smaller components as content sections with blog posts or the blog posts themselves.

8. Responsiveness

the CSS Grid Layout gives us the possibility to easily change the placement of the grid items according to the device screen.

Disadvantages:

  1. Plain and impersonal

  2. Offers limited browsing

  3. Produces constrained and rushed psychological effect

  4. Can be confusing and frustrating.

The Tech Platform

www.thetechplatform.com

    0