What is the best way to add spacing between the fields within a Bootstrap 4 form row that spans across 2 rows when viewed on small displays?

Trying to create a bootstrap 4 form with fields in one row for larger screens and two rows for smaller screens.

The attempt was made using the following code:

<div class="form-group row">
  <label class="col-sm-2 col-form-label">File:</label>
  <div class="col-sm-10 col-md-6">
    <input class="form-control" type="text" />
  </div>
  <label class="col-sm-2 col-md-2 col-form-label">Type</label>
  <div class="col-sm-4 col-md-2">
    <select class="form-control"><option value=""></option></select>
  </div>
</div>

For md+ screens, the fields will be in one row: 2+6+2+2
For sm screens, the first row will consist of 2+10 (let's call it A), and the next row - B - will have 2+4...

The issue arises between rows A and B where there is no margin, unlike if there were two divs with the "row" class.

Refer to this fiddle for visualization: https://jsfiddle.net/truesoft/q1djhp5b/. Row 1 demonstrates the margin between the rows, while Type label and select do not have a margin when on the next line for sm screens. Adding a margin for the submit button may cause display issues in other scenarios.

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

What would be the most effective approach to address this challenge?

Answer №1

You were so close to getting it right! All you need to do is incorporate the appropriate spacing utilities into your code.

.row {
  background: #f8f9fa;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="form-group row py-sm-3 mb-0">
    <label class="col-sm-2 col-form-label">Row 1:</label>
    <div class="col-sm-10">
      <input class="form-control" type="text">
    </div>
  </div>
  <div class="form-group row pb-3">
    <label class="col-sm-2 col-form-label" for="file">File:</label>
    <div class="col-sm-10 col-md-6">
      <input class="form-control mb-sm-3" id="file" type="text">
    </div>
    <label class="col-sm-2 col-md-1 col-form-label" for="type">Type</label>
    <div class="col-sm-5 col-md-3 mb-3">
      <select class="form-control" id="type">
        <option value=""></option>
        <option value="1">1</option>
        </select>
    </div>
    <div class="offset-sm-1 offset-md-8 col-sm-4">
      <input name="commit" value="Save" class="btn btn-primary btn-block" type="submit">
    </div>
  </div>
</div>

Just a friendly note: I also made an adjustment to enlarge the type field on md view, as it appeared too cramped and there was some space available for expansion. However, this change is completely optional and can be reversed if needed.

The main thing lacking in your example was the incorporation of the

{property}{sides}-{breakpoint}-{size}
classes.

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

Troubleshooting problem with presenting real-time data in a Bootstrap Carousel using PHP

Currently, I am in the process of developing a dynamic events calendar to showcase on a website homepage. The information displayed on this calendar is retrieved from a database using PHP. Additionally, I have implemented the Bootstrap framework (version 4 ...

Ways to align pictures in the center vertically when placed side by side

Can someone help me make the transition between tall and short images on my website smoother? I am not familiar with coding terminology, so please bear with me. The attached image provides a visual representation of the changes I am looking for: click he ...

Image in an Outlook email surrounded by padding and enclosed in a paragraph

I am facing an issue with my email signature setup in Outlook Live where there is a white space below the image. After trying to add vertical-align:bottom to the CSS, it seems that Outlook Live does not accept it. The code for my HTML in Outlook 20xx is a ...

Find the height of the viewport using jQuery, subtracting (n) pixels

Apologies if the topic seems puzzling, I couldn't find a better way to explain it. I utilized jQuery to adjust the height of a div to match the size of the viewport. var slidevh = function() { var bheight = $(window).height(); $(".container" ...

Struggling to figure out the ::before border trim issue

As I work on designing a banner for a webpage, I have added a sliced bottom right border using the ::before pseudo class. The code snippet I used includes: &::before { content: ""; position: absolute; bottom: 0; right: 0; ...

Prevent the parent component's ripple effect from being activated by the child component

If I have a simple code snippet like the following: <ListItem button={true} > <Typography variant='caption' color='primary'> {value} </Typography> <Button onClick={foo} > Button ...

Tips for employing CSS hover within HTML elements?

Is there a way to achieve the following: <li style="hover:background-color:#006db9;"> I have tried this approach but it doesn't seem to work. Is there an alternative method, or do I need to include the CSS in the head section or an external CS ...

Lost in a sea of confusion with ember-cli-postcss and autoprefixer

I'm currently working on an ember build that incorporates autoprefixer. My issue arises from the fact that the output location for 'assets/application-name.css' has shifted from its normal path to 'app/styles/app.css', and I would ...

Add a plethora of images to the canvas

Just starting out with Fabric.js and trying to figure out how to draw a picture on the canvas after a drop event. I managed to do it once, but am struggling with inserting more pictures onto the canvas (each new drop event replaces the previous picture). ...

JavaScript for Verifying Phone Numbers

After extensive research and tutorials, I am still unable to figure out what is going wrong with my work. I have a button that looks like this: Despite Stack Overflow causing some trouble for me, please ignore the missing class as it is there. This is no ...

The functionality of my website is currently experiencing difficulties when accessed through the Android UC Browser

My website, , is experiencing issues with product loading and the '+' button in the side menu not working on UC Browser. It works fine on other Android browsers like Chrome and Firefox, but I am confused as to why it is not functioning properly o ...

Tips for capturing changes in a "count" variable and executing actions based on its value

I have a counter on my webpage and I'm trying to change the style of an element based on the count variable. I tried using the .change action but I haven't been able to get it working. It might not be the right solution. Can anyone provide some ...

How do I set up a 5 column layout for the home page and switch to a 4 column layout for category pages in Magento?

Is it possible to display 5 columns of products on the Magento home page and only show 4 columns in the category area, or is this a pre-set parameter that applies platform-wide? ...

What is the best way to ensure that JavaScript is loaded in a specific sequential order?

I'm having trouble ensuring that JS files load in the correct sequence, despite my efforts to research async and defer on various platforms. This is the structure of my code: <script src="lang.js"></script> <!--loading either ...

Struggling to implement a Datepicker in the date column of a table created using HTML and JavaScript

I am encountering an issue while creating a table in html and javascript. I am unable to use a Datepicker in the date column when inserting a new row, even though it works fine when using in normal html code. Below is the javascript function: //Script fo ...

Tips for using a unique format for a single field in jqGrid

My JQGRID site has table configuration set up like this: configid config rule 1. Application_name 'MyApps' 2. Max number 1,000,00 3. Application End 12/31/2012 Some cells are for numbers and others for dates. If a user wants to edit a date ce ...

Sleek carousel design with optimal spacing between images

Solving the Issue Utilizing Ken Wheeler's Slick plugin has allowed me to create a carousel on my website. However, I've encountered an issue where the images inside the <a> tag aren't evenly spaced and are even overlapping in some ins ...

Ways to ensure the first div always expands when sorting in AngularJS

In my AngularJS application, I have a set of div elements generated using ng-repeat. The first div is always expanded by default upon loading the page. Now, when I click a button to sort the divs based on their ID in the JSON data, I want the top div to ...

How to Utilize USB Devices with Node-Webkit?

I am in the process of creating a node-webkit application that must be compatible with all three major desktop operating systems (Windows, Mac, and Linux). My goal is to establish a connection between my app and a USB device that is plugged in, but I am en ...

Issue with division operation in HTML not functioning

I've been working on a division calculation within an HTML file, but so far I haven't had success using PHP or JavaScript. While {{ property.listprice }} is correctly displaying a number in the HTML file, the other elements are not functioning as ...