twbs / twbs/bootstrap

Floating labels incorrectly positioned for inputs with autofill, but no value

Open
#39,899 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

css v5
Dominant language
MDX
Stars
175k
Forks
78.6k
Avg merge
7h 19m
Merged PRs (30d)
35

Description

Prerequisites
Describe the issue

Floating labels get stuck in their transformed position for inputs that were initially auto-filled by the browser, but cleared programmatically using javascript.
ex.

$('input').val('')

example html:

<div class="form-floating string">
  <input class="form-control string" id="field" placeholder="placeholder" type="text">
  <label class="string" for="field"> Username </label>
</div>

Even though the input in its current state doesn't have any value attribute (the same applies if value=""),
it still holds its :autofill pseudo-class, therefore incorrect css rules are being applied to its label.

important note:

I observed this issue in the Firefox browser. This may be irrelevant for other browsers.

Reduced test cases

here is an example html body

<form id="login-form" action="/" accept-charset="UTF-8">
  <div class="form-floating mb-3 string required col-4">
    <input class="form-control string required" required="required" aria-required="true" placeholder="Username" type="text" id="username">
    <label class="string required custom-label" for="username"> Username </label>
  </div>

  <div class="form-floating mb-3 password required col-4">
    <input class="form-control password required" required="required" aria-required="true" placeholder="Password" type="password" id="password">
    <label class="password required custom-label" for="password"> Password </label>
  </div>

  <div class="actions mt-3">
    <input type="submit" value="Sign Up" class="btn btn-primary" data-disable-with="Sign Up">
  </div>
</form>
<br>
In order to reproduce the issue, fill up and submit the form. Then save the data in your browser and refresh the page.
<script>
  $( () => $('input').val('') )
</script>

Potential solution:
Exclude inputs with no value attribute or it being empty from the :autofill, :-webkit-autofill etc. selectors.

Edit: The solution might not work since apparently autofill in Firefox doesn't add nor update the value attribute 😮

What operating system(s) are you seeing the problem on?

Windows

What browser(s) are you seeing the problem on?

Firefox

What version of Bootstrap are you using?

v5.3.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reduced HTML test case and reproduce the behavior in Firefox after autofill, form submission, refresh, and the jQuery value clear. Inspect Bootstrap v5.3.3 floating-label autofill selectors and confirm that the label returns to its untransformed position when the input has no current value.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.