使标签的整个宽度可点击和响应

所以,我有一个带过滤器的画廊。我需要过滤器(作为标签完成)可以点击,就像目前第一个过滤器“全部”一样。问题是,正如您在调整窗口大小时所看到的那样,标签没有响应。填充以 rems 完成,但 div.filters 不包含其内部可点击性所需的标签填充。当您调整标签填充包含在红色边框内的窗口大小时,如何实现这一点?

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

 :root {
  --turquoise: rgb(56, 184, 201);
  --white: rgb(255, 255, 255);
  --poppyred: rgb(199, 74, 82);
}

ol,
li {
  list-style-type: none;
}


/* 5. FILTERS */

input[type="radio"] {
  display: none;
}

.filters {
  display: flex;
  justify-content: center;
  margin: 2rem 1rem;
  border: 1px solid red;
}

.filter-item {
  display: block;
  text-align: center;
  margin-bottom: .3rem;
  color: var(--white);
}

.filter-item label {
  text-align: center;
  background: var(--turquoise);
  cursor: pointer;
}

.filter-item label:hover {
  background: var(--poppyred);
}

[value="all"]:checked~.grid-gallery [data-category] {
  display: block;
}

[value="branding"]:checked~.grid-gallery .photo:not([data-category~="branding"]),
[value="graphical"]:checked~.grid-gallery .photo:not([data-category~="graphical"]),
[value="interior"]:checked~.grid-gallery .photo:not([data-category~="interior"]),
[value="illustrations"]:checked~.grid-gallery .photo:not([data-category~="illustrations"]),
[value="arts"]:checked~.grid-gallery .photo:not([data-category~="arts"]) {
  display: none;
}


/* Change color on click */

[value="all"]:checked~.filters [for="all"],
[value="branding"]:checked~.filters [for="branding"],
[value="graphical"]:checked~.filters [for="graphical"],
[value="interior"]:checked~.filters [for="interior"],
[value="illustrations"]:checked~.filters [for="illustrations"],
[value="arts"]:checked~.filters [for="arts"] {
  background: var(--poppyred);
}


/* 6. GALLERY */

.grid-gallery {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  margin: 1rem;
}

.photo img {
  height: 100%;
  width: 100%;
}


/*LABEL PADDING*/

.filter-items .filter-item:nth-child(1) label {
  padding: 0 57.8em;
}
<!-- Filters -->
<input type="radio" name="categories" id="all" value="all" checked>
<input type="radio" name="categories" id="branding" value="branding">
<input type="radio" name="categories" id="graphical" value="graphical">
<input type="radio" name="categories" id="interior" value="interior">
<input type="radio" name="categories" id="illustrations" value="illustrations">
<input type="radio" name="categories" id="arts" value="arts">

<div class="filters">
  <ol class="filter-items">
    <li class="filter-item"><label for="all">All</label></li>
    <li class="filter-item"><label for="branding">Branding</label></li>
    <li class="filter-item"><label for="graphical">Graphical Design</label></li>
    <li class="filter-item"><label for="interior">Interior Design</label></li>
    <li class="filter-item"><label for="illustrations">Illustrations</label></li>
    <li class="filter-item"><label for="arts">Arts</label></li>
  </ol>
</div>

<!-- Gallery -->
<div class="grid-gallery">

  <div class="photo" data-category="branding">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="branding">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

  <div class="photo" data-category="graphical">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="arts">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

  <div class="photo" data-category="arts">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="interior">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

  <div class="photo" data-category="graphical">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="branding">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

  <div class="photo" data-category="illustrations">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="interior">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

</div>

这是 Codepen:带有过滤器的 Codepen Gallery

他们离开了。S

你有一个奇怪的填充使“全部”变宽。我会删除它并display: block;在标签上使用在下面的示例中,我做了一些其他更改,<div class="filter"></div>因为您不需要它,因此删除了它。将边距和边框移至<ol>自身。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

 :root {
  --turquoise: rgb(56, 184, 201);
  --white: rgb(255, 255, 255);
  --poppyred: rgb(199, 74, 82);
}

ol,
li {
  list-style-type: none;
}


/* 5. FILTERS */

input[type="radio"] {
  display: none;
}

.filter-items {
  margin: 2rem 1rem;
  border: 1px solid red;
}

.filter-item {
  text-align: center;
  margin-bottom: .3rem;
  color: var(--white);
}

.filter-item:last-child {
  margin-bottom: 0;
}

.filter-item label {
  display: block;
  text-align: center;
  background: var(--turquoise);
  cursor: pointer;
}

.filter-item label:hover {
  background: var(--poppyred);
}

[value="all"]:checked~.grid-gallery [data-category] {
  display: block;
}

[value="branding"]:checked~.grid-gallery .photo:not([data-category~="branding"]),
[value="graphical"]:checked~.grid-gallery .photo:not([data-category~="graphical"]),
[value="interior"]:checked~.grid-gallery .photo:not([data-category~="interior"]),
[value="illustrations"]:checked~.grid-gallery .photo:not([data-category~="illustrations"]),
[value="arts"]:checked~.grid-gallery .photo:not([data-category~="arts"]) {
  display: none;
}


/* Change color on click */

[value="all"]:checked~.filter-items [for="all"],
[value="branding"]:checked~.filter-items [for="branding"],
[value="graphical"]:checked~.filter-items [for="graphical"],
[value="interior"]:checked~.filter-items [for="interior"],
[value="illustrations"]:checked~.filter-items [for="illustrations"],
[value="arts"]:checked~.filter-items [for="arts"] {
  background: var(--poppyred);
}


/* 6. GALLERY */

.grid-gallery {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  margin: 1rem;
}

.photo img {
  height: 100%;
  width: 100%;
}


/*LABEL PADDING

.filter-items .filter-item:nth-child(1) label {
  padding: 0 57.8em;
}
*/
<!-- Filters -->
<input type="radio" name="categories" id="all" value="all" checked>
<input type="radio" name="categories" id="branding" value="branding">
<input type="radio" name="categories" id="graphical" value="graphical">
<input type="radio" name="categories" id="interior" value="interior">
<input type="radio" name="categories" id="illustrations" value="illustrations">
<input type="radio" name="categories" id="arts" value="arts">

<ol class="filter-items">
  <li class="filter-item"><label for="all">All</label></li>
  <li class="filter-item"><label for="branding">Branding</label></li>
  <li class="filter-item"><label for="graphical">Graphical Design</label></li>
  <li class="filter-item"><label for="interior">Interior Design</label></li>
  <li class="filter-item"><label for="illustrations">Illustrations</label></li>
  <li class="filter-item"><label for="arts">Arts</label></li>
</ol>

<!-- Gallery -->
<div class="grid-gallery">

  <div class="photo" data-category="branding">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="branding">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

  <div class="photo" data-category="graphical">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="arts">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

  <div class="photo" data-category="arts">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="interior">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

  <div class="photo" data-category="graphical">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="branding">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

  <div class="photo" data-category="illustrations">
    <img src="https://i.picsum.photos/id/529/200/200.jpg?hmac=LiB-rmOEJ-iPyye6kU2u9mmHGs_o7w5wrCHbzlNX5b0" alt="">
  </div>

  <div class="photo" data-category="interior">
    <img src="https://i.picsum.photos/id/861/200/200.jpg?hmac=UJSK-tjn1gjzSmwHWZhjpaGahNSBDQWpMoNvg8Bxy8k" alt="">
  </div>

</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章