Overflow issue with floating labels in Bootstrap 5 within a grid container

Incorporated within this code snippet are floating selects placed inside a bootstrap grid:

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4b6bbbba0a7a0a6b5a494e1fae7fae7">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="11737e7e65626563706151243f223f22">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<div class="container w-50 mt-4">
  <div class="row">

    <div class="col">
      <div class="form-floating">
        <select class="form-select" id="a">
          <option value="" selected=""></option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
        <label for="a">Foo Bar Baz</label>
      </div>
    </div>

    <div class="col-auto">
       <div class="form-floating">
         <select class="form-select" id="b">
           <option value="" selected=""></option>
           <option value="1">1</option>
           <option value="2">2</option>
           <option value="3">3</option>
         </select>
         <label for="b">Spam Ham Eggs</label>      <!-- this overflows -->
       </div>
    </div>
    
  </div>
</div>

The issue arises in the second column, with the usage of col-auto to reduce its width causing the label to overflow from the column. I attempted using overflow-visible, but as the label is absolutely positioned, it didn't resolve the problem.

Any suggestions on resolving this matter without setting a fixed width?

Answer №1

The issue lies in the fact that the label has been styled with position: absolute, causing the width of the selection container to have no correlation with the width of the label. Even if both containers were set to use col, the label would still overflow when the viewport is narrow.

To tackle this, a workaround could involve duplicating the label inside a hidden span element so that the width of the selection box remains equal to or greater than the width of the label text.

<span class="m-1 invisible">Spam Ham Eggs</span>

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a4a9a9b2b5b2b4a7b686f3e8f5e8f5">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="21434e4e55525553405161140f120f12">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<div class="container w-50 mt-4">
  <div class="row">

    <div class="col">
      <div class="form-floating">
        <select class="form-select" id="a">
          <option value="" selected=""></option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
        <label for="a">Foo Bar Baz</label>
      </div>
    </div>

    <div class="col-auto">
       <div class="form-floating">
         <select class="form-select" id="b">
           <option value="" selected=""></option>
           <option value="1">1</option>
           <option value="2">2</option>
           <option value="3">3</option>
         </select>
         <label for="b">Spam Ham Eggs</label>      <!-- this overflows -->
         <span class="m-1 invisible">Spam Ham Eggs</span>
       </div>
    </div>
    
  </div>
</div>

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

During the process of converting documents to HTML, any graphics or shapes may not be accurately translated into HTML format

I am looking for a way to display a doc file in a dialog box on a browser. To achieve this, I converted the doc file into an html file successfully. However, I encountered an issue when the doc file contained graphics or shapes - these elements were not co ...

Discover the character "’" as well as other fascinating symbols

When transferring content into an HTML file from sources like Word documents or PDFs, I want to ensure that the special quotes and apostrophes are converted to their standard counterparts. In simpler terms, I desire to switch characters like ’ with &apo ...

Utilizing absolute positioning with a rotated div element

The situation I am facing involves a parent div and child div. The child div is positioned absolutely with respect to the parent div and has been rotated by an angle of 90 degrees. Due to the fact that the origin is located at the center of the child div ( ...

Sticky sidebar that adjusts to viewport size in a responsive layout

When designing a CSS fluid layout, most divs utilize percentages to adjust based on the viewport size. However, I have a specific scenario where I need my sidebar to maintain a fixed position. For instance: <aside style="width:25%; float:left; positio ...

What is the best way to maintain a div centered when the browser window shrinks smaller than the div's dimensions?

Is there a way to keep a div centered and create white space around it when the browser window is resized? I want this effect to happen whether the browser window gets larger or smaller. Currently, when the window size decreases, the left side of the div l ...

Ways to Design ASP.NET Buttons for Users with Disabilities

Recently, I attempted to style an asp button using CSS. The Button HTML: <asp:Button ID="btnClockin" runat="server" Text="Clock In" class="FullWidthButton" /> Here is the CSS code: .FullWidthButton {width:100%;} Everything was working fine until ...

Troubleshooting issues with applying styles in Vue framework when configured with webpack

I'm facing an issue with setting up the Vue framework using webpack. Specifically, I'm having trouble with styles not being applied when included in the <style> tag within single file components. Despite following several tutorials on this ...

Textarea generated on-the-fly without any assigned value

My goal is to enable users to edit the text within a paragraph on a website. I am attempting to replace the <p> tags with <textarea> tags using the .replaceWith() function. However, when I try to retrieve the value of the textarea, it comes bac ...

Disabling autocomplete in the DOCTYPE HTML tag in Visual Studio Code prevents it from appearing when

When I type !, it should show up like in the image. However, it's not working. Could it be because Visual Studio Code got an update? Or did I make a mistake on my end? I found a workaround by using html:5, but I'm still curious to know if this ...

This message is designed to validate the form as it is dynamic and

Is there a way to dynamically determine which .input fields have not been entered yet? In the following code snippet, you can observe that if I input data out of sequence, the #message displays how many inputs have been filled and shows the message in sequ ...

Introducing the new default in Google Chrome: Now accessing window.opener for target="_blank" links after rel="noopener" is the standard feature

Exploring a new attribute called rel="noopener", I discovered that it keeps opened windows unaware of their opener. After some experimentation, I consistently found window.opener === null, only to realize that this is now the default behavior in ...

Instructions on making a Popup appear after a specified duration of mouse inactivity on a webpage

Hello, I am new to web development and could use some guidance on creating a popup window for users to enter their username and contact number. It should be able to store this information in a database and activate after the user has been idle for about 10 ...

Adding a div element to the canvas and generating a DataURL is currently experiencing technical difficulties

My task involves displaying an image with two div elements placed above it - one containing an image and the other containing text. I need to combine these three elements to create a final image and store it in a database/server. To achieve this, I am usin ...

The droplist functionality in jQuery does not seem to be functioning properly within an ASP.NET MVC 5

After adding an external theme to my project and writing a script for listing data in a dropdown list on a separate page, I encountered an issue where the script works on other pages but not on the Layout theme page. HomeController Note: The partial view ...

`CSS Animation fails to run in Internet Explorer and Microsoft Edge browsers`

My issue with the CSS animation is that while the opacity animates properly, the translate effect does not seem to work as expected. You can see the problem here: https://jsfiddle.net/bLxb8k3s/ It appears that the reason for this issue is because IE and ...

finding the source file or code where a particular html element is being generated

In the HTML file I have, there are multiple JavaScript files added. Whenever I run the HTML file, a title tag is automatically created in my HTML file. I do not want this title tag to appear in my HTML file. How can I prevent this from happening? Or how c ...

Is there a way to grab code from a different HTML page using JavaScript and jQuery when the user clicks on something?

After testing various codes for the intended purpose mentioned in the title, I have observed a peculiar behavior. The code from settings.html appears briefly and then disappears rapidly. Javascript: function load() { $("#paste").load("settings.html") ...

Is it possible to create a CSS checkbox without using an ID or "for" attribute

Is it possible to create a CSS checkbox with label without using id and for attributes? I have tried the following method, but it doesn't seem to work. I know that I can create a CSS checkbox with id and for attributes like this: <div class="chec ...

The button contains a clickable link inside it, however the button itself is not clickable

I am encountering an issue with a button that contains a link. Whenever I hover over the button, the cursor changes as expected, but clicking on the button itself does not trigger any action. Instead, I have to click on the link inside the button to redi ...

Guide on setting the value of a select element in Vue Core UI

Apologies for the newbie question, as I am just starting out with Vue.js. I have recently started using CoreUI, but I am having trouble finding detailed documentation or tutorials on CoreUI+Vue integration. Specifically, I am working with the <CForm> ...