quantity-queries icon indicating copy to clipboard operation
quantity-queries copied to clipboard

Add first-row last-row mixins

Open nirazul opened this issue 9 years ago • 6 comments

Thanks for the nice compilation of mixins! I think it would be a cool addition to also implement first-row and last-row mixins displayed in this article.

I occasionally use this mixin for targeting last rows:

/// Targets the last row in a grid of constant column count
/// @link http://keithclark.co.uk/articles/targeting-first-and-last-rows-in-css-grid-layouts/
/// @param {number} $num-cols - The number of columns within the context
///
@mixin last-row($num-cols) {
  $selector: nth(nth(&, 1), length(nth(&, 1)));

  &:nth-child(#{$num-cols}n + 1):nth-last-child(-n + #{$num-cols}),
  &:nth-child(#{$num-cols}n + 1):nth-last-child(-n + #{$num-cols}) ~ #{$selector} {
    @content;
  }
}

/// Usage
///
ul > li {
  @include last-row(2) {
    color: red
  }
}

nirazul avatar Sep 16 '16 06:09 nirazul

Very interesting. I'll take a look at it. Thanks for the suggestion @Nirazul!

danielguillan avatar Sep 17 '16 11:09 danielguillan

I've found a use for that!

    &:last-child {
      border-bottom: 0;
    }

    @media screen and(min-width: 40.5em) { // 648px
      @include my.last-row(2) {
        border-bottom: 0;
      }
    }

    @media screen and(min-width: 59.25em) { // 948px
      @include my.last-row(3) {
        border-bottom: 0;
      }
    }

robsonsobral avatar Oct 29 '21 17:10 robsonsobral

@danielguillan , would you accept a PR?

robsonsobral avatar Mar 25 '22 20:03 robsonsobral

@danielguillan ?

robsonsobral avatar Dec 10 '22 22:12 robsonsobral

I did some tests.

Based on this article, we could have something like:

ul > li {
    @include first(4) { color: red; }
}

ul > li {
    @include last(6) { color: blue; }
}

ul > li {
    @include last-or-remainder(6) { color: blue; }
}
ul > li:nth-child(-n+4) {
  color: red;
}

ul > li:nth-child(6n+1):nth-last-child(-n+6) {
  color: blue;
}

ul > li:nth-child(6n+1):nth-last-child(-n+6),
ul > li:nth-child(6n+1):nth-last-child(-n+6) ~ li {
  color: blue;
}

However, in these cases the total quantity doesn't matter, so... I'm not sure these mixins have a place in here. I think they do, but I'm not the boss.

robsonsobral avatar Dec 11 '22 20:12 robsonsobral

Don't you want another PR, @danielguillan ?

robsonsobral avatar Dec 14 '22 14:12 robsonsobral