Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>

              
            
!

CSS

              
                @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}

              
            
!

JS

              
                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

Console