Achieving vertical alignment for Bootstrap inputs on mobile devices and horizontal alignment on desktop screens

The code below showcases my current setup:

<form>
    <div class="row">
        <div class="col-md-8">
            <label class="sr-only" for="first-name">First name</label>
            <input type="text" class="form-control mr-2" id="first-name" name="first_name" required>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <label class="sr-only" for="last-name">Last Name</label>
            <input type="text" class="form-control mr-2" id="last-name" name="last_name" required>
        </div>

        <div class="col-md-2">
            <button type="button" class="btn btn-success">
                Submit
            </button>
        </div>
    </div>
</form>

For mobile devices, the layout appears as shown in the following image: https://i.sstatic.net/HnUuC.png

My attempts to improve alignment include adding col-md-offset-4 to the input divs.

Additionally, I tried using justify-content-center on the divs.

On desktop, I want the alignment to resemble the layout in this image, hence why I dynamically apply the class form-inline on larger resolutions: https://i.sstatic.net/8l8ed.png

How can I center align these elements with the submit button on mobile while maintaining the desired desktop layout?

Answer №1

Make the necessary changes to your code as shown below

<form>
    <div class="row">
        <div class="col-md-4 offset-md-2">
            <label class="sr-only" for="first-name">First name</label>
            <input type="text" class="form-control mr-2" id="first-name" name="first_name" required="">
        </div>
        <div class="col-md-4">
            <label class="sr-only" for="last-name">Last Name</label>
            <input type="text" class="form-control mr-2" id="last-name" name="last_name" required="">
        </div>
        <div class="col-md-2">
        <button type="button" class="btn btn-success">
                Submit
            </button>
            </div>
    </div>

</form>

To ensure this layout is responsive for small screens, you can add the class col-sm-4 as shown below

<!DOCTYPE html>
<html lang="en>
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  
  
  <form>
    <div class="row">
        <div class="col-md-4 offset-md-2">
            <label class="sr-only" for="first-name">First name</label>
            <input type="text" class="form-control mr-2" id="first-name" name="first_name" required="">
        </div>
        <div class="col-md-4">
            <label class="sr-only" for="last-name">Last Name</label>
            <input type="text" class="form-control mr-2" id="last-name" name="last_name" required="">
        </div>
        <div class="col-md-2">
        <button type="button" class="btn btn-success">
                Submit
            </button>
            </div>
    </div>

</form>
  
  
  
</div>

</body>
</html>

Answer №2

I may not have completely grasped your question, but I made an attempt to address it.

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

<form>
  <div class="row m-0">
    <div class="offset-md-2 col-md-8">
      <label class="sr-only" for="first-name">First name</label>
      <input type="text" class="form-control mr-2" id="first-name" name="first_name" required>
    </div>
  </div>

  <div class="row m-0">
    <div class="offset-md-2 col-md-8">
      <label class="sr-only" for="last-name">Last Name</label>
      <input type="text" class="form-control mr-2" id="last-name" name="last_name" required>
    </div>

    <div class="col-md-2 text-center text-md-left mt-3 mt-md-0">
      <button type="button" class="btn btn-success">Submit</button>
    </div>
  </div>
</form>


<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7c7d8c7c7d2c599ddc4f7869986819987">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

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 is the best way to modify a newly added element using jQuery?

When a user clicks a button on my screen, a new template is loaded dynamically using jQuery's .load() method. This transition adds new HTML to the page that was not present when the script initially loaded: (function($) { $('.go').on("c ...

Use a PHP form to send an email with HTML formatting, rather than plain text

I am facing an issue with sending emails from a web form. The received email needs to be displayed as HTML and not text. function createMailBodyLine ( $title, $value ) { $value = nl2br(htmlspecialchars($value)); return "<tr> ...

Center-align the text within the dropdown menu

Here is the HTML code snippet I am working on: <select id="ddlCountry" placeholder="optional" class="select" title="Select Country"> <option value="0">country</option> </select> This is how the CSS is set up: .select { width ...

The functionality of Jquery UI is not compatible with version 1.12

Incorporating jQuery UI into my current project has presented some challenges. Both the jquery-ui.min.css and jquery-ui.min.js files are version 1.12, so I opted for the latest jQuery version, jquery-3.2.1.min.js. Specifically, I decided to test the datep ...

The Ultimate Slider: Highlighting Custom Navigation Link as Active While Navigating with Arrows

I have implemented custom navigation links for my slick slider in order to navigate to specific slides. The functionality works perfectly, but I encountered an issue when I added the built-in arrows provided by the slider. Whenever I use these arrows to n ...

Discover the complete compilation of CSS class regulations (derived from various stylesheets) along with their currently active properties for the chosen element

Is there a way to retrieve all the matched CSS Selectors for a selected element and access the properties of each active class applied to that element? I have researched methods like using getMatchedCSSRules and checking out However, I am hesitant to use ...

I'm encountering inexplicable duplications of elements on my Wordpress site

Here is an example of my template header: <header> <?php if ( function_exists( 'jetpack_the_site_logo' ) ) jetpack_the_site_logo(); ?> <a class="menu-toggle">menu</div> <?php wp_nav_menu( array('them ...

How come @keyframes animations do not seem to cooperate with Vue.js?

I'm attempting to create a flashing link upon site load, but I'm encountering issues with the animation not working in Vue. Interestingly, when testing the same code without Vue in a separate file, it functions perfectly fine. Below is the spec ...

Issue with template updating when utilizing ui-router and ion-tabs in Ionic framework

CODE http://codepen.io/hawkphil/pen/LEBNVB I have set up two pages (link1 and link2) accessible from a side menu. Each page contains 2 tabs: link1: tab 1.1 and tab 1.2 link2: tab 2.1 and tab 2.2 To organize the tabs, I am utilizing ion-tabs for each p ...

Disable hover effects in CSS with JavaScript

I am looking for a way to deactivate CSS hover functionality using JavaScript. Within a form, there is a button that sends data to the database server. By utilizing OnClientClick() of an ASP.NET Button control, my goal is to modify the text of the element ...

Tips for resolving the need to manually set h-100 to HTML and BODY tags and addressing centering problems

When I don't set my HTML and BODY tag to have the class "h-100," they display at a height of around 210 pixels. This then requires me to also set lower-level elements with the h-100 class. I'm unsure how to resolve this issue without including t ...

Looking to dynamically generate HTML tags using jQuery and JSON?

Looking for help with inserting HTML code into a div using jQuery. <div id="addme"></div> Here is some HTML with PHP: <div class="col-md-4 product secondproduct"> <div class="images1"> <a href="<?php echo base_u ...

Restricting a checkbox to a maximum of 5 checkmarks

In a multi-column table, each column is represented by a checkmark. I want to limit the ability to tick a checkmark to only 5 checkmarks. Here is the code that has been implemented: <tbody> <ng-container *ngFor="let col of testData" ...

Is it possible to use bootstrap to hide specific components depending on the size of the media screen?

Struggling with styling responsiveness and hoping to find a Bootstrap class that can easily hide components on medium and small screens. I'm looking for a way to hide headings, paragraphs, buttons, and more without any extra hassle. ...

Display an image when the cursor hovers over a text

I'm attempting to display an image when hovering over specific text. The image should not replace the text, but appear in a different location. The concept is as follows: When hovering over the word "Google", the Google logo should appear in the red ...

Can Selenium be executed from a <py-script> within an HTML file?

prefs = webdriver.ChromeOptions() prefs.add_experimental_option("detach", True) driver = webdriver.Chrome(ChromeDriverManager().install(), options=prefs) Following this code, it seems to stop running (It runs fine in a .py file so I believe all ...

Reacting to the dynamic removal of spaces

Is there a way to dynamically remove the space between removed chips and older chips in Material-UI when deleting one of them? <div className="row contactsContainer"> {contacts.map((contac ...

Pressing the button will add text into the input field

Below is a jsfiddle link showcasing the current issue I am trying to address: http://jsfiddle.net/YD6PL/61/ Here is the HTML code snippet: <input type="text> <input type="text> <input type="text> <div> <button class="buttons"& ...

creating PHP buttons to update inventory

As a newcomer to AJAX and PHP, I am facing some challenges with an inventory management system built using phpmyadmin and wamp. The registration/login system is functioning well. However, upon user login, a table displaying all inventory items is generated ...

The functionality of the Angular ng-bind-html directive may be unreliable in specific scenarios

I've exhausted all the solutions found on stack overflow but still haven't found a solution. Using angular 1.4.4 and ng-repeat directive, I am attempting to display a HTML comment inside a table row. A sample comment looks like 'Test commen ...