Skip to content

Commit

Permalink
Streamline and bulletproof some things
Browse files Browse the repository at this point in the history
- Apply some optimizations from code review
- Removed unecessary properties from the label
- Add some comments for what properties are required
- Move from fixed height for labels to height 100% so we can support textareas
- Improve docs a little bit, add ToC
  • Loading branch information
mdo committed Sep 17, 2020
1 parent 77a83f5 commit 50a6a16
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
12 changes: 5 additions & 7 deletions scss/forms/_floating-labels.scss
Expand Up @@ -3,7 +3,6 @@
.form-floating {
position: relative;

> label,
> .form-control,
> .form-select {
height: $form-floating-height;
Expand All @@ -14,13 +13,10 @@
position: absolute;
top: 0;
left: 0;
display: block;
width: 100%;
margin-bottom: 0; // Override default `<label>` margin
height: 100%; // allow textareas
padding: $form-floating-padding-y $form-floating-padding-x;
pointer-events: none;
cursor: text; // Match the input under the label
border: $input-border-width solid transparent;
@include border-radius(.25rem);
border: $input-border-width solid transparent; // Required for aligning label's text with the input as it affects inner box model
@include transition(all .1s ease-in-out);
}

Expand All @@ -29,6 +25,7 @@
color: transparent;
}

&:focus,
&:not(:placeholder-shown) {
padding-top: $form-floating-input-padding-t;
padding-bottom: $form-floating-input-padding-b;
Expand All @@ -40,6 +37,7 @@
padding-bottom: $form-floating-input-padding-b;
}

> .form-control:focus,
> .form-control:not(:placeholder-shown),
> .form-select {
~ label {
Expand Down
29 changes: 25 additions & 4 deletions site/content/docs/5.0/forms/floating-labels.md
Expand Up @@ -3,6 +3,7 @@ layout: docs
title: Floating labels
description: Create beautifully simple forms labels that float over your input fields.
group: forms
toc: true
---

## Example
Expand All @@ -16,7 +17,7 @@ This approach works in the new Microsoft Edge built on Chromium and gracefully d
<input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
<label for="floatingInput">Email address</label>
</div>
<div class="form-floating mb-3">
<div class="form-floating">
<input type="password" class="form-control" id="floatingPassword" placeholder="Password">
<label for="floatingPassword">Password</label>
</div>
Expand All @@ -25,7 +26,7 @@ This approach works in the new Microsoft Edge built on Chromium and gracefully d
When there's a `value` already defined, `<label>`s will automatically adjust to their floated position.

{{< example >}}
<form class="form-floating mb-3">
<form class="form-floating">
<input type="email" class="form-control" id="floatingInputValue" placeholder="name@example.com" value="test@example.com">
<label for="floatingInputValue">Input with value</label>
</form>
Expand All @@ -34,18 +35,38 @@ When there's a `value` already defined, `<label>`s will automatically adjust to
Form validation styles also work as expected.

{{< example >}}
<form class="form-floating mb-3">
<form class="form-floating">
<input type="email" class="form-control is-invalid" id="floatingInputInvalid" placeholder="name@example.com" value="test@example.com">
<label for="floatingInputInvalid">Invalid input</label>
</form>
{{< /example >}}

## Textareas

By default, `<textarea>`s with `.form-control` will be the same height as `<input>`s.

{{< example >}}
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea"></textarea>
<label for="floatingTextarea">Comments</label>
</div>
{{< /example >}}

To set a custom height on your `<textarea>`, do not use the `rows` attribute. Instead, set an explicit `height` (either inline or via custom CSS).

{{< example >}}
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea2" style="height: 100px"></textarea>
<label for="floatingTextarea2">Comments</label>
</div>
{{< /example >}}

## Selects

Other than `.form-control`, floating labels only available on `.form-select`s. They work in the same way, but unlike `<input>`s, they'll always show the `<label>` in its floated state.

{{< example >}}
<div class="form-floating mb-3">
<div class="form-floating">
<select class="form-select" id="floatingSelect" aria-label="Floating label select example">
<option selected>Open this select menu</option>
<option value="1">One</option>
Expand Down

0 comments on commit 50a6a16

Please sign in to comment.