Chuck's Academy

CSS Grid

Grid Lines and Grid Areas

CSS Grid offers an advanced and flexible way to define the placement of elements using Grid Lines and Grid Areas. These concepts allow detailed control over the layout and are fundamental for creating complex designs.

Grid Lines

Grid Lines are the horizontal and vertical lines that define the start and end of each row and column in the grid. Each line is numbered from 1, starting from the top left edge.

Usage of Grid Lines

You can use Grid Lines to place elements in the grid by specifying which lines they should start and end on. This is done using the grid-column and grid-row properties.

css

Negative Line Counting

In addition to counting from the start of the grid, you can also count from the end using negative numbers, starting from -1 at the bottom right edge.

css

Grid Areas

Grid Areas are rectangular sections within the Grid that can span multiple cells. Areas are defined using names and the properties grid-template-areas, grid-area, grid-column, and grid-row.

Defining Grid Areas

To define areas, you first need to name the Grid cells in the grid-template-areas property.

css
html

In this example, we create a Grid with three areas: header, sidebar, and main.

Positioning Elements with Grid Area

Besides defining areas, you can use the grid-area property on individual elements to position them.

css

Manual Positioning with grid-column and grid-row

You can also position elements manually by specifying which lines they should start and end on.

css

Complete Example

Let's look at a complete example that combines Grid Lines and Grid Areas:

html

In this example, we use both Grid Lines and Grid Areas to create an organized and easily manageable layout.

Conclusion

Using Grid Lines and Grid Areas gives you unprecedented control over grid designs in CSS Grid. Understanding how to use these features will enable you to create sophisticated and highly precise layouts, tailored to any design need you may have.


Ask me anything