CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



grid-template-columns

The grid-template-columns property is a part of the CSS Grid Layout module. It is used to define the columns of a grid container. This property specifies the size, position, and number of columns in a grid layout.

The grid-template-columns property is used to create a grid layout by defining the columns of the grid. It is used in conjunction with the grid-template-rows property to define the rows of the grid. Together, these two properties define the structure of the grid.

The grid-template-columns property can be used to define the size of each column in the grid. The size can be specified in pixels, percentages, or using the fr unit. The fr unit is used to specify a fraction of the available space in the grid container.

Here are some examples of using the grid-template-columns property:

<div class="grid-container">
  <div class="item item-1"></div>
  <div class="item item-2"></div>
  <div class="item item-3"></div>
</div>

.grid-container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
}

.item {
  background-color: #ccc;
  padding: 10px;
  text-align: center;
}

.item-1 {
  grid-column: 1 / 2;
}

.item-2 {
  grid-column: 2 / 3;
}

.item-3 {
  grid-column: 3 / 4;
}

In this example, we have a grid container with three columns. The first and third columns are 1 fraction of the available space, and the second column is 2 fractions of the available space. The grid-column property is used to position the items within the grid.

Another example:

<div class="grid-container">
  <div class="item item-1"></div>
  <div class="item item-2"></div>
  <div class="item item-3"></div>
</div>

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

.item {
  background-color: #ccc;
  padding: 10px;
  text-align: center;
}

.item-1 {
  grid-column: 1 / 2;
}

.item-2 {
  grid-column: 2 / 3;
}

.item-3 {
  grid-column: 3 / 4;
}

In this example, we have a grid container with three columns, each with a size of 1 fraction of the available space. The repeat() function is used to repeat the column size definition three times.

The grid-template-columns property can also be used to define the position of grid lines. Grid lines are the lines that separate the columns and rows of the grid. The position of the grid lines can be specified using the grid-template-columns property.

Here is an example:

<div class="grid-container">
  <div class="item item-1"></div>
  <div class="item item-2"></div>
  <div class="item item-3"></div>
</div>

.grid-container {
  display: grid;
  grid-template-columns: 50px 100px 50px;
  grid-template-rows: 50px 100px 50px;
}

.item {
  background-color: #ccc;
  padding: 10px;
  text-align: center;
}

.item-1 {
  grid-column: 1 / 2;
  grid-row: 1 / 2;
}

.item-2 {
  grid-column: 2 / 3;
  grid-row: 2 / 3;
}

.item-3 {
  grid-column: 3 / 4;
  grid-row: 3 / 4;
}

In this example, we have a grid container with three columns and three rows. The size of each column and row is specified using the grid-template-columns and grid-template-rows properties. The grid-column and grid-row properties are used to position the items within the grid.

The grid-template-columns property is a powerful tool for creating complex grid layouts. It allows you to define the size, position, and number of columns in a grid layout. By using this property, you can create responsive and flexible grid layouts that adapt to different screen sizes and devices.

Conclusion

The grid-template-columns property is a key part of the CSS Grid Layout module. It is used to define the columns of a grid container, and it allows you to create complex grid layouts that adapt to different screen sizes and devices. By using this property, you can create responsive and flexible grid layouts that are easy to maintain and update.

References

Activity