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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="container">

  <button>General Sibling Selector (<code>~</code>)</button>
  <button>Adjacent Sibling Selector (<code>+</code>)</button>
  <button>Clickable Label for Checkbox</button>

  <h3>Click on the buttons above to learn more about the related CSS concepts</h3>

  <!--CORRESPONDS TO THE GENERAL SIBLING SELECTOR-->
  <p class="general"><strong>Description:</strong> This selector will select any sibling(s) (in our case the first three paragraphs) that follow(s) the former specified element (in our case the <code>h3</code> element) supposing that both share the same parent.
    <span><strong>Example:</strong> <code>h3 ~ p { border: 1px solid red; }</code>.</span>
  </p>

  <!--CORRESPONDS TO THE ADJACENT SIBLING SELECTOR-->
  <p class="adjacent"><strong>Description:</strong> This selector will select only the first sibling (in our case the first paragraph) that immediately follows the former specified element (in our case the h3 element).
    <span><strong>Example:</strong> <code>h3 + p { border: 1px solid red; }</code>. </span>
  </p>

  <!--CORRESPONDS TO THE CHECKBOX-->
  <section class="label">
    <p>There are two possible ways to associate a <code>label</code> with a form control (in our case <code>the input[type="checkbox"]</code> element).</p>
    <p>First, by taking advantage of the label's <code>for</code> attribute. The value of it should match the <code>id</code> attribute value of the corresponding checkbox (see example code for more info).
      <span><strong>Example:</strong></span>
    </p>

    <input type="checkbox" name="checkbox" id="check_id" value="value">
    <label for="check_id">Label for Checkbox</label>

    <p>Second, by wrapping the form control within a <code>label</code> element (see example code for more info).
      <span><strong>Example:</strong></span>
    </p>
    <label>
      <input type="checkbox" name="checkbox" value="value">Label for Checkbox
    </label>

    <h4><code>:checked</code> pseudo-class</h4>
    <p>The <code>:checked</code> pseudo-class matches checkboxes and radio buttons that are checked ("on" state).</p>
    <p>This capability provides us a way to mimic the Javascript's <code>onclick</code> event.</p>
    <p>The <strong>checkbox hack</strong> is a way to create Javacript-based things like accordions and off canvas menus by taking advantage of the <code>:checked</code> pseudo-class and the label that is associated to this checkbox.</p>
    <span class="last"><strong>Example:</strong></span>
    <p>In the example below, we're using the adjacent sibling selector along with the <code>:checked</code> pseudo-class to change the appearance of the label. So, first we hide the checkbox. Then, when we click on the corresponding label we set its background
      color to yellow.</p>
    <div class="checkbox-example">
      <input type="checkbox" id="check_example">
      <label for="check_example">When you click me. I 'll have the yellow as background color.</label>
    </div>

    <h4>Browser Support</h4>
    <table>
      <thead>
        <tr>
          <th>Chrome</th>
          <th>Mozilla</th>
          <th>IE</th>
          <th>Opera</th>
          <th>Safari</th>
        </tr </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td>1</td>
            <td>9</td>
            <td>9</td>
            <td>3.1</td>
          </tr>
        </tbody>
    </table>
  </section>

  <!--CORRESPONDS TO THE GENERAL AND ADJACENT SIBLING SELECTORS-->
  <section class="example">
    <h3>This is an <code>h3</code></h3>
    <p>This is the first paragraph</p>
    <p>This is the second paragraph</p>
    <p>This is the third paragraph</p>
    <div>
      <p>This is a paragraph nested inside a <code>div</code></p>
    </div>
  </section>

  <!--CORRESPONDS TO THE GENERAL AND ADJACENT SIBLING SELECTORS-->
  <section class="br-support">
    <h4>Browser Support</h4>
    <table>
      <thead>
        <tr>
          <th>Chrome</th>
          <th>Mozilla</th>
          <th>IE</th>
          <th>Opera</th>
          <th>Safari</th>
        </tr </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td>1</td>
            <td>7</td>
            <td>9</td>
            <td>3</td>
          </tr>
        </tbody>
    </table>
  </section>

  <!--CORRESPONDS TO THE GENERAL SIBLING SELECTOR-->
  <section class="rs-general">
    <h4>Useful Resources</h4>
    <ul>
      <li><a href="http://dev.w3.org/csswg/selectors-3/#general-sibling-combinators">http://dev.w3.org/csswg/selectors-3/#general-sibling-combinators</a></li>
      <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors">https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors</a></li>
    </ul>
  </section>

  <!--CORRESPONDS TO THE ADJACENT SIBLING SELECTOR-->
  <section class="rs-adjacent">
    <h4>Useful Resources</h4>
    <ul>
      <li><a href="http://dev.w3.org/csswg/selectors-3/#adjacent-sibling-combinators">http://dev.w3.org/csswg/selectors-3/#adjacent-sibling-combinators</a></li>
      <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors" target="_blank">https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors</a></li>
    </ul>
  </section>

  <!--CORRESPONDS TO THE CHECKBOX-->
  <section class="rs-checkbox">
    <h4>Useful Resources</h4>
    <ul>
      <li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/checkbox">https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/checkbox</a></li>
      <li><a href="http://dev.w3.org/csswg/selectors-4/#checked">  http://dev.w3.org/csswg/selectors-4/#checked</a></li>
      <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/%3Achecked">https://developer.mozilla.org/en-US/docs/Web/CSS/%3Achecked</a></li>
      <li><a href="http://stackoverflow.com/questions/6293588/how-to-create-an-html-checkbox-with-a-clickable-label">  http://stackoverflow.com/questions/6293588/how-to-create-an-html-checkbox-with-a-clickable-label</a></li>
    </ul>
    <h4>Articles on SitePoint related to the checkbox (or radio button) hack</h4>
    <ul>
      <li><a href="http://www.sitepoint.com/replacing-radio-buttons-without-replacing-radio-buttons/">Replacing Radio Buttons Without Replacing Radio Buttons</a></li>
      <li><a href="http://www.sitepoint.com/pure-css-off-screen-navigation-menu/">Pure CSS Off-screen Navigation Menu</a></li>
    </ul>
  </section>

</div>
              
            
!

CSS

              
                .container {
  max-width: 700px;
  margin: auto;
  text-align: center;
  padding: 0 20px;
  line-height: 1.3;
}

.container * {
  text-align: left;
}

button {
  margin-top: 20px;
}

button:first-child,
input {
  margin-left: 10px;
}

.general,
.adjacent,
section {
  display: none;
}

.container span,
.container a {
  display: block;
}

span {
  margin-top: 10px;
  margin-bottom: 30px;
}

.label span {
  margin-bottom: 0;
}

[type="checkbox"] + label {
  margin-bottom: 20px;
}

p:not(.general):not(.adjacent) {
  width: 80%;
}

section,
span,
strong,
[type="checkbox"]:last-child,
div,
code {
  margin-left: 0;
}

table th,
table td {
  border: 1px solid #ccc;
}

table th {
  padding: 10px;
}

table td {
  padding: 5px;
  text-align: center;
}

.active {
  background: tomato;
}

.borderClass {
  border: 1px solid tomato;
}

.last {
  margin-left: 10px;
}
/* CHECKBOX STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.checkbox-example [type="checkbox"] {
  position: absolute;
  left: -9999px;
}

.checkbox-example [type="checkbox"] + label {
  width: 20%;
  display: block;
  cursor: pointer;
  padding: 5px;
  border: 1px solid tomato;
  margin-top: 30px;
}

.checkbox-example [type="checkbox"]:checked + label {
  background: yellow;
}
              
            
!

JS

              
                // Demo by George Martsoukos. See article:

// FIRST BUTTON
$('button:nth-child(1)').on('click', function() {
  $('h3:first-of-type, .adjacent, .label, .rs-adjacent, .rs-checkbox').hide();
  $('.general, .example, .br-support, .rs-general').show();
  $('p').removeClass('borderClass');
  $('button').removeClass('active');
  $('.example h3 ~ p').addClass('borderClass');
  $(this).addClass('active');
});

// SECOND BUTTON
$('button:nth-child(2)').on('click', function() {
  $('h3:first-of-type, .general, .label, .rs-general, .rs-checkbox').hide();
  $('.adjacent, .example, .br-support, .rs-adjacent').show();
  $('p').removeClass('borderClass');
  $('button').removeClass('active');
  $('.example h3 + p').addClass('borderClass');
  $(this).addClass('active');
});

// THIRD BUTTON
$('button:nth-child(3)').on('click', function() {
  $('h3:first-of-type, .adjacent, .general, .example, .br-support, .rs-general, .rs-adjacent').hide();
  $('.label, .rs-checkbox').show();
  $('button').removeClass('active');
  $(this).addClass('active');
});
              
            
!
999px

Console