Luke ketchen
Luke ketchen

Reputation: 57

Can I select the last column in a css grid?

I have an unordered list display grid and I have put a border on the right of each item in the grid.


    ul{
      display: grid;
      grid-template-columns: repeat(8,1fr);
      grid-row-gap: 20px;
      grid-column-gap: 18px;
    }

Each item can be different widths so a row might have 4 items and another might have 3.


    li{
      list-style: none;
      grid-column: span 2;
      margin-right: 0;
      height: 200px;
      padding-right: 0;
      position: relative;
      &:after{
        content: '';
        position: absolute;
        right: -10px;
        background-color: #7c7c7c;
        width: 1px;
        height: 100%;
      }
    }
    
    .wide-item{
      grid-column: span 4;
    }

I want to remove the border on the right-side items. I have tried :nth-last-child but as the columns can change I can't do the math this way.

below a full working code:

body {
  margin: 0;
}

.container {
  background-color: darkgrey;
  min-height: 100vh;
  padding: 0;
  margin: 0;
  display: flex;
  align-items: center;
}

ul {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-row-gap: 20px;
  grid-column-gap: 18px;
  width: 50%;
  margin: 0 auto;
}
ul :nth-last-child(4n+1) {
  background-color: teal;
}

li {
  list-style: none;
  grid-column: span 2;
  margin-right: 0;
  height: 200px;
  padding-right: 0;
  position: relative;
}
li:after {
  content: "";
  position: absolute;
  right: -10px;
  background-color: #7c7c7c;
  width: 1px;
  height: 100%;
}

.wide-item {
  grid-column: span 4;
}

.post {
  border-top: 2px solid #000;
  margin-right: 0;
}
<div class="container">
   <ul>
    <li><div class="post">Item 2</div></li>
     <li><div class="post">Item 3</div></li>
     <li class="wide-item"><div class="post">Item 4</div></li>
     <li><div class="post">Item 5</div></li>
     <li><div class="post">Item 6</div></li>
     <li><div class="post">Item 7</div></li>
     <li><div class="post">Item 8</div></li>
     <li><div class="post">Item 9</div></li>
     <li><div class="post">Item 10</div></li>
  </ul> 
</div>

Upvotes: 4

Views: 1443

Answers (2)

R&#250;nar Berg
R&#250;nar Berg

Reputation: 4832

CSS selectors level 4 will introduce several column selectors, such as the column combinator (||), :nth-col pseudo class and—relevant for this question—:nth-last-col pseudo class:

ul {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  list-style: none;
  padding: 0;
}

li {
  background-color: gold;
  border: 1px solid forestgreen;
  block-size: 1em;
}

/* Experimental */
li:nth-last-col(1) {
  background-color: firebrick;
}
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>

Currently (January 2021) no browser has implemented it and I’m not sure any polyfills exists.

Upvotes: 1

Temani Afif
Temani Afif

Reputation: 272842

Do it differently by adding a border before on the left side that you can hide using overflow:hidden

body {
  margin: 0;
}

.container {
  background-color: darkgrey;
  min-height: 100vh;
  padding: 0;
  margin: 0;
  display: flex;
  align-items: center;
}

ul {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-row-gap: 20px;
  grid-column-gap: 18px;
  width: 50%;
  margin: 0 auto;
  padding:0;
  overflow:hidden; /* added */
}

ul :nth-last-child(4n+1) {
  background-color: teal;
}

li {
  list-style: none;
  grid-column: span 2;
  margin-right: 0;
  height: 200px;
  padding-right: 0;
  position: relative;
}

li:after {
  content: '';
  position: absolute;
  left: -10px; /* changed */
  background-color: #7c7c7c;
  width: 1px;
  height: 100%;
}

.wide-item {
  grid-column: span 4;
}

.post {
  border-top: 2px solid #000;
  margin-right: 0;
}
<div class="container">
  <ul>
    <li>
      <div class="post">Item 2</div>
    </li>
    <li>
      <div class="post">Item 3</div>
    </li>
    <li class="wide-item">
      <div class="post">Item 4</div>
    </li>
    <li>
      <div class="post">Item 5</div>
    </li>
    <li>
      <div class="post">Item 6</div>
    </li>
    <li>
      <div class="post">Item 7</div>
    </li>
    <li>
      <div class="post">Item 8</div>
    </li>
    <li>
      <div class="post">Item 9</div>
    </li>
    <li>
      <div class="post">Item 10</div>
    </li>
  </ul>
</div>

Upvotes: 1

Related Questions