Center Checkboxes in Bootstrap 4 Form Input

I am working with a Bootstrap Form that contains checkboxes. One specific scenario involves a large number of options to choose from. Currently, these options are displayed in a single long list, which can make it difficult for users to read and select their preferences easily.

Here is how the form currently appears:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />

<body>

  <main role="main" class="container">

    <div>

      <form method="get" action="projects.php" role="form">
        <input type="hidden" name="action" value="projectsFind">

        <div class="form-group row">
          <label for="sectionNumber" class="col-sm-2 col-form-label">Section Number</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="sectionNumber" name="sectionNumber" placeholder="Section Number">
          </div>
        </div>
        <div class="form-group row">
          <div class="col-sm-2">Selections</div>
          <div class="col-sm-10">
            <div class="form-check form-check-inline">
              <input class="form-check-input" type="checkbox" name="footTypes[]" id="footTypes" value="Hot Food">
              <label class="form-check-label" for="gridCheck1">
            Hot Food            </label>
            </div>

       [...]
 
             </div>
           </div>
         </div>
      </form>

As mentioned above, the "Selections" checkboxes are currently presented in a vertical list format. Is there a way to arrange them into aligned columns for improved readability?

Answer №1

To achieve a column-like layout, you can set a fixed width to the .form-check-inline element.

The ideal width value should be slightly larger than the widest word in the content.

.form-check-inline {
  width: 150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />

<body>

  <main role="main" class="container">

    <div>

      <form method="get" action="projects.php" role="form">
        <input type="hidden" name="action" value="projectsFind">

        <div class="form-group row">
          <label for="sectionNumber" class="col-sm-2 col-form-label">Section Number</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="sectionNumber" name="sectionNumber" placeholder="Section Number">
          </div>
        </div>>
        <div class="form-group row">
          <div class="col-sm-2">Selections</div>
          <div class="col-sm-10">
            // Checkbox and label elements here
          </div>
        </div>

      </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

Is there a way to apply toggling and styles to only the card that was clicked in Vue.js?

DisplayBooks.vue consists of a single page responsible for showcasing books fetched from a backend API. Each card on the UI features two buttons - ADD TO BAG and ADDED TO BAG. When a user clicks on the ADD TO BAG button of a specific card, it should toggle ...

The issue of centering not working with a specified width under the CSS rule `margin: 0 auto

As I was creating sections for a mini "about me" page, I encountered an issue with setting margins in all divs (this one is the most important). Despite specifying widths and heights, I couldn't get margin: 0 auto; to work. body { background: #aaa; ...

Executing a JQuery function when the webpage is loaded

I am attempting to execute some jQuery code when the page loads, but it doesn't seem to be working properly. The goal is to check if a link has already been saved and, if so, display a table with that information. Is this achievable? It should apply t ...

What is the best way to determine which id has been clicked using jQuery?

Can someone help me figure out how to determine which button has been clicked using jQuery? Here is the HTML code I am working with: <div class="row"> <div class="col-md-6"> <div class="well " id="option_a"> </div& ...

How can I achieve an endless scrolling text effect within a square on a webpage using CSS?

My header currently displays a horizontal infinite scroll of text, but I am looking to achieve a square pattern wrapping around the page. Can you provide guidance on how this can be accomplished? Below is the code snippet I am working with: .marquee { ...

The ability to submit a conversation chat is currently

I encountered an issue when attempting to submit a chat, and I received the error message 'handlebar is not define'. I followed the code tutorial provided in this link: https://codepen.io/drehimself/pen/KdXwxR This is the screenshot of the error ...

Assign a background image to a button using an element that is already present on the page

Is there a way to set the background-image of a button without using an image URL? I am hoping to use an element already in the DOM as the background-image to avoid fetching it again when the button is clicked. For example, caching a loading gif within the ...

Trouble with highlighting the chosen menu item

I have been attempting to implement this code snippet from JSFiddle onto my website. Although I directly copied the HTML, used the CSS inline, and placed the Javascript in an external file, the functionality does not seem to be working properly. Feel free ...

What strategies can I use to dynamically update the .active class in jquery so it doesn't only target the initial one?

Utilizing bootstrap for tab-fade functionality has been successful so far. However, I am facing an issue when trying to select multiple active classes instead of just one. My current JQuery code only changes the text in the first element with the "active" ...

Styling the Menu Item with CSS

A graphic designer provided me with these elements to incorporate into the HTML design. Everything was going smoothly until I encountered the faint, uneven borders on the LI tags, especially when dealing with a list containing only five items. If anyone ...

Creating a responsive slider in CSS that matches this specific design

Looking to create a responsive sidebar based on this specific design https://i.sstatic.net/YKy6Z.png https://i.sstatic.net/qwTuJ.png Link to design on Figma Key focus is implementing the "< 4/12 >" functionality and ensuring it is respo ...

Modifying a CSS property with jQuery

If I have the following HTML, how can I dynamically adjust the width of all "thinger" divs? ... <div class="thinger">...</div> <div class="thinger">...</div> <div class="thinger">...</div> ... One way to do this is usi ...

Remove the ability to select from the dropped list item

Here is the HTML and Javascript code I used to enable drag and drop functionality for list items from one div to another: HTML: <div class="listArea"> <h4> Drag and Drop list in Green Area: </h4> <ul class="unstyle"> & ...

Trouble with the display none function

I am trying to achieve the following: If my shopping cart is empty, I want another div to display over the top of it. Instead of seeing the cart, users should see a message saying 'your cart is empty'. Currently, I have set the other div to dis ...

Is there a way to trigger the opening of a new file or page when a CSS animation comes to an end?

Is there a way to delay the loading of a function or page until after an animation has finished running in JavaScript, HTML, and CSS only? For instance, I'd like to run an animation first and then have a different website or content load afterwards fo ...

PHP: The constant ENT_HTML5 is not defined and has been assumed as 'ENT_HTML5'

I encountered the following error message: Use of undefined constant ENT_HTML5 - assumed 'ENT_HTML5' in /blahblahpath/application/models/Post.php on line 12 This error occurred in a basic model that handles some POST input in a forum applicatio ...

Issue with Angular Script not functioning upon reopening the page

I recently completed my first website, which can be found at Despite my efforts, I've encountered an issue that has me stumped. When you navigate to the certificate page, you should be able to input your name onto the certificate. However, if you sw ...

Why does selecting by data- attribute in jQuery fail for certain elements?

Consider this sample HTML code: <div id="details" data-type="motion"> <p>Lorem ipsum</p> </div> <img src="http://placehold.it/50x50" data-type="motion" /> <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr ...

top approach for transforming form data into JSON format

My task is to convert data from JSON format in order to send it to a webAPI. I have a popup form called from page1: <form id="popupForm" method="post" class="form-horizontal" action=""> <div class="form-group"> <label class="col-sm-4 c ...

Retrieving the checkbox value from a dropdown selection

I'm stuck and feeling lost here - I must be missing something obvious. Any help will be greatly appreciated! (I am a beginner in html and javascript) I have created a dropdown menu with an unordered list of items populated from JSON data. Here is the ...