My question stems from this
I would like to align my checkboxes horizontally in 4 columns
<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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_2" name="0_2">
<label for="0_2">CK1_2</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_3</label>
</div>
</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>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="1_2" name="1_2">
<label for="1_2">CK2_2</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="1_3" name="1_3">
<label for="1_3">CK2_3</label>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
The problem is that when I have more than 10 checkboxes, they align correctly with the labels, but for less checkboxes, as you can see in the snippet above, the rendering is not harmonious
<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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_2" name="0_2">
<label for="0_2">CK1_2</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_3</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_1</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_1</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_1</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_1</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_1</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_1</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_1</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="0_3" name="0_3">
<label for="0_3">CK1_1</label>
</div>
</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>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="1_2" name="1_2">
<label for="1_2">CK2_2</label>
</div>
<div style="width:25%; display: inline-table; margin: 5px 0;">
<input type="checkbox" id="1_3" name="1_3">
<label for="1_3">CK2_3</label>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
2
Answers
You can use the grid display.
If this is not what you’re looking for, let me know. I’ll be happy to help.
You can use Flexbox. When the
flex-direction
iscolumn
you can limit the height of the container (heremax-height: 10em
) and the items (<label>
) will start to form columns.You can also arrange items in a row. This is a very flexible and you don’t need to specify a height.