Responsive input form causes floating label to be positioned incorrectly

I have crafted a form using Django, Crispy Forms, and Bootstrap. When the screen width is above 575px, everything looks great with fields of appropriate width and floating labels in their correct position. However, when the screen width drops below 575px, the floating labels seem to be off. I am seeking advice on how to keep the floating labels aligned correctly with the input field, regardless of the screen width.

Expected: >575px https://i.sstatic.net/V5Brq.png Unexpected: <575px

https://i.sstatic.net/xux1e.png

I suspect this issue arises because in order to ensure responsive resizing of the input fields, I manually adjusted the CSS to reduce the field width to 85% when the screen width is under 575px using a media query as shown below:

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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0805051e191e180b1a2a5f4459445a470b061a020b5b">[email protected]</a>/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="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="690708040c290c11080419050c470a0604">[email protected]</a>">
  <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

Answer №1

There is an issue with the padding of the form label.

To fix this, update your CSS media query code like so:

@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;
      }
    }

I included padding: 1rem 3.2rem!important; in the .form-floating > label CSS attribute to address the lack of padding for smaller screens. The use of !important at the end overrides the global padding set for larger screens. Learn more about this here: !important CSS keyword

You can view the updated code pen link here: https://codepen.io/gd17/pen/RwYVOdq

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to dynamically update the content of <li> tag with values from an array every x seconds

I am trying to create a li list using jQuery that will update every 2 seconds with new content, similar to game news updates. However, I'm encountering an issue during the first cycle where it skips the second item in the array. The subsequent cycles ...

Nginx fails to load CSS in Asp.net Core on Raspberry Pi

First and foremost, thank you for taking the time to read my post. Secondly, I apologize for any language errors in advance. I am currently attempting to run an Asp.net core application on a Raspberry Pi, but I am encountering difficulties with nginx prox ...

Creating custom styles using Material-UI's makeStyles function with both before and after

Currently, I'm tasked with a project that involves utilizing the CSS code below. .hexagon, .hexagon::before, .hexagon::after { width: 67px; height: 116px; border-radius: 18%/5%; } I am wondering if there is a method to apply the specified style ...

Storing form data in temporary local storage using a JSON object array within a PhoneGap project

I am currently developing a data acquisition system using PhoneGap and facing an issue with storing form data temporarily on local storage using JSON. The data should remain visible even after closing and reopening the application by pressing the Get Data ...

Flexible DIVs with a maximum width of 50% that adjust to different

I'm having trouble getting these two DIVs to display properly within a parent DIV while maintaining responsiveness. I want them to each take up 50% of the screen with a 10px margin between them. Does my current code approach seem correct? .box-cont ...

There seems to be a compatibility issue between Angular 16 and Bootstrap 5 styling

In my angular.json, I have defined my styles in the following way: "styles": [ { "input": "node_modules/bootstrap/dist/css/bootstrap.min.css", "bundleName": "ltrCSS" }, { "input": "node_mod ...

The z-index property does not seem to be affecting the positioning of images and buttons

I'm working on a school project creating an educational website about pumas. I want to have a set of buttons that are hidden behind an image, and when the user clicks on the image, the buttons should be revealed. However, I am facing an issue with the ...

Tips for positioning two fields side by side on a webpage with CSS

I currently have two datepickers aligned vertically and I'm looking to display them in a horizontal layout with some spacing between them. What changes do I need to make in order to present these two calendar pickers side by side on the same row? Cou ...

What is the best way to apply CSS modifications to sibling elements that are related?

I have several parent div elements with pairs of button and div child elements. My goal is to apply changes to the corresponding div when a button is clicked. For example, clicking Button 2 should only affect toast 2. However, I am facing an issue where o ...

When the browser tab icon does not appear, it is likely due to not responding to the GET /favicon.ico

I am running a web server for my basic HTML page that does not have a web icon included in the template. "HTTP/1.1 200 OK" Content-Type: text/html Connection: close <!DOCTYPE html> <html> <head> <met ...

Safari does not consistently display uniform button height with the use of flex alignment

Within the div I have a button enclosed in the following structure: HTML: <div class="parent-container"> <div class="child child-1 col-xs-6"> <button class="btn btn-primary">Customize Feed</button> </div> ...

Issue with Bootstrap 4.2 modal: Unable to edit input fields, button cannot be clicked, modal does not close

https://i.sstatic.net/vHnVG.png https://i.sstatic.net/MhU3f.png Utilizing Bootstrap 4.2.1, I have a modal that opens via a specific link: <a href="#" data-toggle="modal" data-target="#inviteByEmailPopup" data-backdrop="false" data-keyboard="false"> ...

Experiencing issues with Bootstrap's width overflow while writing lengthy articles with numerous paragraphs

INTRODUCTION I am in the process of developing a website to exhibit my past work in blockchain and cybersecurity projects. Using bootstrap v5.1.3, I have successfully created two pages on my website so far, despite not being a seasoned front-end developer ...

Different methods for modifying the height of an Angular datatable in response to the data provided

Encountering an issue with adjusting the height of a datatable when calling a webservice to populate it. A webpage features a collapsible div element alongside a datatable. Upon collapsing the div, the table's height is calculated and resized using i ...

Using media queries, one can adjust the positioning of elements in CSS

Can someone please help me? I'm struggling to change the position of a div from absolute to static when the page width reduces to 800px. The challenge is that I can't modify certain code because I am displaying a slideshow that maintains an aspec ...

Are iFrames being utilized for third-party applications?

I am looking to create a web application that can be extended by other developers with their own applications. Would using iFrames, similar to how Facebook does it, be the best approach? Is this considered a good practice in web development? Are there a ...

Discovering Unnecessary CSS & JS Code

Currently, I am tackling a freelance project focused on upgrading a website. The client is requesting specific features from their old site be replicated on the new one. However, the challenge lies in the fact that the old site's CSS and JS are consol ...

Attempting to showcase extensive content based on the selection made in a drop-down menu

As a newcomer to anything beyond basic HTML, I am seeking guidance and assistance (preferably explained at a beginner level) for the following issue. If I have overlooked any crucial rules or concepts in my query, I apologize. I aim to have each selection ...

Guide to positioning the layout inflater on the right side

I am facing an issue with the layout inflater where the inflated layout appears in the center of the page instead of the intended right corner placement. I have attempted to modify the gravity to right, but it did not resolve the problem. Below is a snipp ...

establishing the position of the mouse cursor within a scaled div container

Issue at Hand: I'm encountering an obstacle with a transformed div set up as follows: $('#container').css('-moz-transform-origin', '0 0'); $('#container').css('-webkit-transform-origin', '0 0&ap ...