Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h2>Collapsable link lists with CSS quantity query (no JS)</h2>

<input type="checkbox" id="list1" class="toggle" />
<div class="list">
  <h4 class="title">Lots of links</h4>
  <div class="item"><a href="" class="link">Matförpackningar</a></div>
  <div class="item"><a href="" class="link">Servering</a></div>
  <div class="item"><a href="" class="link">Städ &amp; Hygien</a></div>
  <div class="item"><a href="" class="link">Påsar</a></div>
  <div class="item"><a href="" class="link">Emballage</a></div>
  <div class="item"><a href="" class="link">Kontor</a></div>
  <div class="item"><a href="" class="link">Inredning</a></div>
  <div class="item"><a href="" class="link">Reklamationer</a></div>
  <div class="item"><a href="" class="link">Leverans</a></div>
  <div class="item"><a href="" class="link">Fakturor</a></div>
  <div class="item"><a href="" class="link">Påsar</a></div>
  <div class="item"><a href="" class="link">Emballage</a></div>
  <div class="item"><a href="" class="link">Kontor</a></div>
  <label for="list1" class="more">
    <span class="more__icon">▾</span>
    <span class="more__text" data-toggle-open="View all" data-toggle-close="View less"></span>
  </label>
</div>

<input type="checkbox" id="list2" class="toggle" />
<div class="list" data-js-linklist>
  <h4 class="title">Not so many links</h4>
  <div class="item"><a href="" class="link">This list also has</a></div>
  <div class="item"><a href="" class="link">the "view all" button,</a></div>
  <div class="item"><a href="" class="link">but doesn't</a></div>
  <div class="item"><a href="" class="link">exceed the 5 link max.</a></div>
  <div class="item"><a href="" class="link">So no "view all" button!</a></div>
  <label for="list2" class="more">
    <span class="more__icon">▾</span>
    <span class="more__text" data-toggle-open="View all" data-toggle-close="View less"></span>
  </label>
</div>

<hr style="clear:both">

<p>Lists that only show a set amount of rows, before hiding the overspill behind a "view all" link. But without having to do any server side-markup, javascript logic or setting styles/classes. In fact, the markup is just a list with a hidden checkbox at one end and an empty div at the other. CSS deals with all the show/hide logic, the click events and animation. No touching the DOM or class additions. No failure if JS doesn't load. No onload ficker as JS calcs and hides overspill rows.</p>

<p>It uses <code>nth-of-type</code> CSS to collapse rows 6 or greater, then a <code>~</code> sibling selector to enable the "view all" button (a basic quantity query).</p>

<p>The "view all" link is content-transparent - as it's just UI polish - it uses a combination of data-attributes on the tag and CSS pseudo-elements to create the button text.</p>

<p>The click event is simply run by the "view all" button actually being a label for a hidden checkbox. And we use a sibling selector <code>input:checked+div</code> to control the max-height and opacity of the hidden rows.</p>

<p>Lastly, just a small amount of CSS transitions to tie it together (but transitioning max-height is janky, so it's staggered with the smoother opacity transition to reduce visible jank)</p>

<p>Down sides? Up to IE8 doesn't support the <code>input:checked</code> selector, but then they wouldn't supprt <code>nth-of-type</code> either, so in the spirit of progressive enhancement, users would just see the whole expanded list. It does <b>not</b> have the prettiest or lowest specificity CSS selectors you're ever seen, but you're trading JS+DOM manipulation for it. Fastclick.js goes crazy when dealing with label-checkbox interactions, and I've yet to figure out a solution for that. And I'm still not sure of the accessibility implications, but it seems a better candidate for good accessibilty than many other more traditional JS soltutions.</p>
            
          
!
            
              body {
  margin: 20px;
  font-family: avenir,sans-serif;
}

.toggle {
  display: none;
}
.list {
  display: inline-block;
  vertical-align: top;
  width: 200px;
  margin: 0 20px 20px 0;
  padding: 10px;
  border: 1px solid #000;
}
.title {
  margin: 0 0 1em;
  padding: 0.25em 5px;
}
.more {
  display: none;
  padding: 0.25em 5px;
  cursor: pointer;
  text-transform: uppercase;
  border: 1px dashed #000;
  &__text {
    &:before {
      content: attr(data-toggle-open);
    }
  }
  &__icon {
    display: inline-block;
    vertical-align: middle;
    margin-right: 0.3em;
    line-height: 0.9;
    transform: rotate(0deg);
    transition: transform 500ms;
    transition-delay: 700ms;
  }
  &:hover {
    .no-touch & {
      background-color: #eee;
    }
  }
}
.item {
  overflow: hidden;
  &:nth-of-type(n+6) {
    max-height: 0;
    opacity: 0;
    transition: opacity 500ms, max-height 200ms;
    transition-delay: 0s, 500ms;
    &~.more {
      display: block;
    }
    .toggle:checked+.list & {
      max-height: 4em;
      opacity: 1;
      transition-delay: 200ms, 0s;
      ~.more {
        .more__text {
          &:before {
            content: attr(data-toggle-close);
          }
        }
        .more__icon {
          transform: rotate(180deg);
          transition-delay: 200ms;
        }
      }
    }
  }
}
.link {
  display: block;
  padding: 0.25em 5px;
  color: #000;
  text-decoration: none;
  transition: background-color 500ms;
  &:hover {
    .no-touch & {
      background-color: #eee;
    }
  }
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console