What is the best way to stack checkbox and label pairs vertically and expand to fill the available width?

I am working on a project that utilizes Bootstrap 4. Within one of the forms, I have checkboxes representing each state in the U.S. along with a few territories. Currently, these checkboxes are displayed inline, which is good for utilizing available width on larger screens. However, all the states appear on the same line as if they were just words like this:

☐ Alabama  ☐ Alaska  ☐ Arizona  ☐ Arkansas  ☐ California
☐ Colorado  ☐ Connecticut etc etc

My goal is to display each subsequent checkbox on a new line but wrap them into columns to make use of the available space on larger screens. For example, on a phone screen, there might be only one state per line, while on a large screen, multiple columns would be visible like this:

☐Alabama   ☐Maryland  ☐Rhode Island
☐Alaska    ☐Massachusetts ☐South Carolina
☐Arizona   ☐Michigan      ☐South Dakota
☐Arkansas  ☐Minnesota     ☐Tennessee
☐California ☐Mississippi  ☐Texas
☐Colorado  ☐Missouri      ☐Utah
☐Connecticut ☐Montana     ☐Vermont
☐Delaware ☐Nebraska       ☐Virginia
☐Florida ☐Nevada          ☐Washington
 

Here's an excerpt from my current HTML markup:

<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <div class="card">
                <div class="card-body">
                    <h3 class="card-title">State:</h3>
                    <div class="card-text">
                        <div class="form-check form-check-inline">
                            <label>
                                <input type="checkbox" name="state_nopref" value="state_nopref" onclick="uncheck_all_state(this)" checked>No Preference
                            </label>
                        </div><!-- end checkbox -->
                        <div class="form-check form-check-inline">
                            <label>
                                <input name="state[]" type="checkbox" value="AL" onclick="uncheck_state_nopref(this)" >Alabama
                            </label>
                        </div>

<!-- Continued code blocks...  -->
                        
                    </div><!-- end card-text -->
                </div>
            </div><!-- end card -->
        </div><!-- end column -->
    </div><!-- end row -->
</div>

Is there an easy way to modify my layout so it can take advantage of wider screens by displaying content in additional columns?

Answer №1

.form-check.form-check-inline {
    width: 33%;
    display: block;
    float: left;
}
<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <div class="card">
                <div class="card-body">
                    <h3 class="card-title">State:</h3>
                    <div class="card-text">
                        <div class="form-check form-check-inline">
                            <label>
                                <input type="checkbox" name="state_nopref" value="state_nopref" onclick="uncheck_all_state(this)" checked>No Preference
                            </label>
                        </div><!-- end checkbox -->
                        <div class="form-check form-check-inline">
                            <label>
                                <input name="state[]" type="checkbox" value="AL" onclick="uncheck_state_nopref(this)" >Alabama
                            </label>
                        </div>
                        <div class="form-check form-check-inline">
                            <label>
                                <input name="state[]" type="checkbox" value="AK" onclick="uncheck_state_nopref(this)" >Alaska
                            </label>
                        </div>

<!-- ETC -->
                        <div class="form-check form-check-inline">
                            <label>
                                <input name="state[]" type="checkbox" value="VI" onclick="uncheck_state_nopref(this)" >U.S. Virgin Islands
                            </label>
                        </div>
                        <div class="form-check form-check-inline">
                            <label>
                                <input name="state[]" type="checkbox" value="DC" onclick="uncheck_state_nopref(this)" >Washington, D.C.
                            </label>
                        </div>
                    </div><!-- end card-text -->
                </div>
            </div><!-- end card -->
        </div><!-- end column -->
    </div><!-- end row -->
</div>

This is another example of how this can be implemented.

Answer №2

body {
    padding: 20px;
}

.w-10 {
    width: 100%;
}

.myGroup {
    width: 100%;
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    height: 300px !important;
}
<loadcss/>
<div class="container">
    <div class="row">
        <div class="col-xs-12 w-10">
            <div class="card">
                <div class="card-body">
                    <h3 class="card-title">State:</h3>
                    <div class="card-text myGroup">
                        <div class="form-check form-check-inline">
                            <label>
                                <input type="checkbox" name="state_nopref" value="state_nopref" onclick="uncheck_all_state(this)" checked>No Preference
                            </label>
                        </div><!-- end checkbox -->
                        <!-- More States and Options Here -->
                    </div><!-- end card-text -->
                </div>
            </div><!-- end card -->
        </div><!-- end column -->
    </div><!-- end row -->
</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

To determine the class of an element and reveal its parent if the class is present

Typically, the parent element remains hidden by default. However, upon clicking a specific element, its child might gain a class called "selected". How can I check for this class and then display the entire list if it is present? <ul style="display: ...

The panel header is clickable and overlaps with the header buttons

My panel component includes a header with a title and buttons positioned in the right corner. Currently, the downward arrow (chevron) is used to toggle the expansion/minimization of the panel's contents. When I attempt to make the header clickable to ...

"Access denied: Resteasy and ajax do not support this method (error

Encountering a login problem, although registration is functioning smoothly. Tech stack - HTML/AJAX/JQuery->Resteasy (Wildfly 10.1.0 Final) The URL - https://www.test.com/login.html - (actual URL name altered) Upon submitting the form with method type ...

There seems to be an issue with the behavior of the click-to-toggle menu implemented with jQuery

I can't seem to figure out why my pop-up menu in jQuery is acting so strange and unpredictable. The menu is supposed to appear on a toolbar located at the bottom of the window when clicked (not hovered). It should then disappear again when: a) a menu ...

Embedding a styled list item within a list

How can I customize the style of list items within a list? HTML code <span class=bbb> <ul> <li>Preparing archive inquiries about study periods or work experience at LLU: <ul> <li>Within seven days - 5 ...

Forming a header area by utilizing a 960 Grid container

I'm in the process of designing a website utilizing the 960 Grid system. The header consists of three parts: the logo, the navigation, and the login form. I've implemented media queries to center both the logo and the login section when the pag ...

Eliminate unnecessary spaces in the input field located below the provided text

In the html code, I have an input field that looks like this: https://i.sstatic.net/DsD4y.png This is the CSS for the input fields: .form-control-1 { display: block; margin: 0 auto; width: 30%; height: 50px; padding: 6px 12px; font-size: 18p ...

Is there an issue with integrating Bootstrap with Angular 6?

npm install bootstrap Update the angular.json file: "styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", "styles.scss" ] Include the Bootstrap CSS directly in your src/style.css: @import '~bootstrap/dist/css/bootstrap.min.css'; A ...

"A problem with PrimeNG where the model value does not get updated for the p-auto

<p-autoComplete [style]="{'width':'100%'}" name="searchSuggestions" [(ngModel)]="suggestion" (completeMethod)="searchSuggestions($event)" [suggestions]="searchSuggestionsResult" field="field"></p-autoComplete> Utilizing t ...

Placing an Image in Next.js

I've been trying to position my image all the way to the right of the page using CSS properties like position: absolute and right: 0. I've also attempted setting the parent element's position to relative and the image's position to abso ...

Managing button spacing with Bootstrap 4 in an Angular 2 CLI application

I find it puzzling why the alignment between Bootstrap buttons within the Angular 2 CLI project is not working as expected. To address this issue, I followed the instructions to create a new Angular 2 CLI app outlined here: https://angular.io/guide/quicks ...

Disrupting Alignment Due to Input Tag

Hey there, I'm having an issue with my header design. I have a horizontal list of links in my header, and next to them, I wanted to add a search bar. Unfortunately, when I added the search bar next to the links, they all ended up appearing behind my b ...

Achieving Perfect Alignment in HTML Tables

I have created a simple HTML table and I am trying to center it vertically in the middle of the page based on the user's device height. So far, I have tried adding a div with 100% height and width, positioning the table at 50% from the top and left, b ...

When toggling visibility with JS, the Bootstrap Grid Row may not center-align as expected

Sorry for the odd spacing issue that might occur. I currently have a basic Bootstrap Grid setup as follows: <div class="row justify-content-center" align="center" id="details"> <div class="col-sm-2"> ...

Uncovering the Secrets of Accessing Tag Attributes in ReactJS

I have developed a small app using reactjs. I have set an attribute on the button tag and passed a value to it. Now, when I click on the button, I want to retrieve the attribute value. How can I achieve this with react js? Here is a sample code snippet: ...

"Is it possible to include a button for horizontal scrolling in the table

I have a bootstrap table with overflowing set to auto within its container, along with a locked first column. While a horizontal scroll bar allows for viewing additional data, I am looking to incorporate buttons for navigation as well. Users should have th ...

What is the best way to vertically align Angular Material tabs?

In my current project, I decided to use angular material and encountered a problem with the angular material tab. I wanted to arrange the tabs vertically, but it doesn't seem to be working properly. The alignment seems off and it's not functionin ...

Failure to receive results from $.getJSON request

I am currently working on developing a web page that allows users to search and retrieve Wikipedia pages based on their search results. Below is the relevant HTML code for the search feature: <form class='search-form' onsubmit='return f ...

Eliminating data containers from Google Maps

How can I hide the white info boxes (1 and 2) on my Google Map? I attempted using the following CSS code below, but it doesn't seem to be effective: .place-card { display:none; } Is there a different approach I should take? ...

Tips for customizing a link element that routes to a React component

App.js: import './App.css'; import logo from './images/logo.png'; import Home from './components/Home'; import About from './components/About'; import Calculators from './components/Calculators'; import Cla ...