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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <p><span>Demo #1:</span> This example shows one usecase for style queries on immediate parents. <i> This is an italicised quote in a regular paragraph. </i>I want it to look italic to stand out from the rest of the text.</p>

<blockquote>This is a blockquote that is styled italic. If I have an element within it that I want to stand out using the <code>&lt;i&gt;</code> tag, it will not stand out because they'll look the same. <i>This is such an element. <i>But maybe I want to give it a pink background to stand out.</i></i> This can be achieved with style queries.</blockquote>

<p>Regardless of the type of element (<code>span</code>, <code>i</code>, <code>p</code>, etc.), style queries let you look at the specific style of any parent element to make styling decisions. This enables <i>"chained styles"</i>. If style X, then apply style Y. The code might look like:</p>

<pre><code>@container (font-style: italic) {
  span,
  i,
  .etc {
    background: lightpink;
  }
}
</pre></code>
  
<hr/>

<p><span>Demo #2:</span> This example shows color selection based on a parent's styles (including non-inherited styles). <code>border-color</code> is an example of a property that doesn't inherit. With style queries, we can query a parent's non-inheritable styles to apply to its children. For example, we can query <code>border-color</code> (or even <code>background</code> — see <a href="https://github.com/w3c/csswg-drafts/issues/5292">currentBackgroundColor proposal</a>) to apply styles to the button:</p>

<div class="demo-3">
  <figure>FPO</figure>
  <p>This is some text within the card.</p>
  <button>I am a button</button>
</div>

<pre><code>@container (border-color: lightblue) {
  button {
    border-color: lightblue;
  }
}
</pre></code>

<p>Taking that a step further, we can abstract these values to <a href="https://github.com/w3c/csswg-drafts/issues/5624">higher-level variables</a> like <code>--theme: light</code> or <code>--theme: dark</code>, and apply the stles throughout the card:</p>

<div class="demo-3" style="--theme: dark" data-theme="dark">
  <figure>FPO</figure>
  <p>This card has <code>--theme: dark</code> applied to the parent. We can now style its children </p>
  <button>I am a button</button>
</div>

<pre><code>@container (--theme: dark) {
  .card {
    background: royalblue;
    border-color: navy;
    color: white;
  }

  .card button {
    border-color: navy;
    background-color: dodgerblue;
    color: white;
  }
}
</pre></code>

<p>You could take this further to apply states that might have to do with card interactions or types such as <code>--sold: true</code> for a card that contains a product which is sold out, or <code>--type: post</code> if you have content cards and want to style blog posts differently from videos or other types of content.</p>

<p>These are just some ideas on how to use style queries in ways that enable a better developer experience and more flexible component-owned styles. They really shine when integrated within a larger system where these components are reused in multiple places.</p>
              
            
!

CSS

              
                // Base styles

body {
  font-size: 1.8rem;
  max-width: 50rem;
  margin: 0 auto;
}

pre {
  border: 1px solid gray;
  background: aliceblue;
  padding: 2rem;
}

code {
  font-family: 'Roboto Mono', monospace;
  font-size: 80%;
}

// Demo 1

blockquote {
  font-style: italic;
}

span {
  background: yellow;
  font-family: serif;
}

i > i {
  background: lightpink;
}

// Demo 2

.demo-2 {
  font-family: fantasy;
  letter-spacing: 0.2px;
}

// Demo 3

.demo-3 {
  font-size: 80%;
  background: aliceblue;
  border: 1px solid lightblue;
  max-width: 400px;
  margin: 0 auto;
  padding: 1rem;
}

.demo-3 figure {
  aspect-ratio: 16 / 9;
  margin: 0;
  display: grid;
  place-items: center;
  background-color: lightpink;
}

.demo-3 button {
  background: white;
  border: 1px solid lightblue;
  font-size: 1.5rem;
  padding: 1rem;
  display: block;
  margin: 0 auto;
}

.demo-3[data-theme="dark"] {
  background: royalblue;
  border-color: navy;
  color: white;
}

.demo-3[data-theme="dark"] button {
  border-color: navy;
  background-color: dodgerblue;
  color: white;
}

              
            
!

JS

              
                // Demo #1 via Miriam Suzanne's discussion of immediate parent style queries at the CSS working group

              
            
!
999px

Console