18 · Show more

Show more

Reveal more items without slice() or JS

:has() Chrome 105+ · wide support
← Index

Show more without slice()

The classic antipattern hides items by slicing an array in JS (slice(0, n)) and re-renders the list on every click. Same result, in pure CSS:

The JS antipattern

js
// Antipatrón: ocultar elementos cortando un array con JS
let visibles = items.slice(0, 3);   // estado inicial
visibles = items.slice(0, 6);      // al hacer click en "Ver más"

// Cada click re-renderiza la lista entera desde JS
btn.onclick = () => {
  visibles = items.slice(0, visibles.length + 3);
  render();
};

Pure CSS with :has()

  • 🎧 Amber‑01 — Auriculares
  • ⌨️ Teclado mecánico
  • 🖱️ Mouse ergonómico
  • 📱 Soporte para celular
  • 💡 Lámpara LED
  • 🧹 Limpiador de teclado

A hidden <input type="checkbox"> drives visibility. :has(:checked) reveals the extra items and swaps the label — no JavaScript at all.

Why it matters

Less JS shipped, state lives in the DOM, and the content is already in the HTML (better for SEO and screen readers) instead of being injected by script.

html
<!-- Sin JS: :has() reacciona al estado del checkbox -->
<input type="checkbox" id="sm-demo" class="sm-toggle">
<ul class="sm-list">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li class="sm-extra">Item 4</li>
  <li class="sm-extra">Item 5</li>
  <li class="sm-extra">Item 6</li>
</ul>
<label for="sm-demo" class="sm-btn">
  <span class="sm-more">Ver más</span>
  <span class="sm-less">Ver menos</span>
</label>

An honest note on support

Many of these APIs are recent and not yet in every browser (see the pill on each section). The nice part is they all degrade gracefully: the carousel still scrolls, the accordion opens/closes, the textarea works — just without the animated "plus".