Categories
CSS Web development

CSS grid-column-end

CSS grid-column-end

The CSS grid-column-end property specifies how many columns an item will span, or on which column-line the item will end.

Syntax:

grid-column-end: auto|span n|column-line;

auto (default) – the item will span one column.

span n – defines the number of columns the item will span.

column-line – defines on which column to end the display of the item.

Example1:

Item1 will span 2 columns:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-gap: 10px;
  background-color: #e9c46a;
  padding: 10px;
}

.grid-container > div {
  background-color: #f4a261;
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}

.item1 {
  grid-column-end: span 2;
}
</style>
</head>
<body>

<div class="grid-container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>  
  <div class="item4">4</div>
  <div class="item5">5</div>
  <div class="item6">6</div>
  <div class="item7">7</div>
</div>

</body>
</html>

Output:

1
2
3
4
5
6
7

Example2:

Item-1 will end on column line 3:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container1 {
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-gap: 10px;
  background-color: #e9c46a;
  padding: 10px;
}

.grid-container1 > div {
  background-color: #f4a261;
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}

.item-1 {
  grid-column-end: 3;
}
</style>
</head>
<body>

<div class="grid-container1">
  <div class="item-1">1</div>
  <div class="item-2">2</div>
  <div class="item-3">3</div>  
  <div class="item-4">4</div>
  <div class="item-5">5</div>
  <div class="item-6">6</div>
</div>

</body>
</html>

Output:

1
2
3
4
5
6

Enjoy coding!