How can I stop labels from breaking onto separate lines in Bootstrap?

After developing a form using Bootstrap, I encountered an issue with the responsiveness on different devices. Specifically, when viewing the form on narrow screens, the labels for the inputs break into two lines due to lack of space. While this is expected behavior, I need a solution to prevent it from happening. It would be acceptable if the label/icon gets cropped, but splitting over multiple lines is not desired in any case. The form itself is dynamically generated via PHP, so the length of the labels can vary.

The desired outcome should resemble the image below (with icons next to the labels):

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

However, when the screen width is too narrow, it currently looks like this:

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

To address this issue, I would prefer the icon to be hidden rather than wrapped onto a new line if there isn't enough space for it.

In the provided sample code snippet, the issue becomes evident when setting the width to 350px. Increasing it to 400px or more resolves the problem:

<html>

<head>
  <title>Example</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src='https://kit.fontawesome.com/a076d05399.js'></script>
</head>

<body>
  <div class="container" style="max-width:350px">
    <form>
      <div class="form-row">
        <div class="col-4">
          <div class="form-group">
            <label for="input-1">First label <span class='fas fa-info-circle'></span>
                </label>
            <input id="input-1" class="form-control" name="input1" type="text">
          </div>
        </div>
        <div class="col-4">
          <div class="form-group">
            <label for="input-1">Second label <span class='fas fa-info-circle'></span></label>
            <input id="input-1" class="form-control" name="input2" type="text">
          </div>
        </div>
        <div class="col-4">
          <div class="form-group">
            <label for="input-1">Third label  <span class='fas fa-info-circle'></span></label>
            <input id="input-1" class="form-control" name="input3" type="text">
          </div>
        </div>
      </div>
    </form>
  </div>
</body>

</html>

Answer №1

To address the issue of an icon breaking at a certain device width, you first need to identify at what width this occurs. Once identified, you can implement a media query in your CSS to hide the icon accordingly.

For example:

@media screen and (max-width: 350px) {
  .fa-info-circle {
    display: none;
  }
}

This code snippet will ensure that the icon is hidden for screens with a width of 350px or less.

Answer №2

To incorporate an ellipsis in the label tag, you can utilize the code snippet provided below. This will ensure that when the text within the label surpasses the designated width, it will be replaced by an ellipsis symbol (...) while maintaining the icon.

label {
    white-space: nowrap; 
    overflow: hidden;
    text-overflow: ellipsis; 
}

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

What measures can be taken to ensure that scrollIntoView does not interrupt when the user begins scrolling on their own?

I have a navigation bar where I am using the scrollIntoView method to ensure that the active element is always centered in the list. However, I encountered an issue wherein scrolling the main content interrupts or stops the scroll action triggered by scrol ...

Transform ajax functionality into jquery

I need help converting an AJAX function to a jQuery function, but I'm not sure how to do it. function convertToJquery() { // Create our XMLHttpRequest object var hr = new XMLHttpRequest(); // Set up variables needed to send to PHP file var ur ...

I'm having trouble with my carousel. I suspect it's the "link-rel" in the head tag that's causing the issue

Having trouble with my carousel - it's not navigating properly. When I check the console, it shows: Failed to find a valid digest in the 'integrity' attribute for resource '' with computed SHA-256 integrity 'YLGeXaa ...

Is there a way to ensure that the div is displayed on the same line?

Check out the jsfiddle link provided: http://jsfiddle.net/HE7yT/ I'm trying to align the Image, content, and Amount in the same line. How can I achieve this? The goal is to have "150" displayed on the same line as the picture. var HTML = $(' ...

At a specific media query breakpoint, links are disabled while the rest continue to function without any

Today I encountered a puzzling issue on my website with a responsive layout that utilizes @media queries and breakpoints. At one specific breakpoint (or resolution, as I am still learning the terminology), the links in the navigation bar are inexplicably d ...

A linear gradient effect originating from the center/middle of the background

Is it possible to create a linear-gradient background that starts from the center or middle of the screen? This is the CSS I am currently using: body { display: table; width: 100%; height: 100%; margin: 0 auto; background-repeat: no-r ...

The CSS Bootstrap 4 class 'input-group-append' is not functioning properly. Just recently, the styles were displaying correctly

My web application using AngularJS 1.7.8, jQuery 3.3.1, Bootstrap 4.3.1, and various other CSS and JS libraries worked seamlessly last week. However, today I noticed an issue with the button visualization. To Replicate: Visit openskymap.org to see my d ...

Bootstrap 4 grid system: mixed column widths with fixed and variable, may experience overlapping in IE11"

Dealing with Bootstrap 4 grid challenge in IE11 when using a fixed width column I am working on a three-column grid where the middle column needs to have a fixed width, while the left and right columns are flexible/wrappable. Here is the code snippet I h ...

Encountering a problem with scrolling the modal to the top

I am currently working on a code snippet that involves scrolling the modal to the top position. The purpose of this is to display form validation messages within the modal popup. Below are the jQuery code snippets that I have attempted: //Click event t ...

What are the steps to overlay a button onto an image with CSS?

As a CSS newbie, I have a question that may seem silly or straightforward to some. I am struggling with placing a button over an image and need guidance on how to achieve this effect. Specifically, I want the button to appear like this blue "Kopit" button ...

I'm experimenting with crafting a new color scheme using MUI, which will dynamically alter the background color of my card based on the API

I am attempting to create a function that will change the colors based on the type of Pokemon. However, I'm not sure how to go about it. Any suggestions or ideas!? Check out where I'm brainstorming this logic [This is where the color palette sh ...

Adjusting the line-height and baseline to accommodate various screen sizes on both mobile and desktop

This problem seems to keep coming back for me. I can't figure out what's causing it. Here are two images showing the issue: On desktops: https://i.sstatic.net/AAGbb.png On mobile devices: https://i.sstatic.net/Zk4pc.jpg As you can see, the ...

Guide to creating the onclick feature for a dynamic button in Meteor

<template name="actionTemplate"> {{#each action}} <button class="myButton" id={{_id}}>btn</button> {{> action}} {{/each}} </template> <template name="action"> <div class="sct" id={{_id}}> ...

Adjusting Font Size in Angular Material Design

Recently, I incorporated https://material.angularjs.org/latest/ to optimize the responsive design of my website. One key feature I am focusing on is adjusting the font size based on different screen sizes. For smaller screens, I intend to set the font siz ...

Modifying the default actions of Material UI components: A step-by-step guide

In my project, I am using materialUI to showcase an Expansion Panel. Here is the code snippet: import React from 'react' import ExpansionPanel from '@material-ui/core/ExpansionPanel'; import ExpansionPanelSummary from '@material-u ...

Introducing a novel class designed to leverage the attributes of another class

Is there a way to use the @extend feature in CSS to inherit properties from one class to another? Imagine having two CSS files loading on a page in this order: <link rel="stylesheet" href="/css/one.css" media="screen"> <link rel="stylesheet" href ...

Problem with video tag getting stuck

Is there a method to have the video tag remain fixed to the top (or bottom from the element above the video tag)? Currently, when the window is resized, the video element moves away from the top (or bottom). Could this be due to the video tag attempting to ...

Adjusting page layout following window dimension changes

Every time I work on developing HTML pages, I encounter issues with window resizing. The alignment of the page gets disrupted and elements end up overlapping each other. I want my page to maintain its layout when resized, with scrollbars appearing instea ...

How can I ensure the height of the Bootstrap vertical navigation menu appears consistent on every page

I'm struggling to get the vertical navigation menu to reach all the way down to the footer of the page. I've tried adjusting it, but the closest I came was setting a specific height for the .navbar, which isn't ideal because I want it to be ...

Using Thymeleaf in Spring MVC to link a .css file to your webpage

I am currently working on a project using spring MVC and Thymeleaf. I have encountered an issue regarding how to reference my CSS files within my folder structure: src main webapp resources myCssFolder myCssFile.css web-inf ...