Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for always visible floating labels #36155

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add option for always visible floating labels
  • Loading branch information
mdo committed Apr 12, 2022
commit 36ee50fa845c78d9147f25615a1c1d20c18f976f
47 changes: 37 additions & 10 deletions scss/forms/_floating-labels.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// We use mixins instead of placeholders as placeholders would combine the selectors when @extend-ed

@mixin form-floating-active-input-styles() {
padding-top: $form-floating-input-padding-t;
padding-bottom: $form-floating-input-padding-b;
}

@mixin form-floating-active-label-styles() {
opacity: $form-floating-label-opacity;
transform: $form-floating-label-transform;
}

.form-floating {
position: relative;

Expand Down Expand Up @@ -30,35 +42,32 @@

&:focus,
&:not(:placeholder-shown) {
padding-top: $form-floating-input-padding-t;
padding-bottom: $form-floating-input-padding-b;
@include form-floating-active-input-styles();
}

// Duplicated because `:-webkit-autofill` invalidates other selectors when grouped
&:-webkit-autofill {
padding-top: $form-floating-input-padding-t;
padding-bottom: $form-floating-input-padding-b;
@include form-floating-active-input-styles();
}
}

> .form-select {
padding-top: $form-floating-input-padding-t;
padding-bottom: $form-floating-input-padding-b;
@include form-floating-active-input-styles();
}

> .form-control:focus,
> .form-control:not(:placeholder-shown),
> .form-control-plaintext,
> .form-select {
~ label {
opacity: $form-floating-label-opacity;
transform: $form-floating-label-transform;
@include form-floating-active-label-styles();
}
}

// Duplicated because `:-webkit-autofill` invalidates other selectors when grouped
> .form-control:-webkit-autofill {
~ label {
opacity: $form-floating-label-opacity;
transform: $form-floating-label-transform;
@include form-floating-active-label-styles();
}
}

Expand All @@ -68,3 +77,21 @@
}
}
}

// Todo in v6: Consider combining this with the .form-control-plaintext rules to reduce some CSS?
.form-floating-always {
> .form-control {
@include form-floating-active-input-styles();

&::placeholder {
color: $input-color;
}
&:focus::placeholder {
color: $input-placeholder-color;
}
}

> label {
@include form-floating-active-label-styles();
}
}
15 changes: 15 additions & 0 deletions site/content/docs/5.1/forms/floating-labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ Floating labels also support `.form-control-plaintext`, which can be helpful for
</div>
{{< /example >}}

## Always floating

Make any `.form-control` always use a floating label with visible placeholder with the `.form-floating-always` modifier class. Visible placeholders use the default input `color` and lighten to the placeholder color on focus. This matches them with other floating labels built with plaintext inputs and selects.

{{< example >}}
<div class="form-floating form-floating-always mb-3">
<input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
<label for="floatingInput">Email address</label>
</div>
<div class="form-floating form-floating-always">
<input type="password" class="form-control" id="floatingPassword" placeholder="••••••••">
<label for="floatingPassword">Password</label>
</div>
{{< /example >}}

## Layout

When working with the Bootstrap grid system, be sure to place form elements within column classes.
Expand Down