How can I align my checkboxes horizontally with a set number using bootstrap?

Is there a way to align my checkboxes next to each other in columns of 5, rather than being stacked vertically?

The current layout looks like this:

Checkboxes vertical

I would like the checkboxes to appear like this:

[x] label [x] label [x] label [x] label [x] label
[x] label [x] label [x] label [x] label [x] label

This is what my code shows:

      <div class="row form-group">
        <div class="col col-md-3">
          <a data-toggle="collapse" href="#collapsKlassen">
            <h5 class="mb-0">
              Klassen <i class="fas fa-angle-down rotate-icon"></i>
            </h5>
          </a>
        </div>
        <div class="col-12 col-md-9 collapse" id="collapsKlassen" style="margin-right: 5px;">
          <div class="form-check  "  >
            <div >
              @Html.EditorFor(x => x.CheckBoxKlassen)
            </div>
          </div>
        </div>
      </div>

I have attempted using formcheck inline among other methods, but nothing has provided the desired result. EDIT: Rendered code:

<input id="CheckBoxKlassen_0__ID" name="CheckBoxKlassen[0].ID" type="hidden" value="61" />
<input id="CheckBoxKlassen_0__Display" name="CheckBoxKlassen[0].Display" type="hidden" value="BIO 1Aa" />
<input data-val="true" data-val-required="The WasChecked field is required." id="CheckBoxKlassen_0__WasChecked" name="CheckBoxKlassen[0].WasChecked" type="hidden" value="False" />
<input data-val="true" data-val-required="The IsChecked field is required." id="CheckBoxKlassen_0__IsChecked" name="CheckBoxKlassen[0].IsChecked" type="checkbox" value="true" />
<label for="CheckBoxKlassen_0__IsChecked">BIO 1Aa</label>
<br /><input id="CheckBoxKlassen_1__ID" name="CheckBoxKlassen[1].ID" type="hidden" value="64" />
<input id="CheckBoxKlassen_1__Display" name="CheckBoxKlassen[1].Display" type="hidden" value="BIO 1Ab" />
<input data-val="true" data-val-required="The WasChecked field is required." id="CheckBoxKlassen_1__WasChecked" name="CheckBoxKlassen[1].WasChecked" type="hidden" value="False" />
<input data-val="true" data-val-required="The IsChecked field is required." id="CheckBoxKlassen_1__IsChecked" name="CheckBoxKlassen[1].IsChecked" type="checkbox" value="true" />
<label for="CheckBoxKlassen_1__IsChecked">BIO 1Ab</label>
...

Answer №1

Here is a practical example that you can use as reference and customize according to your requirements.

Example

 public class CustomCheckBox
{
    public int Id { get; set; }
    public string Name { get; set; }

    public bool IsSelected { get; set; }
}

public class CheckBoxListViewModel
{
    public IList<CustomCheckBox> CheckBoxItems { get; set; }
}

CustomCheckBoxList.cshtml, utilize CSS for horizontal alignment of checkboxes and adjust the width percentage of <li> tag to control number of checkboxes per line.

@model YourNamespace.Models.CheckBoxListViewModel
<style>
  .checkbox-grid li {
    display: block;
    float: left;
    width: 20%;
   }
</style>

<div class="row form-group">
  <div class="col col-md-3">
    <a data-toggle="collapse" href="#collapsClasses">
        <h5 class="mb-0">
            Classes <i class="fas fa-angle-down rotate-icon"></i>
        </h5>
    </a>
  </div>
  <div class="col-12 col-md-9 collapse" id="collapsClasses" style="margin-right: 5px;">
    <div class="form-check">
        <ul class="checkbox-grid">
            @for (var i = 0; i < Model.CheckBoxItems.Count(); i++)
            {
            <li>
                <input type="checkbox" asp-for="CheckBoxItems[i].IsSelected" value="@Model.CheckBoxItems[i].Id" />
                <label>@Model.CheckBoxItems[i].Name</label>
            </li>

            }
        </ul>
    </div>
  </div>
</div>

Backend Code, retrieve checkbox list data in the controller

public IActionResult CheckBoxList()
    {
        CheckBoxListViewModel model = new CheckBoxListViewModel()
        {
            CheckBoxItems = _context.CustomCheckBox.ToList()
        };
        return View(model);
    }

Answer №2

In my experience, I find that utilizing flexbox is the ideal solution for scenarios like this one. You can refer to an easy-to-understand guide here which provides a quick explanation. When paired with the CSS calc function, it proves to be one of the most efficient ways for styling.

If you want to implement this in your CSS code, you can use the following:

div.form-check { display: flex; flex-wrap: wrap; width: 100%; }

div.form-check div { flex-basis: 20%; }

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

When working with TextareaAutosize component in MUI, an issue surfaces where you need to click on the textarea again after entering each character

One issue that arises when using TextareaAutosize from MUI is the need to click on the textarea again after entering each character. This problem specifically occurs when utilizing StyledTextarea = styled(TextareaAutosize) The initial code snippet accompl ...

Parsing a Table in Python with HTMLParser

I am trying to convert an HTML table into a 2D array (rows and columns) using only the HTMLParser library in Python, without relying on BeautifulSoup or other non-standard libraries. This task is part of a personal project that I am working on for fun :) ...

Set the color of the text in the Material UI pagination component to a subtle shade

I would like to customize the color of the text in Material UI's pagination component. Specifically, I want the action button to be white and the text portion to be grey, similar to the left action arrow in the image below. Is there a way for me to ac ...

What is the best way to adjust the height of a dropdown box in an angular application?

Is there a way to change the height of the scroll view? It seems too long for my liking, any advice? I attempted to adjust it using css but unfortunately, it didn't work out. The scroll view appears excessively lengthy as shown in the image below. h ...

Encountering an Uncaught TypeError while trying to read properties of undefined (specifically 'remove') during a Change event is causing an issue for me

Looking to update the icons by changing the class from .fa-plus to .fa-minus for this specific menu section <div class="accordion-menu"> <h2 class="accordion-header" id="subseccOne"> ...

The header and logo are missing from my webpage

I am facing an issue where my navigation bar is not displaying at the top of my screen, even though there are no syntax errors. Is there something in the syntax that needs to be adjusted? Or did I overlook something in my default template file? The webpage ...

Tips for displaying a multi-select dropdown in the Creative Tim Angular Material Pro filter panel

I am in need of assistance with modifying the standard Creative Tim Angular Pro Material template due to my limited CSS/SCSS skills. Could someone provide examples of the necessary changes, whether it involves altering the HTML or multiple CSS files withi ...

retrieving data from a database to populate selection options using PHP

While working on our thesis, I encountered a challenge where I needed to transfer certain values from my SQL database to another part of the project. Below is a snippet of the sample code I was using: <tr> <td id="t" align="center"> ...

Internet Explorer fails to accurately determine the height of a textarea based on the line-height

I need to adjust the display of a textarea to show 4 rows with a line height set to 200%. Take a look at the HTML code: <textarea rows="4" cols="50"> 1 2 3 4</textarea> and the accompanying CSS: textarea { line-height: 2; } It appears ...

Differences Between Mobile and Desktop Browser Media Queries

As I work on creating a responsive website, I have come across various examples of sites that adapt well to both desktop and mobile browsers. Currently, my stylesheet setup includes different media queries for various screen sizes. However, I've notic ...

How to insert an HTML element in between multiple list items using jQuery

I have a variety of div elements that I want to group together using jQuery's append method: Here are the initial div elements: <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div& ...

Using JavaScript regular expressions to validate currency without relying on jQuery

Looking for a solution to create a money mask for an input field without using jquery in my application. Is it possible to achieve this using regular expressions with 'on key up' events, for example? Below is the code snippet: <tr> & ...

Update the parent element's class style within a targeted div media query

I am facing a challenge where I want the width of my .container element to be 100% when the screen size reaches 900px. The issue is that I have used .container on multiple pages and only wish to change its appearance within the #sub-top div. The terminolo ...

What is the best way to enclose text within a border on a button?

I am trying to create a button with a colored border around its text. Here is the code for my button: <input id="btnexit" class="exitbutton" type="button" value="Exit" name="btnExit"></input> Although I have already added CSS styles for the ...

Layers of CSS images

Is there a way to create multiple layers for images on a webpage so they can move independently without affecting other elements? I attempted to use z-index, which did work, but it caused the page to increase in height. Additionally, when using jQuery.posi ...

Leveraging Bootstrap column classes with diverse div heights

Currently, I am working on a layout design using Bootstrap 3 that involves eight text divs. These divs have almost the same height, but some are slightly taller depending on the screen width. My goal is to arrange them into three columns on desktop and th ...

Selecting multiple items from a grid using the Ionic framework

I am looking to create a grid of category images in Ionic framework, where users can select multiple categories and send their values to the controller. However, I'm unsure about how to make this happen with Ionic framework. Below is the view I curre ...

Does anyone know how to position div 1 underneath div 2 using CSS?

<div class="1"> <p> THIS IS DIV 1> </p> <div class="2"> <p> THIS IS DIV 2> </p> I'm utilizing a shortcode that seems to consistently appear at the top of all elements. Is there a way to change this behavior? ...

What is the process for implementing a banner across the entire website?

I understand that this question resembles one found at this link, but I did not find a clear solution there. Currently, I am utilizing <?php include "banner.html"; ?>. However, in order for this method to work, I must change the extension of ALL page ...

I am looking to enhance my email subscription form by incorporating a URL into the subscription button, all while ensuring that the email subscription function in the HTML remains fully operational

Is it possible to maintain the functionality of an email subscription form while adding a URL to the subscription button in HTML? I need help modifying the code of the button. ...