css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <header>
  <h1>Select Middle Element</h1>
  <label for="items">Items: <input id="items" type="number" value="6" min="1" max="14"/></label>
</header>
<p><strong>Objective:</strong> Select the middle element in a list of variable length.</p>
<input id="half" type="checkbox"/> <label for="half">Select the second half of the list.</label>
<div class="demo">
  <div>
    <strong>None:</strong>
    <ul class="middle">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ul>
  </div>
  <div>
    <strong>Before:</strong>
    <ul class="middle before">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ul>
  </div>
  <div>
    <strong>After:</strong>
    <ul class="middle after">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ul>
  </div>
  <div>
    <strong>Both:</strong>
    <ul class="middle both">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ul>
  </div>
</div>

            
          
!
            
              @function in-list(
  $needle,
  $haystack
){
  @return not not index($haystack, $needle);
}

///
/// Select middle child
///
/// @param {Number} $max-children - The maximun number of children expected.
/// NOTE: Be as specifc as possible, as the value directly correlates to the
/// the length of the selector
/// @param {String} $when-even ['both'] - The element to be deemed "middle" when an even
/// number of children is present. Possible values: `before`, `after`, `both`, `none`
///
/// @return {List} - A list of selectors
///
@function middle-child(
  $max-children,
  $min-children: 1,
  $when-even: 'both',
  $base-selector: "&"
){
  $selector: join((),(),comma);

  // It is possible to select the middle child
  // of all of an element's direct descendants
  // by setting the base selector
  // $base-selector: "& > *";
  
  @for $i from ceil($min-children/2) through floor($max-children/2+1) {
    $selector: append($selector, "#{$base-selector}:nth-child(#{$i}):nth-last-child(#{$i})");

    @if not in-list($when-even, ('before', 'none')) {
      $selector: append($selector, "#{$base-selector}:nth-child(#{$i}):nth-last-child(#{$i - 1})");
    }

    @if not in-list($when-even, ('after', 'none')) {
      $selector: append($selector, "#{$base-selector}:nth-child(#{$i}):nth-last-child(#{$i + 1})");
    }
  }

  @return $selector;
}

%circle {
  content: '';
  position: absolute;
  border: 2px solid red;
  border-radius: 99in;
  width: 1.5em;
  height: 1.5em;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.middle {
  & > * {
    #{middle-child(14, $when-even: 'none')} {
      &::after {
        @extend %circle;
      }
    }
  }
  &.before {
    & > * {
      #{middle-child(14, $when-even: 'before')} {
        &::after {
          @extend %circle;
        }
      }
    }
  }
  &.after {
    & > * {
      #{middle-child(14, $when-even: 'after')} {
        &::after {
          @extend %circle;
        }
      }
    }
  }
  &.both {
    & > * {
      #{middle-child(14, $when-even: 'both')} {
        &::after {
          @extend %circle;
        }
      }
    }
  }
}

.half {
  & > * {
    #{middle-child(14, $when-even: 'after')} {
      &::after,
      & ~ *::after {
        @extend %circle;
        border-color: blue;
      }
    }
  }
}

.demo {
   div {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    margin: 1em 0;
  }
  
  strong {
    width: 5em;
  }
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h1 {
  margin: 0;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

li {
  display: inline-block;
  margin-right: 1em;
  position: relative;
}

body {
  padding: 1em;
  width: 500px;
}

// Center using flexbox
:root{height:100%;display:flex}:root>*{margin:auto}

            
          
!
            
              let $=(s,o=document)=>o.querySelector(s),
    $$=(s,o=document)=>o.querySelectorAll(s);

items.addEventListener('input', function(){
  t=this
  $$('ul').forEach((e)=>e.innerHTML='<li>'+Array(+t.value).fill(0).map((_, i) => i+1).join('</li><li>')+'</li>')
})

half.addEventListener('change', function(){
  t=this
  $$('ul').forEach((e)=>e.classList.toggle('half',t.checked))
})

            
          
!
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.
Loading ..................

Console