CSS Fundamentals: Beginner’s Guide to Web Styling
Sobre a Aula

Grid

Hello, everyone! Welcome to the Grid lesson.

In this lesson, we will learn about the grid, a CSS property that can be used to create flexible and responsive layouts.

What is grid?

Grid is a CSS property that allows organizing elements in a grid, similar to a spreadsheet.

Grid elements can be resized, reordered, and aligned according to the layout needs.

Basic Grid Properties

To use the grid, we need to set the display property of the element to grid:

.container {
  display: grid;
}

After setting the display property to grid, we can use the following properties to control the layout:

  • Grid-template-columns: Defines the number and size of columns in the grid.
  • Grid-template-rows: Defines the number and size of rows in the grid.
  • Grid-column-start: Defines the starting column of an element.
  • Grid-column-end: Defines the ending column of an element.
  • Grid-row-start: Defines the starting row of an element.
  • Grid-row-end: Defines the ending row of an element.
  • Grid-gap: Defines the spacing between columns and rows in the grid.

Examples

Let’s see some examples of how to use the basic grid properties:

Example 1:

The following CSS code uses the grid to create a two-column layout with equal widths:

.container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}

This code defines two columns with equal widths.

Example 2:

The following CSS code uses the grid to create a layout with three rows of equal heights:

.container {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
}

This code defines three rows with equal heights.

Example 3:

The following CSS code uses the grid to create a layout with four columns and rows of equal widths and heights:

.container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
}

This code defines four columns and four rows with equal widths and heights.

Note

It’s important to use CSS selectors to control which CSS rules are applied to which elements. This will help keep your CSS organized and easy to maintain.

Summary

In this lesson, we learned about the grid, a CSS property that can be used to create flexible and responsive layouts.

We explored the basic grid properties and saw some examples of how to use these properties to create different layouts.

Try creating layouts using the grid properties you learned in this lesson. Experiment with different sizes, shapes, and alignments.

In the next module, we will learn about advanced styling of elements, including forms, tables, navigation menus, and animations.

Note:

Here are some additional tips for using the grid:

  • Use the grid-column-start and grid-column-end properties to horizontally align elements in the grid.
  • Use the grid-row-start and grid-row-end properties to vertically align elements in the grid.
  • Use the grid-gap property to set the spacing between columns and rows in the grid.
  • Use the grid-template-areas property to define grid areas.
Entrar na conversa
Rolar para cima