Learn how to implement Bootstrap 4 in a way that allows the help text to span 100% width, while ensuring that the input

Is there a way to prevent the select field width from being 100% while keeping the help text at 100% width when using Bootstrap form-groups with label, input field, and help text stacked vertically?

You can view a setup example in this JSFiddle.

I attempted to set the select field width to only 30ch by adding a style to the outer div, but it applied the style to the help text as well. Trying to add the style directly to the select element resulted in formatting issues where the label and input were on the same row instead of separate rows.

<div>
<div class="form-group">
  <div>
      <label for="debugLevel" id="debugLevellabel">
          Debug Log Level
      </label>
      <select class="custom-select" style="width:30ch" name="debugLevel" id="debugLevel" aria-describedby="debugLevelhelp">

              <option value="0">
                  Severe
              </option>
              <option selected="selected" value="1">
                  Warning
              </option>
              <option value="2">
                  Info
              </option>
              <option value="3">
                  Config
              </option>
              <option value="4">
                  Fine
              </option>
              <option value="5">
                  Finer
              </option>
              <option value="6">
                  Finest
              </option>

      </select>
  </div>
  <p id="debugLevelhelp" class="form-text text-muted">
      Sets how much logging is written to the debug log, increasing the log level can help Jthink identify the cause of any problems encountered in SongKong
  </p>
</div>
<div class="form-group">
  <div>
      <label for="ioDebugLevel" id="ioDebugLevellabel">
          Debug IO Log Level
      </label>
      <select class="custom-select" style="width:30ch" name="ioDebugLevel" id="ioDebugLevel" aria-describedby="ioDebugLevelhelp">

              <option value="0">
                  Severe
              </option>
              <option selected="selected" value="1">
                  Warning
              </option>
              <option value="2">
                  Info
              </option>
              <option value="3">
                  Config
              </option>
              <option value="4">
                  Fine
              </option>
              <option value="5">
                  Finer
              </option>
              <option value="6">
                  Finest
              </option>

      </select>
  </div>
  <p id="ioDebugLevelhelp" class="form-text text-muted">
      Sets how much logging is written to the debug log when SongKong reads files or saves changes to files, increasing the log level can help Jthink identify the cause of any problems encountered in SongKong
  </p>
</div>
</div>

Answer №1

Explore a different approach using the bootstrap structure code along with classes like d-block and d-inline-block, eliminating the need for inline css.

<div class="form-group">
  <label class="d-block" for="debugLevel" id="debugLevellabel"> Debug Log Level </label>
  <div class="d-inline-block">
    <select class="custom-select" name="debugLevel" id="debugLevel" aria-describedby="debugLevelhelp">
      <option value="0"> Severe </option>
      <option selected="selected" value="1"> Warning </option>
      <option value="2"> Info </option>
      <option value="3"> Config </option>
      <option value="4"> Fine </option>
      <option value="5"> Finer </option>
      <option value="6"> Finest </option>
    </select>
  </div>
  <p id="debugLevelhelp" class="form-text text-muted"> Sets how much logging is written to the debug log, increasing the log level can help Jthink identify the cause of any problems encountered in SongKong </p>
</div>
<div class="form-group">
  <label class="d-block" for="ioDebugLevel" id="ioDebugLevellabel"> Debug IO Log Level </label>
  <div class="d-inline-block">
    <select class="custom-select" name="debugLevel" id="debugLevel" aria-describedby="debugLevelhelp">
      <option value="0"> Severe </option>
      <option selected="selected" value="1"> Warning </option>
      <option value="2"> Info </option>
      <option value="3"> Config </option>
      <option value="4"> Fine </option>
      <option value="5"> Finer </option>
      <option value="6"> Finest </option>
    </select>
  </div>
  <p id="debugLevelhelp" class="form-text text-muted"> Sets how much logging is written to the debug log, increasing the log level can help Jthink identify the cause of any problems encountered in SongKong </p>
</div>

Demo : https://jsfiddle.net/0y9uyn76/4/

Answer №2

One way to resize a select element without affecting the layout of other controls is to wrap it in a div and apply a style of 30ch to that div.

<div>
  <div class="form-group">
    <div>
      <label for="debugLevel" id="debugLevellabel">
                Debug Log Level
            </label>
      <div style="width:30ch">
        <select class="custom-select" name="debugLevel" id="debugLevel" aria-describedby="debugLevelhelp">

          <option value="0">
            Severe
          </option>
          <option selected="selected" value="1">
            Warning
          </option>
          <option value="2">
            Info
          </option>
          <option value="3">
            Config
          </option>
          <option value="4">
            Fine
          </option>
          <option value="5">
            Finer
          </option>
          <option value="6">
            Finest
          </option>

        </select>
      </div>
    </div>
    <p id="debugLevelhelp" class="form-text text-muted">
      Sets how much logging is written to the debug log, increasing the log level can help Jthink identify the cause of any problems encountered in SongKong
    </p>
  </div>
  <div class="form-group">
    <div>
      <label for="ioDebugLevel" id="ioDebugLevellabel">
                Debug IO Log Level
            </label>
      <div style="width:30ch">
        <select class="custom-select" name="ioDebugLevel" id="ioDebugLevel" aria-describedby="ioDebugLevelhelp">

          <option value="0">
            Severe
          </option>
          <option selected="selected" value="1">
            Warning
          </option>
          <option value="2">
            Info
          </option>
          <option value="3">
            Config
          </option>
          <option value="4">
            Fine
          </option>
          <option value="5">
            Finer
          </option>
          <option value="6">
            Finest
          </option>

        </select>
      </div>
    </div>
    <p id="ioDebugLevelhelp" class="form-text text-muted">
      Sets how much logging is written to the debug log when SongKong reads files or saves changes to files, increasing the log level can help Jthink identify the cause of any problems encountered in SongKong
    </p>
  </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

Adjusting the 'Division' Style in HTML

Currently, I am utilizing the incredible StackEdit for my documents. One of its awesome features is the ability to export an HTML file of the document. This document utilizes The default CSS File. As an example, here is the code from the downloaded HT ...

Arranging the form fields on top of an image

Check out the code I've been working on here. I'm facing an issue where the text disappears when I try to position it next to an image. Is it possibly going behind the image? Thank you for your assistance and time. ...

Using jQuery to wrap each individual word and letter within each word

Imagine you have a string of text. Each letter in that string should be assigned a random font from an array. To achieve this, I enclosed each letter within a span and randomized the font-family. The problem with this method is word-wrap. When a sentence ...

Offsets of elements once the animation is finished

Having issues with CSS animation. I am trying to get the small plus icon to rotate/spin in the center and stop once it is completed. Currently, the animation works fine but after it finishes, the icon shifts to the bottom and right. .link { position: ...

The -moz-use-text-color property does not function properly in Chrome and IE versions 9 and above

I have added -moz-use-text-color for border-color in the following CSS code: .booksline{ border-image: none; margin: 0 auto 0px; width: 96%; height:220px; padding:20px 20px 20px 0; border-width: 1px 1px medium; border-style: solid solid none; border-colo ...

The failure of CSS Layout Design

So I'm attempting to create a profile cover like the one shown below: This is what my code looks like: <div class="show_cover"> <%= image_tag @show.cover(:show_cover) %> <!-- Mouseover Options--> <% if current_user == @ho ...

Creating a specific type of table using Ruby on Rails

Is it feasible to create a table with two columns, where the left column incorporates multiple rows with a scroll bar, and the right column contains a large box housing buttons that alter based on the selection of blocks in the left column? ...

How to ensure your content is always visible on Mobile Safari iOS 8 by making sure it stays

Although minimal-ui is no longer supported on iOS8, I've encountered an issue with some fixed content at the bottom of a webpage. The page height is set to 100vh to cover the entire viewport, but some of the content at the bottom is extending below t ...

I'm using SASS in my project, can you provide guidance on customizing the UI with @aws-amplify/ui

I've recently taken over a Next.js (React) project from another developer and they've used .scss files (SASS) for styling. The specific project I'm working on can be found here https://github.com/codebushi/nextjs-starter-dimension My task n ...

Is it possible for JavaScript to identify modifications in the HTML, like an input made with Ctrl+Shift+I?

Check out this website I'm currently working on. As a fun challenge for users, we're encouraging them to use ctrl+shift+i to manipulate the HTML and modify certain elements. Is it possible for Javascript or CSS/HTML to detect these changes? For ...

Issue arising from CSS and div elements

I am facing some challenges in making certain divs behave as desired. Take a look at the following references: Link 1: - contains two divs Link 2: - has only one div I am trying to utilize two divs (.content) without them being positioned incorrectly ...

The Bootstrap HTML code fails to display correctly after adding a button

After minimizing the page, I need to display some buttons but for some reason, the menu is not showing. How can I fix this? <div class="navbar navbar-inverse navbar-static-top"> <div class="container"> <a href="#" class="navb ...

Manipulating graphics with CSS - dynamically linking Divs

I'm attempting to create a unique design by connecting a series of dynamically generated content divs with CSS. The goal is to have a curved line every three divs, complete with an arrow image at the end and an additional line to separate the contents ...

There seems to be an issue with the functionality of the `Nav` component in React-bootstrap when used within a `NavBar` with the

Desiring my NavBar to occupy the entire available width, I included a fill flag within the Nav section of the NavBar component. <Navbar bg="light" expand="lg"> <Navbar.Toggle aria-controls="basic-navbar-nav" /&g ...

Embedding a compact table using HTML on the sidebar

I am facing a challenge in this HTML code where the table appears next to the spans. How can I make sure that the spans appear below the table instead? <div> <table align="right"> <tr> <td>smthg</td> ...

Irregular dimensions of div elements following automated selection

My tile div can be selected using the mouse or a timed event. Here are the states of selected and unselected : Not .selected .selected applied .tile { height: 70px; padding: 5px 10px 5px 10px; margin: 8px auto 0px auto; width: 280px; ...

Interested in incorporating complimentary templates from 'https://startbootstrap.com/' within my React application

Currently, I am keen on downloading a free bootstrap template from here and incorporating it into my react app. Despite my efforts to explore the available documentation, I have not been able to find any guidance on how to seamlessly merge these templates ...

Position footer at the bottom of the webpage and adjust the height to be filled with main content using Bootstrap 4

I am currently in the process of designing a bootstrap layout that includes a header navbar along with a footer. My goal is to ensure that the footer is always at the bottom of the page, even if there is limited content. Here's what I have implemente ...

Any suggestions on resolving the issue of a white background in the Spotify embed code?

<iframe src="https://open.spotify.com/embed/track/6wsqVwoiVH2kde4k4KKAFU?utm_source=generator" width="500" height="100" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading=&q ...

Tips for adjusting the item count in mat-autocomplete

For quite some time now, I've been attempting to adjust the number of items in a mat-autocomplete without any luck. My project involves using material.angular.io Below is a snippet of my code import { Component, OnInit } from '@angular/core&a ...