123

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.

            
              <div class="inherit-vars">
  <p style="padding: 0 10px;">This demo shows the exact same component in different widths and contexts (dark or light). The component uses CSS conditions instead of media queries in order to respond to the context it was placed in. It also uses fluid typography that is based on the width of the container instead of the viewport. All CSS only, no JavaScript involved 🎉
    <br>For details about how this works, take a look at my blog post: <a href="https://au.si/css-conditions-today" target="_blank">au.si/css-conditions-today</a>
    <br><small>Works currently only in Firefox and Chrome</small></p>
  <div class="component">
    <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
    <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
    <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
    <small class="component-date">2019/01/18</small>
    <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
  </div>
  <div class="columns c2">
    <div class="inherit-vars">
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
    </div>
  </div>
  <div class="columns c3">
    <div class="inherit-vars">
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
    </div>
  </div>
  <div class="columns c4">
    <div class="inherit-vars">
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
    </div>
  </div>
  <div class="invert-colors">
    <div class="inherit-vars">
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="columns c2">
        <div class="inherit-vars">
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
        </div>
      </div>
      <div class="columns c3">
        <div class="inherit-vars">
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
        </div>
      </div>
      <div class="columns c4">
        <div class="inherit-vars">
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="very-wide">
    <div class="inherit-vars">
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="invert-colors">
        <div class="inherit-vars">
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="very-narrow">
    <div class="inherit-vars">
      <div class="component">
        <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
        <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
        <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
        <small class="component-date">2019/01/18</small>
        <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
      </div>
      <div class="invert-colors">
        <div class="inherit-vars">
          <div class="component">
            <h2 class="component-headline">Lorem Ipsum Dolor sit Amet</h2>
            <img class="component-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='300' style='background: %23666;'%3E%3Cpath d='M0,0l400,300L0,300' fill='%23999'/%3E%3C/svg%3E">
            <p class="component-text">The source code of this component is the same across the whole page. It is styled using conditional CSS without media queries.</p>
            <small class="component-date">2019/01/18</small>
            <p class="component-stars">⭐️⭐️⭐️⭐️⭐️</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
            
          
!
            
              :root {
  box-sizing: border-box;
  --dark-mode: 0;
  --set-width: var(--width);
  --set-dark-mode: var(--dark-mode);
  /* Take a look at the JS tab to see how the --width property gets set */
}
@media (prefers-color-scheme: dark) {
  :root {
    --dark-mode: 1;
    background: black;
    color: white;
  }
}
*, :after, :before {
    box-sizing: inherit;
}
body {
  font: 100%/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  margin: 5px;
  /* Reduce the width by 10 pixels because of the padding */
  --set-width: calc(var(--width) - 10);
}
.inherit-vars {
  /*
  This element flips the variable pairs back,
  as a workaround for the missing parent-var()
  */
  --width: var(--set-width);
  --dark-mode: var(--set-dark-mode);
  display: contents;
}
.invert-colors {
  /* 
  This element inverts the dark-mode, 
  dark becomes light and light bekomes dark 
  */
  padding: 5px;
  margin-left: -5px;
  margin-right: -5px;
  background: rgb(
    calc(var(--dark-mode) * 255),
    calc(var(--dark-mode) * 255),
    calc(var(--dark-mode) * 255)
  );
  --set-dark-mode: calc(1 - var(--dark-mode));
}
.columns {
  display: flex;
}
.columns.c2 {
  /* For two columns the width for the children gets set to half */
  --set-width: calc(var(--width) / 2);
}
.columns.c3 {
  /* For three columns the width for the children gets set to one third */
  --set-width: calc(var(--width) / 3);
}
.columns.c4 {
  /* For four columns the width for the children gets set to one fourth */
  --set-width: calc(var(--width) / 4);
}
.very-wide {
  width: 1680px;
  --set-width: 1680;
}
.very-narrow {
  width: 100px;
  --set-width: 100;
}

.component {
  display: grid;
  grid-gap: 10px;
  font-size: calc(10px + (var(--width) * 0.01px));
  color: rgb(calc(var(--dark-mode) * 255), calc(var(--dark-mode) * 255), calc(var(--dark-mode) * 255));
  grid-template-areas:
    "headline"
    "image"
    "date"
    "text"
    "stars"
  ;
  /*
  This is where the actual magic happens.
  The paused “component” animation applies the styles based on the --width.
  */
  animation:
    10000s step-end calc((0 - var(--width)) * 1s) component forwards paused,
    1s step-end calc((var(--dark-mode) - 1) * 1s) component-dark-mode forwards paused
  ;
  padding: 10px;
  margin: 5px;
  border: 2px solid #ccc;
  hyphens: auto;
}
@keyframes component {
  /* (min-width: 200px) */ 2% {
    grid-template:
      "headline headline"
      "image    image"
      "date     stars"
      "text     text" /
       auto     min-content
    ;
  }
  /* (min-width: 400px) */ 4% {
    grid-template:
      "headline headline stars"
      "image    text     text"
      "image    date     date" /
       150px    auto     auto
    ;
  }
  /* (min-width: 600px) */ 6% {
    grid-template:
      "image headline headline"
      "image text     text"
      "image date     stars" /
       200px auto     auto
    ;
    font-size: 1em;
  }
  /* (min-width: 800px) */ 8% {
    grid-template:
      "image headline date stars"
      "image text     text text" /
       150px auto     min-content min-content
    ;
  }
  /* (min-width: 1400px) */ 14% {
    grid-template:
      "image headline    text date        stars" /
       60px  max-content auto min-content min-content
    ;
    font-size: 0.85em;
  }
  /* (min-width: 1550px) */ 15.5% {
    font-size: 1em;
  }
}
@keyframes component-dark-mode {
  from {
    font-weight: 100;
    border-color: #333;
    border-width: 0;
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.15) 0%, rgba(255, 255, 255, 0.05) 100%)
  }
}
.component-headline {
  margin: 0;
  font-size: 1.2em;
  grid-area: headline;
  align-self: baseline;
  animation: 
    10000s step-end calc((0 - var(--width)) * 1s) component-headline forwards paused,
    1s step-end calc((var(--dark-mode) - 1) * 1s) component-headline-dark-mode forwards paused
  ;
}
@keyframes component-headline {
  /* (min-width: 1400px) */ 14% {
    text-transform: uppercase;
    font-size: 1em;
  }
}
@keyframes component-headline-dark-mode {
  from {
    font-weight: 300;
  }
}
.component-image {
  width: calc(100% + 20px);
  margin-right: -10px;
  margin-left: -10px;
  grid-area: image;
  animation: 
    10000s step-end calc((0 - var(--width)) * 1s) component-image forwards paused
  ;
}
@keyframes component-image {
  /* (min-width: 200px) */ 2% {
    width: 100%;
    margin-right: 0;
    margin-left: 0;
  }
  /* (min-width: 800px) */ 8% {
    margin-top: -10px;
    margin-bottom: -10px;
    margin-left: -10px;
  }
}
.component-text {
  margin: 0;
  grid-area: text;
  align-self: baseline;
}
.component-date {
  font-size: 0.7em;
  grid-area: date;
  align-self: baseline;
  animation: 
    10000s step-end calc((0 - var(--width)) * 1s) component-date forwards paused
  ;
}
@keyframes component-date {
  /* (min-width: 100px) */ 1% {
    font-size: 0.9em;
  }
}
.component-stars {
  margin: 0;
  grid-area: stars;
  align-self: baseline;
  font-size: calc(var(--width) * 0.08px);
  animation: 
    10000s step-end calc((0 - var(--width)) * 1s) component-stars forwards paused
  ;
}
@keyframes component-stars {
  /* (min-width: 200px) */ 2% {
    font-size: 1em;
    text-align: right;
  }
}
            
          
!
            
              /*
Yes. I know. I said “no JavaScript involved”. 😜
This script just generates some static CSS that sets the --width property based on the viewport. If you want to avoid that script you can simply copy the generated CSS code.
*/
(() => {
  var style = document.createElement('style');
  var css = ':root { --width: 0 }\n';
  for (var i = 0; i < 5000; i+=1) {
    css += `@media (min-width: ${i}px) and (max-width: ${i+1}px) { :root { --width: ${i} } }`;
  }
  css += `@media (min-width: ${i}px) { :root { --width: ${i} } }\n`;
  style.innerHTML = css;
  document.head.appendChild(style);
})();

/* Bugfix for font-size issues */
(() => {
  document.body.style.cssText = '--set-width:calc(var(--width) + 1)';
  setTimeout(() => {
    document.body.style.cssText = '';
  });
})();
            
          
!
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