CSS Grid Layout is a two-dimensional layout system for developing responsive webpages. In this tutorial we will learn how to design a webpage layout using grid systems.
What is a Grid Layout?
Grid Layout used to enhance user experience over all the user devices by providing a responsive and flexible arrangement of HTML components over the webpage.
Grid is ideal for creating overall layout of a webpage, while flexbox is mostly used to align items inside a container.
Table of Contents
Grid Elements
Display Grid Property
Grid Rows and Columns
Grid Gap
Grid Lines
CSS Grid Properties Reference
Grid Elements
In a grid layout system, we have two elements, Grid Container and Grid Item.
Grid Container
Grid container defines the outer element of grid, where all the child elements are present. A grid container can be defined by setting display: grid or display: inline-grid
Grid items
Grid items are all the direct elements inside grid container. This items can be arranged vertically and horizontally inside flexbox container based on need.
Display Grid Property
A HTML element become a grid container if we set value of display property as grid or inline-grid. All direct children of the grid container automatically become grid items.
grid: This value defines block level grid container, means it behave like a block element taking full width available. ( Similar to div )
inline-grid: This value defines inline level grid container, means it behave like a inline element taking as much width as it’s content. ( Similar to span )
Example
Following example show the difference between grid and inline-grid
Here is an example of
<span class="inline-grid-container"><span class="grid-item">1</span><span class="grid-item">2</span><span class="grid-item">3</span></span>
inline grid.
In CSS, we can define the number of columns and rows required in our layout. Each cell will represent a grid item. Following code shows how to define rows and columns in grid.
Example
In this example we will create two grid layout one is for row and another one is for column, each grid has row and column but showing them individually will help you to crack the rows and columns.
The grid gap properties are used to add gaps between rows and columns of grid items. We have three properties associated with this, gap, column-gap and row-gap.
The gap property can set equal gap for both row and column, while row-gap and column-gap can set different gap values for row and column. Row value and column value have more preference over gap value, so if we define all three row value and column value will override over gap value.
Example
Here in this example we tried to explain the gap between grid items.
/* Sets both row and column gaps to 20px */
gap: 20px;
/* Make gap between rows as 10px (override) */
row-gap: 10px;
/* Make gap between columns as 5px (override) */
column-gap: 5px;
}
.grid-item {
background-color: #4CAF50;
border: 1px solid #ddd;
padding: 20px;
text-align: center;
font-size: 1.2em;
color: white;
}
</style></head><body><h1>Grid Gap Example</h1><div class="grid-container"><div class="grid-item">
In CSS, Grid items can be placed according to the imaginary ines between grid cell called grid items. The lines between columns are called column lines and lines between rows are called row lines.
CSS Grid Properties Reference
Following is the list of CSS properties related to grid:
property
value
Example
display
Define whether an element is a grid container or an inline grid container.
Try It
gap
Defines the gap between rows and columns.
Try It
grid-area
Defines the location and size of the grid item within a grid layout.
Try It
grid-column
Control the placement of a grid item within the grid container in the column direction.
Try It
grid-row
Control the placement of a grid item within the grid container in the row direction.
Try It
grid-template
Specify the grid columns, grid rows and grid areas.
Try It
grid-auto-columns
Determines the size of automatically generated grid column tracks or a pattern of such tracks.
Try It
grid-auto-rows
Determines the size of automatically- generated grid rows tracks or a pattern of such tracks.
Try It
grid-auto-flow
Specifies arrangement of the grid item within the grid.
Leave a Reply