CSS Grid

A grid can be defined as an intersecting set of horizontal lines and vertical lines. CSS Grid layout divides a page into major regions. It defines the relationship between the parts of a control built from HTML primitives in terms of layer, position, and size. Grid property offers a grid-based layout system having rows and columns. It makes the designing of web pages easy without positioning and floating.

Similar to the table, it enables a user to align the elements into rows and columns. But compare to tables, it is easy to design layout with the CSS grid. We can define columns and rows on the grid by using grid-template-rows and grid-template-columns properties.

The CSS grid property is supported in browsers such as Google Chrome, Internet Explorer, Firefox, Safari, and Opera.

Grid Container

We can define the grid container by setting the display property to grid or inline-grid on an element.

Grid container contains grid items that are placed inside rows and columns.

Let's see a simple example of a grid in CSS.

Example

Test it Now

Let's see some of the shorthand properties:

  • grid-template-columns: It is used to specify the size of the columns.
  • grid-template-rows: It is used to specify the row size.
  • grid-template-areas: It is used to specify the grid layout by using the named items.
  • grid-auto-rows: It is used to specify the automatic size of the rows.
  • grid-auto-columns: It is used to specify the automatic size of the columns.
  • grid-auto-flow: It is used to specify how to place auto-placed items and the automatic row size.

In the following example, we are including some of the above shorthand properties. Now, let's see the example of using some properties:

Example

Test it Now

The justify-content property

It is used to align the entire grid within the container. It includes values such as:

  • space-evenly: It provides equal space in between or around the columns.
  • space-around: It provides equal space around the columns.
  • space-between: It gives an equal amount of space between the columns.
  • center: It is used to align the grid in the middle of the container.
  • start: It is used to align the grid at the beginning of the container.
  • end: It is used to align the grid at the end of the container.

Note: It is noted that the total width of the grid should be less than the width of the container for any effect of the justify-content property.

Let's understand this property along with the values by using an example.

Example

Test it Now

The align-content property

It is used to align the entire grid within the container vertically.

Note: It is noted that the total height of the grid should be less than the height of the container for any effect of the align-content property.

The values of the align-content property are the same as the values of the justify-content property.


Next TopicCSS Layout




Contact US

Email:[email protected]

CSS Grid
10/30