top of page
Writer's pictureThe Tech Platform

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

Specifies the gap between the columns

Gap

A shorthand property for the row-gap and the column-gap properties

Grid

A shorthand property for the grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and the grid-auto-flow properties

Either specifies a name for the grid item, or this property is a shorthand property for the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties

Specifies a default column size

Specifies how auto-placed items are inserted in the grid

Specifies a default row size

Grid-column

A shorthand property for the grid-column-start and the grid-column-end properties

Specifies where to end the grid item

Specifies the size of the gap between columns

Specifies where to start the grid item

A shorthand property for the grid-row-gap and grid-column-gap properties

Grid-row

A shorthand property for the grid-row-start and the grid-row-end properties

Specifies where to end the grid item

Specifies the size of the gap between rows

Specifies where to start the grid item

A shorthand property for the grid-template-rows, grid-template-columns and grid-areas properties

Specifies how to display columns and rows, using named grid items

Specifies the size of the columns, and how many columns in a grid layout

Specifies the size of the rows in a grid layout

Specifies the gap between the grid rows




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

0 comments

Recent Posts

See All

Comments


bottom of page