Tips for centering text with a Bootstrap switch

Currently, I am in the process of creating a form using Bootstrap, and I have been progressing well, except for one issue. I want to integrate a switch type checkbox into the form, with a label on top and text on either side of it. Despite several attempts, I have been unable to align the two texts and the checkbox in a single line.

Here is what it looks like at the moment:

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

Below is a detailed example of the functioning form:

<!DOCTYPE html>
<html>

<head>
  <title>TestSite</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8e8383989f989e8d9cacd9c2ddc2df">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <!--link th:rel="stylesheet" th:href="@{/css/styles.css}"/-->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>

<body class="bodyClass sb-nav-fixed">

  <div class="container">
    <div class="card shadow-lg border-0 rounded-lg mt-5">
      <div class="card-header">
        <h2 class="text-left font-weight-light my-4">Add content</h2>
      </div>
      <div class="card-body">
        <form>
          <div class="row">
             <div class="col">
              <label class="small mb-1" for="price">Price:</label>
              <div class="input-group mb-3">
                <input id="price" name="price" type="text" class="form-control" placeholder="Price">
                <span class="input-group-text">$</span>
              </div>
            </div>
            <div class="col">
              <label for="revenue">Revenue</label>
              <div class="form-text">Fix</div>
              <div class="form-check form-switch form-check-inline">
                <input id="revenue" class="form-check-input form-check-inline" type="checkbox">
              </div>
              <div class="form-text">Percent</div>
            </div>
          </div>
          <div class="btn btn-lg btn-secondary btn-block col-4">Cancel</div>
          <button class="btn btn-lg btn-primary btn-block col-4" type="submit">Save</button>
        </form>
      </div>
    </div>

  </div>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/js/all.min.js" crossorigin="anonymous"></script>

  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22404d4d56515650435262170c130c11">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>

</html>

This aspect is part of the administrative panel of my project, and I do not intend to utilize it on mobile devices; it is specifically designed for PC browsers, hence the use of col/row.

Answer №1

To achieve the desired layout, you can use a container with flex to align the elements in the same line or float them to one side. In this case, the former approach was chosen. The only remaining task is to slightly adjust the positioning of the components.

Update: The updated version includes a single label that dynamically changes its text based on the checkbox state.

.my-switch {
  background: lightyellow;
  padding-top: 6px;
}

.my-switch .form-switch {
  margin: 3px 0px 0 15px;
}
<!DOCTYPE html>
<html>

<head>
  <title>TestSite</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa8a5a5beb9beb8abba8affe4fbe4f9">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <!--link th:rel="stylesheet" th:href="@{/css/styles.css}"/-->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>

<body class="bodyClass sb-nav-fixed">

  <div class="container">
    <div class="card shadow-lg border-0 rounded-lg mt-5">
      <div class="card-header">
        <h2 class="text-left font-weight-light my-4">Add content</h2>
      </div>
      <div class="card-body">
        <form>
          <div class="row">
            <div class="col">
              <label class="small mb-1" for="price">Price:</label>
              <div class="input-group mb-3">
                <input id="price" name="price" type="text" class="form-control" placeholder="Price">
                <span class="input-group-text">$</span>
              </div>
            </div>
            <div class="col col-revenue">
              <label for="revenue">Revenue</label>

              <div class="d-flex my-switch">
                <div class="form-text text-1">State</div>

                <div class="form-check form-switch form-check-inline">
                  <input id="revenue" class="form-check-input form-check-inline" type="checkbox">
                </div>

                <div class="form-text text-2">ON or OFF</div>
              </div>


            </div>
          </div>
          <div class="btn btn-lg btn-secondary btn-block col-4">Cancel</div>
          <button class="btn btn-lg btn-primary btn-block col-4" type="submit">Save</button>
        </form>
      </div>
    </div>

  </div>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/js/all.min.js" crossorigin="anonymous"></script>

  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2909d9d868186809382b2c7dcc3dcc1">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>

</html>

Answer №2

Answer is similar to IT goldman's, but lacks any additional styling.

When using bootstrap 5, include a "d-flex" class at the outermost layer and a "float-end" on your button/switch

<div class="d-flex">
    <p>
        This is the first sentence
    </p>
    <div class="form-check form-switch form-check-inline">
      <input class="form-check-input float-end"
             type="checkbox"
             role="switch"
             [title]="'Example button'"
             [id]="'example'">
    </div>
    <p>
      This is the second sentence
    </p>
</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 canvas context position after resizing

I've been experimenting with using a canvas as a texture in three.js. Since three.js requires textures to have dimensions that are powers of two, I initially set the canvas width and height to [512, 512]. However, I want the final canvas to have non-p ...

The textbox is not able to process the complete input string provided

I am having trouble with entering an email address into a textbox, for example [email protected], as it only accepts dd@g. Here is the HTML code: <input type="email" class="form-control ng-pristine ng-valid-email ng-invalid ng-invalid-required ng ...

Adjust the height of the image using CSS while maintaining its aspect ratio within the Gall-Peters projection

I've implemented a Gall-Peters projection on my website and I'm looking for the right CSS code to maintain its aspect ratio. I want the image to take up 100% of the available width without elongating the countries' shapes. Can anyone help wi ...

Optimizing performance: Enhance readback operations in Canvas2D by enabling the willReadFrequently attribute for faster getImageData processing

I am currently utilizing the react-wordcloud package and encountering an issue where the word cloud adjusts to fit 70% of the screen size whenever the container size changes. My console is being flooded with this warning message: https://i.sstatic.net/vdV ...

Should I use a single CSS file or separate CSS files for each page?

As I construct my website pages, I often ponder whether it's best to utilize separate stylesheets for each page or to have one comprehensive stylesheet for the entire site. In terms of loading efficiency, wouldn't having individual files be more ...

AngularJS enhances user experience by allowing textareas to expand upon click

I am just starting to learn about angular.js and I'm wondering how to add a text area when clicking on an image. ....... ..... ..... </thead> <tbody> <tr ng-repeat="stu in Studentlist"> <td>{{stu.rollno}}</td> <td> ...

What is the method for executing a server-side script without it being transmitted to the browser in any way?

Although the title may be misleading, my goal is to have my website execute a php file on the server side using arguments extracted from the html code. For example: document.getElementById("example").value; I want to run this operation on the se ...

When using iOS, inserting an iFrame with a source URL (SRC) on a webpage will automatically open the URL

Currently facing a peculiar issue within a Cordova app, specifically on iOS. The scenario is: I have an HTML page with existing content. At a later stage, I fetch additional HTML from a remote source and inject it into the original HTML page. However, whe ...

Is it possible to execute a server-side click on a div element?

I have a div with an image and name inside. How can I make this div trigger a server-side click event without changing its structure? Here is my code: <div class="tutorial" style="margin-left:5px;"> TUTORIAL<div class="firstico" style=" ...

Step-by-step guide on resolving the issue of an ajax form redirecting to a mailer

I have been working on implementing the contact form on an HTML page. The validation was functioning correctly until I added it, and now the form is no longer submitting properly. Despite this issue, the page still redirects to mailer.php. The AJAX functio ...

What is the process for implementing an image as the background instead of a color for each column in a Google chart?

I'm currently working with Google Chart and I have set up a column chart. However, I am looking to display a background image instead of a background color in each column. Is this achievable with Google Chart? If so, how can I accomplish this? If anyo ...

The target="_blank" attribute does not seem to function properly within an iframe

I've been tackling the WordPress media library tab recently. Within this iframe, I included a link with target="_blank". My expectation was for this link to open in a new browser tab or window, but instead, it opens within the iframe tab. Here's ...

What could be causing my Time Picker MUI to malfunction in my React project?

I am currently working on a React and Rails project where I am attempting to style my React components. However, I have encountered an issue with the MUI component Time and Date picker. Whenever I try to implement the time picker, I get a strange error reg ...

Compatibility Issue between Jquery UI CheckBox Button and Click Function in IE8

Situation: After calling addButton() function in jQuery, the checkbox element is successfully turning into a button. However, the click event that should trigger an alert message is not functioning. This issue seems to be specific to IE-8 compatibility. ...

Menu stack with content displayed to the right on larger screens

Given the content div's position (up middle), what method can I use to stack the divs in this manner? Is it possible to accomplish with only CSS or is jQuery necessary to move the div content out on larger screens? ...

Creating a Select component in React without relying on the Material-UI library involves customizing a dropdown

Is there a way to achieve a material UI style Select component without using the material UI library in my project? I'm interested in moving the label to the top left corner when the Select is focused. export default function App({...props}) { retu ...

I am struggling to move my dropdown list in HTML and CSS

I'm having trouble adjusting the position of a dropdown list on my website using CSS. Can someone help me move the dropdown list to the center? HTML Code: <header> <h1 id="my_cycle_head">MY CYCLE</h1> <ul id= ...

Creating a div overlay triggered by the addition of a child tag

Using the Paypal zoid feature, I have a button that opens an iframe in the parent div when clicked. However, the iframe causes the other contents of the website to shift around, making it look messy. I'm wondering if there is a way to make the parent ...

JavaScript encountered a problem when trying to call the inline function

I created a function called controls that adds HTML elements dynamically into the specified div. function controls() { var block = $('#controls'); block.html(''); block.append('<button type="button" onClick="f ...

Duplicating labels with JavaScript

I need assistance with copying HTML to my clipboard. The issue I am encountering is that when I try to copy the button inside the tagHolder, it ends up copying <span style="font-family: Arial; font-size: 13.3333px; text-align: center; background-color: ...