Tips for organizing checkboxes in rows horizontally

My question is related to this issue

I am trying to arrange my checkboxes in 4 columns horizontally

<html lang="en">
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>TEST</title>
      <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22404d4d56515650435262170c110c12">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
      <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abc9c4c4dfd8dfd9cadbeb9e8598859b">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
  </head>
  <body>
     <div class="row">
        <div class="col-md-12">
            <form action='' method="POST" novalidate>
                <div>
                    <div>
                        <label>GROUP 1</label>
                        <div style="display: table;">
                          <div style="width:25%; display: inline-table; margin: 5px 0;">
                            <input type="checkbox" id="0_1" name="0_1">
                        <label for="0_1">CK1_1</label>
                          </div>

<!-- rest of the code omitted for brevity -->
                    
                    </div>
                    </div>
            <br/>
                    <div>
                        <label>GROUP 2</label>
                        <div style="display: table;">
                          <div style="width:25%; display: inline-table; margin: 5px 0;">
                            <input type="checkbox" id="1_1" name="1_1">
                        <label for="1_1">CK2_1</label>
                          </div>

<!-- rest of the code omitted for brevity -->

                        </div>
                    </div>
                </div>       
         </form>
        </div>
     </div>
  </body>
</html>

The challenge I'm facing is with the alignment of checkboxes when there are less than 10, which results in a non-uniform layout as shown above

<html lang="en">
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE-edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>TEST</title>
      <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7d5d8d8c3c4c3c5d6c7f78299849987">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
      <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="54363b3b20272026352414617a677a64">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
  </head>
  <body>
     <div class="row">
        <div class="col-md-12">
            <form action='' method="POST" novalidate>
                <div>
                    <div>
                        <label>GROUP 1</label>
                        <div style="display: table;">
                          <div style="width:25%; display: inline-table; margin: 5px 0;">
                            <input type="checkbox" id="0_1" name="0_1">
                        <label for="0_1">CK1_1</label>
                          </div>

<!-- rest of the code omitted for brevity -->

                        </div>
                    </div>
                    <div>
                        <label>GROUP 2</label>
                        <div style="display: table;">
                          <div style="width:25%; display: inline-table; margin: 5px 0;">
                            <input type="checkbox" id="1_1" name="1_1">
                        <label for="1_1">CK2_1</label>
                          </div>

<!-- rest of the code omitted for brevity -->

                        </div>
                    </div>
                </div>       
                </form>
        </div>
     </div>
  </body>
</html>

Answer №1

One way to organize your content is by using a grid display.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            display: grid;
            grid-template-columns: 25% 25% 25% 25%;
            gap: 10px;
        }

        .content {
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
        <div class="content">
            <input type="checkbox" name="check" id="check">
            <label for="check">Checkbox</label>
        </div>
    </div>
</body>
</html>

If this solution does not meet your needs, please inform me so I can provide further assistance.

Answer №2

To achieve a column layout using Flexbox, set the flex-direction property to column and specify a maximum height for the container (e.g., max-height: 30vh). The items within the container will then arrange themselves in columns, but controlling the exact number of columns and item heights can be challenging.

fieldset {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  max-height: 30vh;
  border: none;
  gap: .5em 0;
  margin: 0;
}

legend {
  font-weight: bold;
}

label {
  width: 25%;
}
<form name="form01">
  <fieldset name="group1">
    <legend>Group 1</legend>
    <label><input type="checkbox" name="check1">Checkbox 1</label>
    <label><input type="checkbox" name="check2">Checkbox 2</label>
    <label><input type="checkbox" name="check3">Checkbox 3</label>
    <label><input type="checkbox" name="check4">Checkbox 4</label>
    <label><input type="checkbox" name="check5">Checkbox 5</label>
    <label><input type="checkbox" name="check6">Checkbox 6</label>
    <label><input type="checkbox" name="check7">Checkbox 7</label>
    <label><input type="checkbox" name="check8">Checkbox 8</label>
    <label><input type="checkbox" name="check9">Checkbox 9</label>
  </fieldset>
  <fieldset name="group2">
    <legend>Group 2</legend>
    <label><input type="checkbox" name="check10">Checkbox 10</label>
    <label><input type="checkbox" name="check11">Checkbox 11</label>
    <label><input type="checkbox" name="check12">Checkbox 12</label>
  </fieldset>
</form>

For a row layout with Flexbox, simply change the flex-direction to row without specifying a height. If each <label> has a width of 25%, you'll get four equally spaced columns.

fieldset {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  border: none;
  gap: .5em 0;
  margin: 0;
}

legend {
  font-weight: bold;
}

label {
  width: 25%;
}
<form name="form01">
  <fieldset name="group1">
    <legend>Group 1</legend>
    <label><input type="checkbox" name="check1">Checkbox 1</label>
    <label><input type="checkbox" name="check2">Checkbox 2</label>
    <label><input type="checkbox" name="check3">Checkbox 3</label>
    <label><input type="checkbox" name="check4">Checkbox 4</label>
    <label><input type="checkbox" name="check5">Checkbox 5</label>
    <label><input type="checkbox" name="check6">Checkbox 6</label>
    <label><input type="checkbox" name="check7">Checkbox 7</label>
    <label><input type="checkbox" name="check8">Checkbox 8</label>
    <label><input type="checkbox" name="check9">Checkbox 9</label>
  </fieldset>
  <fieldset name="group2">
    <legend>Group 2</legend>
    <label><input type="checkbox" name="check10">Checkbox 10</label>
    <label><input type="checkbox" name="check11">Checkbox 11</label>
    <label><input type="checkbox" name="check12">Checkbox 12</label>
  </fieldset>
</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

Issue with retrieving body class in Internet Explorer on Magento platform. The body class is not being recognized in IE, but works fine in Mozilla and Chrome

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->getLang() ?>" lang="<?php echo $this->getLang() ?>" dir="ltr"> <?php echo $this->getChildHtml('head') ?> <bod ...

Tips for utilizing Sass and CSS Modules within create-react-app

I've been using FileName.module.scss to style my react elements like this: // Component styling with SCSS import React from "react"; import Aux from '../../hoc/Aux'; import classes from './Layout.module.scss'; const lay ...

Delving into the World of CSS

I have been working on changing the background image using this JavaScript code, but I am not sure what to reference in the CSS file. Despite reading through everything, my screen still remains blank. $(function() { var body = $('body'); var bac ...

The event.js file is displaying an error at line 141, causing an unhandled 'error' event to be thrown

Trying to execute node 4.2.2 on a Mac OS is causing me some confusion as I keep encountering this error message: events.js:141 throw er; // Unhandled 'error' event ^ Error: spawn /Users/user/Documents/Projects/project-x/node_modules/ ...

Run the setInterval function immediately on the first iteration without any delay

Is there a way to display the value immediately the first time? I want to retrieve the value without delay on the first load and then refresh it in the background. <script> function ajax() { ...

Is it unnecessary to mention both JavaScript and AJAX together?

During a recent conversation I had, someone mentioned that it is inaccurate to state that since Ajax is JavaScript. The scenario was: "How can I perform an action on a webpage without requiring a page refresh?" My response: "Utilize JavaScript along wi ...

"Return to the top" feature that seamlessly integrates with jQuery's pop-up functionality

Currently, I am facing an issue with a jQuery selectmenu list that opens as a popup due to its length. My goal is to add a "back to top" button at the end of the list. While researching online, I came across this tutorial which seems promising, but unfor ...

Guide on utilizing popup box input to amend CSS (background color)

I'm looking for some guidance on JavaScript and CSS. Is there a way to create a popup box where users can input a color (any main primary color recognized by CSS) and then have the background color of the page change accordingly in an external styles ...

The PasswordResetView in Django using Vue.js encounters a 403 Forbidden error when attempting to post with Axios due to an

I'm currently working on building a unique Email Restore page in the frontend using Vue.js. My goal is to send email input data via Axios to /api/v1/accounts/password_reset/, essentially utilizing the original ResetPasswordView as an endpoint instead ...

creating an interactive table with the help of the useState hook

I'm new to JavaScipt and ReactJS, so I have a question that may seem obvious but I can't seem to figure it out. I am attempting to display my array in a table using useState, but currently I can only show the header. Additionally, if anyone know ...

How to extract the value of a key from JSON using JavaScript

Need help with an API call to retrieve a list of subcategories? Here's an example of the JSON format: { "description": "Flower", "name": "Flower", "parent_id": "1" }, { "description": "Moon", "n ...

Running complex operations within a sorting function just once

I am facing the challenge of sorting an array of objects based on multiple date fields, with the added complexity of excluding certain dates depending on the category. In order to optimize performance, I want to minimize the number of times the getUsefulJo ...

Issue with Fancybox Buttons not appearing on parent page upon opening

I previously asked for help on how to open fancybox from an iframe to the parent page. The solution worked perfectly and is still functioning well. Now, I've been requested to add left and right arrow buttons, but it seems that using the code to open ...

Executing Node.js Function from an External File Dynamically

Is it possible to run a Node function from an external file that may be subject to change? main.js function read_external(){ var external = require('./external.js'); var result = external.result(); console.log(result); } setInterva ...

Is it possible to manipulate an image tag's image using only CSS?

Is it possible to hide an image from the src attribute of an <img> tag and instead set a background image on the tag using CSS? One idea could be adjusting the positioning or text-indent of the actual image to move it out of view, and then applying ...

The video being shown in Jupyter Notebook is unable to be played

I'm attempting to insert a video stored on my local drive into Jupyter Notebook. The file goes by the name "openaigym.video.6.7524.video000000.mp4" and is located within a folder named "gym-results". Despite using the code below, I am not seeing any ...

Tips for showing an Object with nested Objects in Vue.js

Looking for guidance on displaying a JSON object in Vue.js with the following structure: { "1": [ "15-16", "16-17", "17-18", "18-19" ], "2": [ & ...

Modify the size of a div based on the status of a checkbox using only HTML and CSS

Here is a small project that I created using only HTML & CSS. The goal is to modify the properties of the <div class="pro"> when the checkbox is checked. When this happens, all other <div class="pro"> elements should be hidden and the <art ...

How can you connect a property to the output of a method in Vue.js when working with TypeScript and vue-property-decorator?

I'm working with a TypeScript single file vue component. I've encountered an issue where the template does not display the values returned by certain component methods. Here's a snippet of the template: <div class="order-items"> ...

Retrieve childNodes of the Select All input using jQuery from the container node with the class name "container"

I am trying to retrieve the text value of all the childNodes within the container of the corresponding input when the Select All checkbox is checked. Currently, my code captures the text inside each input label. However, it only logs the label (e.g. &apos ...