그때 그때/CSS

li 개수에 따른 너비 조절

web_seul 2023. 6. 12. 15:43
반응형

css

li {
  float: left;
}

/* one item */
li:first-child:nth-last-child(1) {
  width: 100%;
}

/* two items */
li:first-child:nth-last-child(2),
li:first-child:nth-last-child(2) ~ li {
  width: 50%;
}

/* three items */
li:first-child:nth-last-child(3),
li:first-child:nth-last-child(3) ~ li {
  width: 33.3333%;
}

/* four items */
li:first-child:nth-last-child(4),
li:first-child:nth-last-child(4) ~ li {
  width: 25%;
}

 

sass

@mixin space-out($min, $max) {
  @for $i from $min through $max {
    &:first-child:nth-last-child(#{$i}),
    &:first-child:nth-last-child(#{$i}) ~ & {
      width: 1 / $i * 100%;
    }
  }
}

li {
  @include space-out(1, 4);
}

 

 

less

.space-out(@min, @max) {
  .space-out-loop(@i) when (@i > 0) {
    .space-out-loop(@i - 1);
    &:first-child:nth-last-child(@{i}),
    &:first-child:nth-last-child(@{i}) ~ & {
      width: 1 / @i * 100%;
    }
  }
  .space-out-loop(@max);
}

li {
  .space-out(1, 4);
}

 

 

참고) https://www.growingwiththeweb.com/2014/06/detecting-number-of-siblings-with-css.html

 

Detecting number of siblings with CSS

I came across a clever CSS technique, originally developed by André Luís in 2009 and later refined by Lea Verou 2 years later; applying a style when the number of siblings is a particular number. It’s definitely worth knowing.

www.growingwiththeweb.com

 

반응형

'그때 그때 > CSS' 카테고리의 다른 글

2023 새로운 css by.google  (0) 2023.12.22
vertical-align  (0) 2023.05.11
input number 커스텀  (0) 2023.04.13
특정상태에서 텍스트가 흐릿하게보일 때  (0) 2023.01.27
input range 커스텀  (0) 2022.09.16