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



grid-column-end

CSS Grid Layout is a powerful tool for creating complex layouts on the web. It allows you to divide a web page into rows and columns, and then place content within those rows and columns. One of the most important properties in CSS Grid Layout is the grid-column-end property. In this article, we will explore what this property is, how it works, and how you can use it to create beautiful and responsive layouts.

What is grid-column-end?

The grid-column-end property is used to specify the end position of a grid item within a grid column. It defines the grid line where the item should end. This property works in conjunction with the grid-column-start property, which specifies the starting position of the grid item within the grid column.

Together, these two properties allow you to define the size and position of a grid item within a grid column. You can use them to create complex layouts that adapt to different screen sizes and devices.

How does grid-column-end work?

The grid-column-end property works by specifying the grid line where the grid item should end. Grid lines are imaginary lines that divide the grid into rows and columns. They are numbered starting from 1, and can be referred to using positive integers, negative integers, or the span keyword.

Here is an example of how the grid-column-end property works:

<div class="grid-container">
  <div class="grid-item" style="grid-column-start: 1; grid-column-end: 3;">Item 1</div>
  <div class="grid-item" style="grid-column-start: 3; grid-column-end: 5;">Item 2</div>
  <div class="grid-item" style="grid-column-start: 1; grid-column-end: 6;">Item 3</div>
</div>

In this example, we have a grid container with three grid items. The first grid item starts at grid line 1 and ends at grid line 3. The second grid item starts at grid line 3 and ends at grid line 5. The third grid item starts at grid line 1 and ends at grid line 6.

By specifying the start and end positions of each grid item, we can create a complex layout that adapts to different screen sizes and devices.

Code Examples

Here are some code examples that demonstrate how to use the grid-column-end property:

Example 1

<div class="grid-container">
  <div class="grid-item" style="grid-column-start: 1; grid-column-end: 3;">Item 1</div>
  <div class="grid-item" style="grid-column-start: 3; grid-column-end: 5;">Item 2</div>
  <div class="grid-item" style="grid-column-start: 1; grid-column-end: 6;">Item 3</div>
</div>

In this example, we have a grid container with three grid items. The first grid item starts at grid line 1 and ends at grid line 3. The second grid item starts at grid line 3 and ends at grid line 5. The third grid item starts at grid line 1 and ends at grid line 6.

Example 2

<div class="grid-container">
  <div class="grid-item" style="grid-column-start: 1; grid-column-end: span 2;">Item 1</div>
  <div class="grid-item" style="grid-column-start: 3; grid-column-end: span 2;">Item 2</div>
  <div class="grid-item" style="grid-column-start: 1; grid-column-end: span 6;">Item 3</div>
</div>

In this example, we have a grid container with three grid items. The first grid item starts at grid line 1 and spans 2 columns. The second grid item starts at grid line 3 and spans 2 columns. The third grid item starts at grid line 1 and spans 6 columns.

Example 3

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 100px);
  grid-gap: 10px;
}

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

.item-1 {
  grid-column-start: 1;
  grid-column-end: 3;
}

.item-2 {
  grid-column-start: 2;
  grid-column-end: 4;
}

.item-3 {
  grid-column-start: 3;
  grid-column-end: 4;
  grid-row-start: 2;
  grid-row-end: 4;
}

In this example, we have a grid container with three rows and three columns. We have also specified a grid gap of 10 pixels between each grid item. The first grid item starts at grid line 1 and ends at grid line 3. The second grid item starts at grid line 2 and ends at grid line 4. The third grid item starts at grid line 3 and ends at grid line 4, and spans two rows.

Conclusion

The grid-column-end property is a powerful tool for creating complex layouts on the web. It allows you to specify the end position of a grid item within a grid column, and works in conjunction with the grid-column-start property to define the size and position of a grid item within a grid column. By using these properties, you can create beautiful and responsive layouts that adapt to different screen sizes and devices.

References

Activity