Mark As Completed Discussion

In this lesson, we will explore CSS Grid, a powerful layout mechanism for creating advanced grid layouts in web development.

CSS Grid allows you to create flexible and responsive grid layouts for web pages. It provides a two-dimensional grid system that allows you to define rows and columns, and place elements within the grid using grid lines and areas.

CSS Grid is particularly useful for creating complex grid structures, such as magazine-style layouts, image galleries, and responsive grid designs.

Here's an example of how to create a simple grid layout using CSS Grid:

SNIPPET
1<div class="grid-container"></div>
SNIPPET
1.grid-container {
2  display: grid;
3  grid-template-columns: repeat(3, 1fr);
4  grid-template-rows: repeat(3, 1fr);
5}
6
7.grid-item {
8  background-color: lightblue;
9  padding: 1rem;
10  text-align: center;
11}

This example creates a 3x3 grid layout. The grid container has a class of "grid-container" and is styled using CSS. The grid items are dynamically added using JavaScript.

You can customize the number of rows and columns by modifying the "grid-template-columns" and "grid-template-rows" properties. You can also adjust the styling of the grid items by modifying the CSS rules for the "grid-item" class.

Here's some example code to get you started:

JAVASCRIPT
1const container = document.querySelector('.grid-container');
2
3// Set the display property of the container to grid
4container.style.display = 'grid';
5
6// Define the grid template columns and rows
7container.style.gridTemplateColumns = 'repeat(3, 1fr)';
8container.style.gridTemplateRows = 'repeat(3, 1fr)';
9
10// Add grid items dynamically
11for(let i = 0; i < 9; i++) {
12  const item = document.createElement('div');
13  item.classList.add('grid-item');
14  item.innerText = `Item ${i + 1}`;
15  container.appendChild(item);
16}

This code creates a grid container with a class of "grid-container" and adds nine grid items with the class "grid-item". You can modify the number of rows and columns and customize the grid item content and styles to fit your needs.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment