Is there a way to properly line up checkboxes within a form while utilizing table display using CSS?

I am trying to format my check boxes within a form using table display and CSS styling. Here is the code I have implemented:

form {
    display: table;
}
div.tableRow {
    display: table-row;
}
Div.tableRow p {
    display: table-cell;
    vertical-align: top;
    padding: 3px;
}
div.tableRow p:first-child {
    text-align: left;
}
h1 {
    font-weight: bold;
    font-size: 16px;
}
<form action="" method="POST>
        <div class="tableRow">
        <p>Address: <input type="text" name="Address"></p><br></div>
        <div class="tableRow"><p>
        Phone: <input type="tel" name="Phone">
        </p></div>
        <div class="tableRow">
        <p>
        <select name="size">
                <option value="Medium">Medium</option>
                <option value="Large">Large</option>
                <option value="X-Large">X-Large</option>
        </select></p></div>
        <div class="tableRow">
        <p>Crust:
                <input type="radio" name="crust" value="Thin"> Thin <br>
                <input type="radio" name="crust" value="Regular"> Regular
        </p></div>
        <div class="tableRow">
        <p> Toppings:
                <input type="checkbox" name="toppings" value="Pepperoni"> Pepperoni<br>
                <input type="checkbox" name="toppings" value="Beef"> Beef <br>
                <input type="checkbox" name="toppings" value="Italian Sausage"> Italian Sausage <br>
                <input type="checkbox" name="toppings" value="Green Peppers"> Green Peppers <br>
                <input type="checkbox" name="toppings" value="Onions"> Onions <br>
                <input type="checkbox" name="toppings" value="Mushrooms"> Mushrooms <br>
                <input type="checkbox" name="toppings" value="Banana Peppers"> Banana Peppers <br>
                <input type="checkbox" name="toppings" value="Jalapenos"> Jalapenos <br>
            <input type="checkbox" name="toppings" value="Black Olives"> Black Olives <br>
        </p></div>
        <div class="tableRow">
        <p> Extras:
                <input type="checkbox" name="extras" value="Drink"> Drink <br>
                <input type="checkbox" name="extras" value="Breadsticks"> Breadsticks <br>
                <input type="checkbox" name="extras" value="Cheesesticks"> Cheesesticks <br>
        </p></div>
        <div class="tableRow">
        <p>
        <input type="submit" value="Place Order">
        </p></div>
</form>

This is how it looks:

https://i.sstatic.net/qbHdt.png

This is how it is supposed to look:

https://i.sstatic.net/1jdLk.png

Answer №1

Don't forget to transfer all your input elements from inside the p elements to outside.

Take a look at this functional fiddle:

form {
  display: table;
}

.tableRow {
  display: table-row;
}

.tableRow p {
  display: table-cell;
  vertical-align: top;
  padding: 3px;
}

.tableRow p:first-child {
  text-align: left;
}

h1 {
  font-weight: bold;
  font-size: 16px;
}
<form action="" method="POST">
  <div class="tableRow">
    <p>
      Address: </p>
    <input type="text" name="Address">
    <br>
  </div>
  <div class="tableRow">
    <p>
      Phone:
    </p>
    <input type="tel" name="Phone">
  </div>
  <div class="tableRow">
    <p></p>
    <select name="size">
      <option value="Medium">Medium</option>
      <option value="Large">Large</option>
      <option value="X-Large">X-Large</option>
    </select>
  </div>
  <div class="tableRow">
    <p>Crust:</p>
    <input type="radio" name="crust" value="Thin"> Thin
    <br>
    <input type="radio" name="crust" value="Regular"> Regular</div>
  <div class="tableRow">
    <p> Toppings:
    </p>
    <input type="checkbox" name="toppings" value="Pepperoni"> Pepperoni
    <br>
    <input type="checkbox" name="toppings" value="Beef"> Beef
    <br>
    <input type="checkbox" name="toppings" value="Italian Sausage"> Italian Sausage
    <br>
    <input type="checkbox" name="toppings" value="Green Peppers"> Green Peppers
    <br>
    <input type="checkbox" name="toppings" value="Onions"> Onions
    <br>
    <input type="checkbox" name="toppings" value="Mushrooms"> Mushrooms
    <br>
    <input type="checkbox" name="toppings" value="Banana Peppers"> Banana Peppers
    <br>
    <input type="checkbox" name="toppings" value="Jalapenos"> Jalapenos
    <br>
    <input type="checkbox" name="toppings" value="Black Olives"> Black Olives
    <br>
  </div>
  <div class="tableRow">
    <p> Extras:
    </p>
    <input type="checkbox" name="extras" value="Drink"> Drink
    <br>
    <input type="checkbox" name="extras" value="Breadsticks"> Breadsticks
    <br>
    <input type="checkbox" name="extras" value="Cheesesticks"> Cheesesticks
    <br>
  </div>
  <div class="tableRow">
    <p></p>
    <input type="submit" value="Place Order">
  </div>
</form>

Answer №2

Start by implementing HTML labels for every input field:

<label for="Address">Address</label>    

Next, assign a class to the labels and apply the following styles:

.labelStyle {
    display: inline-block;
    width: 50px; /* adjust as needed */
}

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

Capturing both inner text and inner tags with BeautifulSoup

One of the challenges I'm facing is with a document I am parsing, which contains a mixture of div tags and inline text. Extracting the contents in order has proven to be tricky. Let's consider an example: <div> <div>1</div> &l ...

Troubleshooting the issue with reactdom.render() functionality in CodeSandbox

Having issues with ReactDom in CodeSandbox for React. The HTML file includes: <body> <div id="root"></div> </body> <script src="scr/index.js"> The JavaScript file (named index) includes: ReactDOM.rende ...

What is the best way to align this button to the right?

I'm really struggling with this issue and could use some assistance. I've been trying to place this green button on the right side since yesterday, but traditional methods like using margin aren't working. HTML <ion-header> &l ...

Invisible style date input value on iPhone mobile devices

My goal is to enhance the appearance of an HTML5 input with a custom icon, using a font pack. Although I mention FontAwesome in this example, in reality, I'm utilizing a different custom font pack. On my mobile device (iPhone X), I can see the calend ...

Problems with CSS animations on mobile devices in WordPress

On my website powered by Elementor Pro, I have implemented custom CSS to animate the Shape Divider with a "Brush Wave" style. Below is the code snippet: .elementor-shape-bottom .elementor-shape-top .elementor-shape body { overflow-x:hidden; } @keyframes ...

Issues with padding and margin on iOS devices

On my iPhone (iPhone 6, iOS 8), I noticed that the loader is positioned slightly above the button. However, when I use the Chrome mobile emulator, I can't see this issue. I'm puzzled about how to resolve it. What do you think could be causing thi ...

Ways to link two PHP scripts

My code snippet below aims to select a location from a dropdown menu and generate a pie chart based on data fetched from a PostgreSQL database. However, I am facing an issue where the pie chart displays all column values instead of only the selected locati ...

Implementing a smooth transition effect, such as opacity or fade-in, on an image retrieved from an

I am struggling to achieve opacity with a transition on an image after clicking and changing the photo. I have created a basic slider that displays pictures from an array. How can I smoothly transition the opacity of the image from 0 to 1 over a duration ...

Guide to creating a toggle button

Can someone help me create a button that toggles between displaying block and display none when clicked? I've been trying to use Classlist.toggle with JavaScript, but I'm not sure if I have the correct classes targeted. let showCart = documen ...

Navigation that sticks and changes upon hovering over div elements

Just delving into the world of jQuery and JS, so I appreciate your patience:) Currently, I have a sticky navigation bar positioned at the top of my webpage that links to different sections of content below. I am looking to create an effect where the corr ...

Angular Forms, mark a form as invalid if a p-select element retains its original value

I am looking to prevent the submit button from being enabled until a user makes changes in any of the form controls. The challenge I'm facing is that while required input fields are easy to handle, there is also a select control with three options. De ...

Issue with pop-up functionality on web page using HTML, CSS, and JavaScript

Recently, I created a unique popup using HTML. You can see the complete code (excluding CSS) here: https://codepen.io/nope99675/pen/BawrdBX. Below is the snippet of the HTML: <!DOCTYPE html> <html> <head> <meta charset=&quo ...

Is there a way to relocate the collapse button to the bottom after it has been clicked?

I have been using Bootstrap to design a expandable card that can be expanded when clicked. I am currently using the collapse feature, but I am facing an issue where the button remains in its original position, whereas I want it to move to the bottom of the ...

jQuery condition doesn't properly resetting the states of the original checkboxes

Having trouble phrasing the question, apologies! Take a look at this fiddle to see my objective: http://jsfiddle.net/SzQwh/. Essentially, when a user checks checkboxes, they should add up to 45 and the remaining checkboxes should then be disabled. The pr ...

The scrollHeight property is not accurate in Internet Explorer

I'm attempting to determine if a div contains a visible vertical scrollbar However, when comparing the scrollHeight with the clientHeight, there is always a difference of 1 if (div.scrollHeight > div.clientHeight) { // Div has a visible ...

In my Laravel 5.1 form using form::radio, I am experiencing an issue where I am unable to retrieve the value of "false" from a variable

This snippet represents my code: <div class="form-group"> {!! Form::label('', '*Role') !!} @foreach ($roles as $role) <div class="radio"> {!! Form::radio( 'role', $role->id, $user->getRolesChecke ...

Insert a new row with a designated row class

I have a table with 2 columns and an "add row" button. However, I want to add a new row between the rows with the classes "title" and "ongkir". Currently, the new row is being added after the "ongkir" row. Can someone please assist me in achieving this? B ...

The icon on the left displaying peculiar conduct

Currently in the process of developing an application using Bootstrap and incorporating a user feed for admins which is inspired by the Dark Admin theme. The structure of my HTML resembles the demo provided, with CSS styling being compared to ensure consis ...

Link with an embedded background image

When I click on the 'law firms' image in my project, instead of displaying a short HTML page with text as intended, it shows a '>' character. I have three circular images on my jsFiddle, and when hovered over, a background shadow ima ...

Send an email containing the entirety of a web page

After selecting a user from a drop-down and running a search, a PHP page displays a time schedule in HTML table format. Now, the goal is to send the entire page to the user via email when a specific button is clicked. The user's email address is sto ...