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

Show only the present y-coordinate in the ToolTip of a chart.js

I am currently working on developing a chart using Chart.js, and my goal is to only show the y-axis value in the tooltip. The current display shows: date name_of_line: measurement example: Jan 1, 2020, 8:00:00 AM 5th Percentile: 100 oz. However, I would ...

Tips for obtaining the entire date and time on one continuous line without any breaks or separation

Is there a way to retrieve the current date and time in the format of years, months, days, hours, minutes, seconds, and milliseconds like this? 201802281007475001 Currently, I am getting something like: 2018418112252159 This is my code so far: var dat ...

What is the best way to eliminate the border around an image when including a hyperlink?

There seems to be a rectangle showing up outside the link that I would like to remove. How can I get rid of it? No Image <a href="~/Web Pages/Home.aspx" runat="server"> <img src="<%= ResolveUrl("~/Images/TestToday.png") %>" alt="No im ...

Reduce the number of divs on a webpage while incorporating animated transitions

I've been attempting to incorporate an animation on the width property for my div .panels. I've tried using transition-property in CSS and also with .animate() in jQuery, but unfortunately, it doesn't seem to be working. I also noticed that ...

Quick access to HTML URLs without the need for page names

I'm having trouble finding the right search terms. Is there a way to simplify the URL links on my website? For instance: Current URL structure: websitename.com/about_us_what_we_do.php Desired URL structure: websitename.com/what-we-do Another e ...

Holding off on routing the pathName through the router until the next-page-transitions have been completed

In my current setup, I have two distinct elements at play. Using the code snippet below: className={router.pathname.startsWith("/pagename") ? "menu-active" : ""} I am able to apply the menu-active class to the pagename naviga ...

Layering one canvas on top of another

Here is the code I am working with. With a JavaScript library, I can draw on the first canvas and then merge it onto the second canvas. My question is, how can I make the first canvas stay in place or float, regardless of where I scroll on the second canv ...

Finding the perfect pairing: How to align counters with objects?

public counter = 0; x0: any; x1: any; x2: any; x3: any; x4: any; next(){ this.counter += 1; this.storage.set("Count", this.counter); console.log(this.counter); this.logic(); } logic(){ //automatic counter here var xNum = JSON.parse(JSON.stri ...

Could it be a mistake causing Echo to fail in fetching information from advanced custom fields?

When setting up in-content advertising for my blog posts on WordPress, I wanted the ads to point towards other sections of my site. Everything was working fine with the shortcode [in-content] pulling the most recent post from the 'advertising' cu ...

arrange fixed-top elements in a stacked manner using Bootstrap 4

I am looking to inform users about enabling JavaScript for optimal use of the website, and I want this message to appear above the navigation bar using Bootstrap. However, currently they are stacking on top of one another instead of displaying in the desir ...

Using jQuery to calculate mathematical operations in a form

I've been working on creating a form that calculates the total cost based on selected options. So far, I've managed to get it working for three options, but I'm stuck on how to calculate the subtotal by adding the variables "vpageprice" and ...

Enhancing jQuery functionality: Ensuring newly added elements are aware of existing functions

I need assistance with my HTML table. Each row (tr) contains a value, for example 200$, and a delete button. How can I ensure that each new row added automatically knows the recent functions without having to call them every time? $(function () { $(&ap ...

What could be causing my grid-area properties to not take effect?

I'm attempting to create a basic structure with a header in the top row, menu and content in the middle row, and footer at the bottom. Here is what I have so far: body { color: white; } .container { background: cyan; display: grid; ...

Images on NextJS are failing to resize properly in Next 13

I have been facing challenges with optimizing image sizes for different screen resolutions using the NextJS Image component. Currently, all images seem to be loading at their largest size on various screens. Below is a screenshot showing the resolution t ...

Dropdown menu remains unable to open

One of the dropdown menus on my website is not retracting back properly, but only on the contact page where a Google map is displayed. The issue looks like this: I've tried tweaking the CSS code without success. What could I be missing? Should I prov ...

What is the best way to insert an HTML data attribute string into a SCSS mixin using attr()?

I'm currently working on setting up a color scheme in SCSS that allows me to use the following HTML structure: <div class="swatch" data-bg="green">...</div> In my SCSS file, I have defined a mixin like this: @function color($key: ' ...

Finding a Div ID with Python and Selenium

Currently, I am facing an issue where I have to interact with an image of a dropdown arrow in order to select an item that will automatically fill the input box. Both the dropdown arrow and input box are components within the dev id:decision. I require ass ...

Is it possible to represent a recursive variable using CSS?

When it comes to the html structure: <body> <div> <div> <div> ... </div> </div> </div> </body> Is there a method to create recursive variables that utilize their parent's value: body ...

The combination of eq, parent, and index appears to be ineffective

Okay, so I have this table snippet: <tr> <td> <input type="text" name="value[]" class="value" /> </td> <td> <input type="text" name="quantity[]" class="quantity" /> </td> </tr> Here& ...

Are web components capable of managing changes in CSS (maybe through cssChangedCallback)?

Is there a way to control the application of CSS to a web component similar to using attributes through attributeChangedCallback. I am currently developing a couple of web components that could benefit from being styled with CSS classes. However, I requir ...