Creating Bootstrap 4 forms with full-width input fields within an inline form layout

In my observation, there is a noticeable variance in the behavior of in-line forms between the older version of Bootstrap, v3, and the newer version, V4.

I have included two code snippets below:

Bootstrap 3

In Bootstrap 3, when the page width decreases, each form element adjusts to occupy the entire width of the page. However, this functionality no longer exists in Bootstrap 4. Surprisingly, I couldn't find any mention of this full-width behavior in the Bootstrap 3 documentation. Even though it's not documented, the fields still expand to full width in an inline form when enclosed in a form-group div.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<h1>bootstrap/3.3.6</h1>

{Form HTML content for Bootstrap 3}
 

Bootstrap 4

Now, using the same HTML structure in Bootstrap 4 doesn't produce the full-width behavior anymore. Just like in Bootstrap 3, there seems to be no reference to the full-width form fields on narrower pages or in inline forms within the Bootstrap 4 documentation.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<h1>bootstrap/4.0.0</h1>

{Form HTML content for Bootstrap 4}
 

I am curious if anyone knows of a way to replicate the Bootstrap 3 behavior in Bootstrap 4, where the form-fields expand to full width as on wider screens?

I prefer not to remove the class='form-inline' from the form, as I want it to appear inline on larger screens.

Answer №1

To optimize your code, simply remove the '.form-group' div and incorporate a media query for resolutions up to 767 pixels. With Bootstrap 4, inputs automatically expand to 100% width on screens that are below 576px wide.

@media (max-width: 767px){
  .form-inline .form-control {
    display: block;
    width: 100%;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<h1>bootstrap/4.0.0</h1>

<form class='form-inline' role='form' action='search.php' method='post' id='form_search'>
   <input type='hidden' name='method' value='search' />

      <input type='text' class='form-control' id='str' name='str' placeholder = 'Search'>

      <select name='mn' id='mn' class='form-control'>
         <option value='n'>Month</option>
         <option  value='1'>January</option>
         <option  value='2'>February</option>
      </select>

      <select class='form-control' name='yr' id='yr'>
         <option value='n'>Year</option>
         <option  value='1995'>1995</option>
         <option  value='1996'>1996</option>
      </select>

      <select name='format' id='format' class='form-control'>
         <option value='n'>Format</option>
         <option  value='1'>1</option>
      </select>

      <select name='week_end_or_not' id='week_end_or_not' class='form-control'>
         <option value='n'>Weekend Or Not</option>
         <option  value='week'>Week Days</option>
         <option  value='weekend'>Weekend</option>
      </select>

      <select name='work_days' id='work_days' class='form-control'>
         <option value='n'>Work Days?</option>
         <option  value='yes'>Yes</option>
         <option  value='no'>No</option>
      </select>

      <select name='exact_phrase' id='exact_phrase' class='form-control'>
         <option value='n'>Exact Phrase?</option>
         <option  value='yes'>Yes</option>
         <option  value='no'>No</option>
      </select>

   <button type='submit' class='btn btn-success'><i class="fa fa-search"></i> Search</button>
</form>

For a live demonstration, check out this Demo

Answer №2

Easy Solution: Using .form-inline Class Without Grouping

If you want a simple solution, according to Bootstrap documentation, you can utilize the .form-inline class on your form without grouping single input elements. Just remove the corresponding <div class="form-group"> elements.

To customize breakpoints, refer to the Responsive Breakpoints section in the documentation.

Check out the live example below (expand to fullscreen and adjust viewport width for effect):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<h1>Bootstrap 4.0.0 Example</h1>

<form class='form-inline' role='form' action='search.php' method='post' id='form_search'>
  <input type='hidden' name='method' value='search' />
  <input type='text' class='form-control' id='str' name='str' placeholder='Search'>
  
  <!-- Additional input fields go here -->
  
  <button type='submit' class='btn btn-success'><i class="fa fa-search"></i> Search</button>

</form>

Better Approach: Utilizing .input-group instead of .form-group

If you prefer grouping your input elements, consider swapping out .form-group with .input-group as explained in Bootstrap's documentation about Multiple Inputs.

View the responsive demonstration below:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<h1>Bootstrap 4.0.0 Example</h1>

<form class='form-inline' role='form' action='search.php' method='post' id='form_search'>
   <input type='hidden' name='method' value='search' />
   <div class='form-group'>
      <input type='text' class='form-control' id='str' name='str' placeholder = 'Search'>
   </div>

   <!-- Insert additional input fields wrapped in .input-group -->

   <button type='submit' class='btn btn-success'><i class="fa fa-search"></i> Search</button>
</form>

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

Ways to expand the search box in a navigation bar with Bootstrap 5

Recently delving into front-end development, I've been working on designing a webpage that includes a search box (you can view the jsfiddle here). I decided to borrow the navbar component from https://getbootstrap.com/docs/5.0/components/navbar/ and m ...

jQuery divs seem to "gaze up" towards the mouse pointer

I am attempting to recreate a fascinating effect using jQuery - where "cards" move in response to the mouse cursor as if they are looking up at it. I have come across a script that almost achieves what I need, but I require the ability to control the mov ...

When a value is deleted from an input in jQuery, all other changes become void

I am experiencing an issue with Javascript where even after deleting or changing the value of a field, jQuery effects still persist. Is there a way to reset all changes made when the input value is deleted? For example, you can visit , type 6 in and then ...

Is there a way to incorporate CSS or tables when converting HTML to PDF using JavaScript?

While working on a project, I successfully converted an HTML file into a PDF. However, the output did not display the CSS design. Can anyone provide suggestions on how to include CSS design in the PDF file? Below is the JavaScript function code: $(funct ...

Do CSS Stylesheets load asynchronously?

Is there a difference in how stylesheets are loaded via the link tag - asynchronously or synchronously? In my design, I have two stylesheets: mura.css and typography.css. They are loaded within the head section of the page, with typography.css being load ...

Update the Django translation file with new modifications

Currently, I am in the process of developing a web application using Django and have recently integrated Django allauth to streamline the creation of my user interface. I have successfully customized the templates by incorporating the templates from their ...

Can you place more than one Twitter Bootstrap carousel on a single webpage?

Latest Version of Twitter Bootstrap: 2.0.3 Sample HTML Code: <!DOCTYPE html> <html dir="ltr" lang="en-US" xmlns:og="http://opengraphprotocol.org/schema/"> <head> <link rel="stylesheet" type="text/css" media="all" href="reddlec/style. ...

Tips for displaying multiple table rows when clicked

Looking to display two hidden rows at a time out of a total of 5 rows when a display button is clicked. Once the last 2 rows are displayed, the button should become disabled. Similar to a question on Stack Overflow, but unable to use the solution provided ...

Guide to configuring capybara-webkit for testing ajax requests and capturing screenshots containing HTML with loaded CSS and JavaScript

Having some trouble setting up capybara-webkit on my Rails 4.2.1 app. Tests are running fine, links are being clicked, and ajax calls are working, but there's one odd issue with capybara-screenshot gem. I wanted to capture html screenshots with all as ...

positioning a div based on the location of another div

I am currently working on aligning a div that sits at the bottom of all other divs. The last div is set to float and aligned at the top using CSS. I am unsure how to align it from the left side without any issues when the screen resolution changes. Is th ...

Attempting to vertically center text within a percentage div

I'm trying to create a button that is centered on the screen with text aligned vertically within the button. I want to achieve this using CSS only and restrict the usage of percentages only. Here's what I have so far: Check out my code snippet h ...

Can I add different tags directly inside a div container in Bootstrap 3 without using .row and .col?

Should I place a tag directly inside in Bootstrap 3? or is it better to have each inside .row>.col-md-# Is this layout acceptable, or could it potentially cause issues on mobile devices? <div class="container"> <h1>Lorem ipsum dolor sit ...

Modifying SVG gradients with JavaScript

My goal is to modify the stop color of a gradient displayed in an SVG element. I've been trying to adjust the stop-color attribute, but so far, my attempts have not been successful: <svg><defs> <linearGradient gradientTransform="rotat ...

Discover the art of implementing linear gradient backgrounds in Tailwind css effortlessly

Is there a way to incorporate this specific color scheme into my background? linear-gradient(355.45deg, #FFFFFF 11.26%, rgba(255, 255, 255, 0) 95.74%) I have knowledge on how to implement this color using regular CSS, but I'm curious about how it can ...

Load a page from a different domain using uframe

Looking for a solution to successfully load an external URI using uFrame. Currently encountering an "Access Denied" issue when attempting to do so on Firefox. Any suggestions? ...

Tips for saving input data from an HTML page to a text file

How can I save the inputs from a simple form as a text file using HTML code? Here is an example of the HTML code: <div id="wrapper"> <div class="main-content"> <div class="header"> </div> ...

Tips for inserting an image within a horizontal list of items

Struggling to find a solution here. I have a menu with a unique bar design between list elements, featuring breaks at the top and bottom. I attempted to use li:after in the CSS to insert an image, but encountered issues with padding and margin. Below is t ...

When resetting the function, make sure to move the class to the closest sibling element

I am currently utilizing a slider that employs the .prev and .next jQuery functions to toggle the active class, indicating which slide is being displayed. Here is the code responsible for this functionality. It identifies the current sibling with the acti ...

Is it possible to customize the arrow on a drop-down menu?

Here is an example of the page I am facing issues with: If you look at the right side of the bottom bar, you will notice a selection option for different themes. I have been attempting to replace the down arrow with an image. Below is the code I have used ...

What is the process for incorporating personalized field attributes using an inlineformset_factory in Django?

I am currently working with an 'inlineformset_factory' and I am trying to determine the best way to set attributes for the individual fields. So far, I have not been able to find a method to add them without resorting to using javascript, which i ...