Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <h1>Floating <code>&lt;label&gt;</code> using <code>:has(…)</code></h1>
  
<p>Traditional floating label implementations, like in <a href="https://getbootstrap.com/docs/5.3/forms/floating-labels/" target=_blank>Bootstrap</a>, rely on the CSS <code>+</code> sibling or <code>~</code> subsequent sibling selector, and therefore require the form control before the actual label in the markup. This is not ideal from a screen reader perspective: when in browse mode / virtual cursor, this means the form control is encountered first, and generally the control is not announced using its label this way.</p>

<div id="classic">
  <div class="form-floating">
    <input id="foo" class="form-control" type="text">
    <label for="foo">Username</label>
  </div>
</div>

<pre><code>&lt;input id="foo" type="text"&gt;
&lt;label for="foo"&gt;Username&lt;/label&gt;</code></pre>

<pre><code>/* simplified CSS */
label { … /* cover the input */ … }

input:focus ~ label,
input:placeholder-shown ~ label,
input:user-valid ~ label,
input:user-invalid ~ label { … /* float the label */ … }</code></pre>

<p>Note that the above approach still has a slight quirk that if a user enters something in the input, but then deletes/blanks it, <code>:user-valid</code> / <code>:user-invalid</code> will still evaluate to true and float the label.</p>

<hr>

<p>Using the <a href="https://drafts.csswg.org/selectors-4/#relational" target=_blank><code>:has(…)</code> relational pseudo-class</a> (and a wrapper element around the control and label, as is the case in Bootstrap already) we can finally use sensible source order while also doing the floating label dance.</p>

<div id="new">
  <div class="form-floating">
    <label for="bar">Username</label>
    <input id="bar" class="form-control" type="text">
  </div>
</div>

<pre><code>&lt;div class="wrapper"&gt;
  &lt;label for="bar"&gt;Username&lt;/label&gt;
  &lt;input id="bar" type="text"&gt;
&lt;/div&gt;</code></pre>

<pre><code>/* simplified CSS */
.wrapper > label { … /* cover the input */ … }

.wrapper:has(input:focus) > label { … }
.wrapper:has(input:focus) > label,
.wrapper:has(input:placeholder-shown) > label,
.wrapper:has(input:user-valid) > label,
.wrapper:has(input:user-invalid) > label { … /* float the label */ … }</code></pre>

<p>Note as always that you should check <a href="https://caniuse.com/css-has" target=_blank>support for the <code>:has()</code></a> before using this in production.</p>

<p>This experiment was prompted by <a href="https://github.com/orgs/twbs/discussions/39343" target=_blank>this question in the Bootstrap discussion board</a>.</p>
              
            
!

CSS

              
                pre { margin: 2em 0; }

/* floating label styles liberally borrowed/stolen from Bootstrap */

.form-floating {
  position: relative;
}

.form-floating>.form-control {
  padding: 1rem 0.75rem;
}

.form-floating>label {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  height: 100%;
  padding: 1rem 0.75rem;
  overflow: hidden;
  text-align: start;
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
  transform-origin: 0 0;
  transition: opacity .1s ease-in-out,transform .1s ease-in-out;
}

label {
  display: inline-block;
}

/* the classic approach using ~ and the weird source order */

#classic .form-floating>.form-control:focus~label,
#classic .form-floating>.form-control:placeholder-shown~label,
#classic .form-floating>.form-control:user-valid~label,
#classic .form-floating>.form-control:user-invalid~label {
  transform: scale(.85) translateY(-0.5rem) translateX(0.15rem);
}

/* the new funky way using :has() */

#new .form-floating:has(.form-control:focus)>label,
#new .form-floating:has(.form-control:placeholder-shown)>label,
#new .form-floating:has(.form-control:user-valid)>label,
#new .form-floating:has(.form-control:user-invalid)>label {
  transform: scale(.85) translateY(-0.5rem) translateX(0.15rem);
}
              
            
!

JS

              
                
              
            
!
999px

Console