Guide on how to style and arrange multiple checkboxes in both horizontal and vertical rows using CSS

Is it possible to align a collection of checkboxes both vertically and horizontally, even when the labels vary in size?

I found an example that demonstrates vertical alignment. Click here to view.

How can I neatly arrange these checkboxes so that they are uniform in their alignment? Thank you for your help!

Below is my basic HTML/CSS code:

li {
    margin: 5px;
}

input {
    width: 20px;
    background-color: blue;
    position: relative;
    left: 200px;
    vertical-align: middle;
}

.vertical-list {
    width: 200px;
    position: relative;
    left: -20px;
    display: inline-block;
    vertical-align: middle;
}

li{
    list-style:none;
}

.horizontal-list{
    display: inline;
}
<center>    
  <ul>        
      <li>
          <input type="checkbox" >
          <label  class="vertical-list"> label1  label1  label1</label>
          <input type="checkbox"  >
          <label class="horizontal-list" for="myid2">label2</label>
      </li>
      <li>
          <input type="checkbox" >
          <label class="vertical-list" >label2label2label2</label>
          <input type="checkbox">
          <label class="horizontal-list" for="myid2">label2label2</label>

      </li>

      <li>
          <input type="checkbox"  >
          <label class="vertical-list" > label4  label4  label4</label>
          <input type="checkbox"  >
          <label class="horizontal-list" >label2</label>
      </li>
  </ul>
</center>

Answer №1

CSS-Tables

li {
  margin: 5px;
  display: table-row;
}

li * {
  display: table-cell;
  padding: 0.5em;
}

input {
  background-color: blue;
  position: relative;
}

.vertical-list {}

li {
  list-style: none;
}

.horizontal-list {
  display: inline;
}
<ul>
  <li>
    <input type="checkbox">
    <label class="vertical-list"> label1  label1  label1</label>
    <input type="checkbox">
    <label class="horizontal-list" for="myid2">label2</label>
  </li>
  <li>
    <input type="checkbox">
    <label class="vertical-list">label2label2label2</label>
    <input type="checkbox">
    <label class="horizontal-list" for="myid2">label2label2</label>

  </li>

  <li>
    <input type="checkbox">
    <label class="vertical-list"> label4  label4  label4</label>
    <input type="checkbox">
    <label class="horizontal-list">label2</label>
  </li>
</ul>

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

Tips for styling a button upon being clicked

Is there a CSS technique to make a button change color when clicked with the mouse? I am aware of using ':hover' for changing color, but I need something specific to a left mouse click. I want the same functionality as a standard button but with ...

Is there a way to apply -webkit-line-clamp to this JavaScript content using CSS?

i have a random-posts script for my blogger website <div class="noop-random-posts"><script type="text/javascript"> var randarray = new Array(); var l=0; var flag; var numofpost=10; function nooprandomposts(json){ var total = ...

What is the process for registering a click using a class in jQuery and retrieving the ID of the clicked element?

Currently, I am working on developing a webpage where I need to use jQuery to register a click using the items class and then extract the ID of that particular object. For example: HTML: <div class="exampleclass" id="exampleid1"></div> <d ...

Utilizing jQuery taggd to add annotations to images

I am currently utilizing the taggd Plugin to develop an application that creates a tag whenever any part of the body is clicked. I have successfully implemented most aspects of it, but I am facing an issue with the coordinates. The tags are sometimes creat ...

Establish limits for draggable item

I am working on a project where I have a draggable element generated dynamically using jQuery. The div containing the element also has a background image. How can I ensure that the draggable element never goes outside of the boundaries of the div? If you ...

A guide to showcasing JSON data on a webpage using JavaScript

I am currently working on a SOAP WSDL invocation application in MobileFirst. The response I receive from the SOAP WSDL is in JSON format and is stored in the result variable. When trying to access the length of the response using result.length, I encounter ...

Preventing Page Refresh when Button is Clicked in Angular

How can I prevent page reload when a button is clicked in Angular? I'm developing a quiz application in Angular where I fetch random questions and options from an API. When users select an option, the next question should appear without reloading the ...

Creating a binary tree in vanilla JavaScript and styling it with HTML and CSS

I'm facing a challenge with my homework. I am required to convert my JavaScript binary tree into HTML and CSS, strictly using vanilla JavaScript without any HTML code. I have the tree structure and a recursive function that adds all the tree elements ...

Steps for designing a complete background picture

Seeking to enhance my CSS skills, I've been faced with a challenging task recently. The objective is to create a full-screen background image with centered text overlay. However, the image appears larger than the screen itself. This is my current se ...

Uploading Multiple Files Using HTML5 and AJAX

I recently stumbled upon this simple plain JavaScript AJAX upload code (apparently jQuery's $.post doesn't work properly with HTML5 for some reason), /* If you want to upload only a file along with arbitrary data that is not in the fo ...

Absolute positioned TD creates a gap upon being displayed

Hey there, I am facing an issue with a table I have. In each row (TR), the last TD element has a class called 'abs'. The style for this class is specified in the provided code snippet. Initially, this element is hidden. However, when you hover o ...

Guidance on Configuring Django Static Files

For setting up my Django static files, I added the following code to settings.py: STATIC_URL = '/static/' STATIC_DIRS = [ os.path.join(BASE_DIR, 'static') ] STATIC_ROOT = os.path.join(BASE_DIR, 'assets') To implement this ...

"Returning undefined: The outcome of using jQuery's .html() method on

I am having an issue with the JavaScript function I have created to load content from another URL into the current document. For some reason, both contentHtml and menuHtml variables are showing as undefined. Can someone please help me identify where I we ...

Trying to arrange HTML elements in a diagonal configuration while ensuring they are all uniform in size

My goal is to create an attractive splash page with diagonal, circular links that are all the same size. The <p>'s will function as the links, but I'm struggling to figure out how to make them all diagonal and uniform in size. I've ex ...

Obtaining data from console.log and showcasing it within a div element

Currently, I am facing a challenge where I want to retrieve the title, plot, and poster using the themoviedb API. However, I am lost on how to begin coding this. Whenever I perform a search, the information is displayed in the console log of the browser. ...

Is there a way to modify HTML using a C# program in a web browser?

I’m considering the possibility of editing the HTML document text through the web browser. I am currently working on an email client that utilizes an HTML template for its design. Everything seems to be in order, but now I need to customize the template ...

Discover the art of customizing child elements using vanilla extract!

I recently started using vanilla extract to add styles to a NextJS application. Is there a way to style child elements within the parent element without having to create another class? My React component has a structure like this: <ul className={style ...

Custom Jquery function is failing to position divs correctly within ajax-loaded content

I recently coded a custom function that efficiently positions absolute divs within a container by calculating top positions and heights: $.fn.gridB = function() { var o = $(this); //UPDATED from var o = $(this[0]); o.each(function() { var ...

The navigation bar struggles to display list elements without proper line breaks

Recently, I've been immersed in a web development project for a company. For the navigation bar, I opted to use the FlexNav JQuery plugin. While referencing the example on , I encountered an issue when attempting to add more than 5 list elements; they ...

What is the best way to integrate a PHP page with its own CSS and JavaScript into a Wordpress page?

I'm facing a challenge with integrating a dynamic PHP table into my WordPress page. Despite working perfectly when opened locally, the CSS and JavaScript used to create the table are not functioning properly when included in a WordPress page via short ...