浮动标签在响应式输入表单上的位置不正确

huangapple go评论143阅读模式
英文:

Incorrect floating label positioning on responsive input form

问题

I've created a form using Django, Crispy Forms, and Bootstrap.
At screen widths above 575px the form and labels look as expected with fields of the correct width and floating labels in the correct position, however at screen widths below 575px the floating labels appear in the wrong position. I'd appreciate any guidance as to how I can make the floating labels remain in the correct position relative to the input field regardless of screen width.

Expected: >575px
浮动标签在响应式输入表单上的位置不正确
Unexpected: <575px

浮动标签在响应式输入表单上的位置不正确

I believe this happens because, in order to force the responsive resizing of the input fields, I manually amended the CSS to reduce the field width to 85% at screen widths lower than 575px by using a media query as follows:

CSS

.form-floating {
  position: relative;
}
.form-floating > .form-control,
.form-floating > .form-select {
  height: calc(3.5rem + 2px);
  line-height: 1.25;
}
.form-floating > label {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  padding: 1rem 0.75rem;
  pointer-events: none;
  border: 1px solid transparent;
  transform-origin: 0 0;
  transition: opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
}

@media (max-width: 575px) {
  .form-floating > .form-control,
  .form-floating > .form-select {
    display: block;
    width: 85%;
    padding: 0.375rem 0.75rem;
    margin: 0 auto;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
  }
  .form-floating > label {
    position: absolute;
    top: 0.5rem;
    left: 0.75rem;
    transition: opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
  }
}

HTML

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">

<div class="container">
  <div class="row justify-content-center" style="margin-top: 50px;">

     
     <div class="col-md-4 mb-4 mb-md-0">
     <div class="border rounded bg-light h-100">
        <div class="row justify-content-center">
             <h5 class="text-center"> Login Please</h5>
        </div>
        <br>
       <div class="col-sm-10  mx-auto">
<div class="form-floating 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">
  <input type="password" class="form-control" id="floatingPassword" placeholder="Password">
  <label for="floatingPassword">Password</label>
</div>
       </div>
       </div>
       </div>
    </div>
  </div>

CODEPEN LINK
https://codepen.io/lowdowner/pen/LYJyaGP

英文:

I've created a form using Django, Crispy Forms and Bootstrap.
At screen widths above 575px the form and labels look as expected with fields of the correct width and floating labels in the correct position, however at screen widths below 575px the floating labels appear in the wrong position. I'd appreciate any guidance as to how I can make the floating labels remain in the correct position relative to the input field regardless of screen width.

Expected: >575px
浮动标签在响应式输入表单上的位置不正确
Unexpected: <575px

浮动标签在响应式输入表单上的位置不正确

I believe this happens because, in order to force the responsive resizing of the input fields, I manually amended the CSS to reduce the the field width to 85% at screen widths lower than 575px by using a media query as follows:

CSS

.form-floating {
  position: relative;
}
.form-floating &gt; .form-control,
.form-floating &gt; .form-select {
  height: calc(3.5rem + 2px);
  line-height: 1.25;
}
.form-floating &gt; label {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  padding: 1rem 0.75rem;
  pointer-events: none;
  border: 1px solid transparent;
  transform-origin: 0 0;
  transition: opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
  
}

@media (max-width: 575px) {
  .form-floating &gt; .form-control,
  .form-floating &gt; .form-select {
    display: block;
    width: 85%;
    padding: 0.375rem 0.75rem;
    margin: 0 auto;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
  }
  .form-floating &gt; label {
    position: absolute;
    top: 0.5rem;
    left: 0.75rem;
    transition: opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
  }
}

HTML

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;row justify-content-center&quot; style=&quot;margin-top: 50px;&quot;&gt;

     
     &lt;div class=&quot;col-md-4 mb-4 mb-md-0&quot;&gt;
     &lt;div class=&quot;border rounded bg-light h-100&quot;&gt;
        &lt;div class=&quot;row justify-content-center&quot;&gt;
             &lt;h5 class=&quot;text-center&quot;&gt; Login Please&lt;/h5&gt;
        &lt;/div&gt;
        &lt;br&gt;
       &lt;div class=&quot;col-sm-10  mx-auto&quot;&gt;
&lt;div class=&quot;form-floating mb-3&quot;&gt;
  &lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;floatingInput&quot; placeholder=&quot;name@example.com&quot;&gt;
  &lt;label for=&quot;floatingInput&quot;&gt;Email address&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-floating&quot;&gt;
  &lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;floatingPassword&quot; placeholder=&quot;Password&quot;&gt;
  &lt;label for=&quot;floatingPassword&quot;&gt;Password&lt;/label&gt;
&lt;/div&gt;
       &lt;/div&gt;
       &lt;/div&gt;
       &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;

CODEPEN LINK
https://codepen.io/lowdowner/pen/LYJyaGP

答案1

得分: 1

问题出在表单标签的填充上。

请使用以下代码更新您的CSS媒体查询:

@media (max-width: 575px) {
  .form-floating > .form-control,
  .form-floating > .form-select {
    display: block;
    width: 85%;
    padding: 0.375rem 0.75rem;
    margin: 0 auto;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
  }
  .form-floating > label {
    position: absolute;
    top: 0.5rem;
    left: 0.75rem;
    padding: 1rem 3.2rem!important;
    transition: opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
  }
}

请注意,我刚刚在.form-floating > label的CSS属性中添加了padding: 1rem 3.2rem!important;,因为在较小的屏幕上,输入标签没有填充。另外,我在末尾使用了!important以覆盖与此类相关的全局填充设置,用于较大的屏幕。您可以在此处了解更多信息:!important CSS关键字

更新后的代码pen链接在这里:https://codepen.io/gd17/pen/RwYVOdq

英文:

The problem is with the padding of form label.

Update your CSS media query code with this below:

@media (max-width: 575px) {
      .form-floating &gt; .form-control,
      .form-floating &gt; .form-select {
        display: block;
        width: 85%;
        padding: 0.375rem 0.75rem;
        margin: 0 auto;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
      }
      .form-floating &gt; label {
        position: absolute;
        top: 0.5rem;
        left: 0.75rem;
        padding: 1rem 3.2rem!important;
        transition: opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
      }
    }

Please note I have just added padding: 1rem 3.2rem!important; to .form-floating > label CSS attribute

Here is the updated code pen link: https://codepen.io/gd17/pen/RwYVOdq

On the class .form-floating > label I added padding with 1rem 3.2rem!important; as the label of input doesn't have any padding for smaller screen. Also, the reason I used !important in the end to override the global padding associated with this class for larger screens. You can check about this more here: !important CSS keyword

huangapple
  • 本文由 发表于 2023年3月4日 05:46:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定