Differences between using tables and divs for form layouts

After searching online for examples of form implementations using DIVs, I noticed that most were simple one-column forms. However, my forms are more complex, like the one shown in this image:

Although I can easily create these forms using tables, I encounter issues when attempting to hide certain choices and adjust values to prevent gaps in the layout.

While experimenting with div-based forms, I found that they become disorganized when resizing the browser window and aligning elements is challenging.

Although the topic discussed in this link is relevant: Is it bad design to use table tags when displaying forms in HTML? it doesn't completely address my concerns.

Do you have any suggestions for a solution? Should I continue manipulating values within tables or should I explore alternative methods using DIVs?

Answer №1

My recommendation would be to utilize the div approach for creating layouts. By being cautious with your widths when using floats, achieving proper layout across various screen resolutions can be relatively straightforward.

Consider the following guidelines:

  1. Set a maximum width for your form or its wrapper element. When floating elements on a single row, ensure that the combined widths do not exceed this set width.
  2. If you are applying horizontal padding/margins to your floated elements, remember that these additions contribute to the total width of the element.
  3. Avoid mixing percentage widths with pixel-based padding and margins. Allocate the percentage width to a parent element and the pixel-based padding/margins to a child element.
  4. Insert clearing elements between rows to maintain alignment throughout the layout.

For markup, combine form elements with CSS to construct a semantic structure:

<fieldset>
    <legend>Fieldset Title</legend>

    <label for="input1">Input 1:</label>
    <span><input type="text" id="input1" name="input1"/></span>
    <label for="input2">Input 2:</label>
    <span><input type="text" id="input2" name="input2"/></span>

    <br/>

    <label for="input3">Input 3:</label>
    <span><input type="text" id="input3" name="input3"/></span>
    <label for="input4">Input 4:</label>
    <span><input type="text" id="input4" name="input4"/></span>
</fieldset>

Corresponding CSS styling:

fieldset {
    padding: 20px 0;
    width: 600px;
    margin: 0;
    border: 0;
}

...

View the result here.

Answer №2

If you're dealing with a table of fixed width, it's simple to arrange using divs and floats. Just specify the width for each element as needed.

But when working with a liquid-layout table—which is usually preferred—it becomes more challenging to create a form without resorting to table-style display properties. This is because attributes like float and position don't easily allow for calculations such as "this cell should take up half of the remaining width after allocating space for fixed-width labels."

In scenarios like this, particularly with a two-column form like yours, your best option is to utilize CSS display values related to tables. While you can use divs with display: table-row for modern browsers like IE8 and above, older versions including IE6-7 require actual <table>, <tr>, and <td> elements since they do not support table-CSS independently.

This approach is perfectly acceptable, as forms have a somewhat tabular structure and maintaining logical order in page content is key for accessibility.

Additionally, when working with liquid-layout forms, there's also the challenge of sizing input fields to align with their parent elements. While input { width: 100%; } gets close, it doesn't account for the border or padding that inputs typically have by default. To address this, you can leverage CSS3 box-sizing along with browser-specific versions for modern browsers. For precise alignment on older browsers like IE6-7, you may need to adjust the padding on parent elements to match the border/padding of the input field.

Answer №3

  • Basic facts is a particular type of listing, consisting of key and value pairs - the ideal structure for this would likely be <dl />
  • Problem numbers exist in a tabular format,
  • Evaluations are also presented in table form,
  • Both Recovery and Signals are organized as lists - specifically unordered lists <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

The overflow:hidden feature doesn't appear to be functioning as expected

I am struggling to hide the text details within a div containing text and image, organized in ul li structure. Despite applying overflow hidden to the .sbox class, the text details refuse to remain hidden and appear to be overflowing. Explore this issue o ...

An improved solution for uploading images to the cloud in NextJS

Currently, I am working on my first real project for a client using NextJS and MongoDB. I have run into issues with uploading images while working with Cloudinary. The problem is that Cloudinary does not support multiple file uploads and there are also som ...

Incorporate a unique identifier for dynamic elements

Is there a way to give generated divs the same name so I can markup them easily? $.getJSON("data/reviews.json", function(data){ for(var i=0; i<data.length; i++) { var review = sym.createChildSymbol("Template", "content"); review.$("title").html ...

Designing webpages by superimposing text onto images with customizable positioning

My current project involves developing an HTML document with a floor plan image as the main layer. On top of this image, I need to display employee names extracted from a database and positioned on the map based on a location variable within the database ...

Using javascript, hide or show a div without using jquery or the display:none property

I am looking for a way to show/hide a div up and down, but I have some requirements: I cannot use jQuery: toggle(), slideToggle(), fade, animate, etc. all use display: none, and I need the div to still occupy space in the DOM (I will be processing things ...

What is the best method for positioning two forms in a vertical arrangement?

I'm looking to integrate a Login form in HTML within my C# project. Here's what I have so far: However, as you can see, the layout is not very aesthetically pleasing. I'm struggling to align the elements in a more visually appealing way. My ...

What are some ways to create a div section within a Google Map interface?

Is there a way to create a div area within the Google map iframe? Some of my code is already prepared here (). The image in this link () illustrates exactly what I'm trying to achieve. ...

Craft a Flawlessly Repeating Sound Experience - Online

I'm facing a challenge in creating a flawless loop of an audio file. However, all the methods I've tried so far have resulted in a noticeable gap between the end and the start. Here are the approaches I experimented with: The first approach inv ...

When I hover my mouse over the items on the Navigation bar, they shift forward

Seeking assistance with a coding issue. When hovering over TOYOTA, the HONDA and CONTACT US sections move forward unexpectedly. This movement is unwanted as I would like only the next list item to display when hovering over TOYOTA and HONDA. How can I corr ...

Unexpected Issues with the Ant Design Sidebar Layout Demo

My React webapp is utilizing the Ant design component framework version 4. I attempted to integrate this example from the Antd documentation into my webapp: https://codesandbox.io/s/ymspt However, when I implemented the code in my webapp, the result didn ...

A commonly used method to adjust the width of a table column to accommodate the size of the child element

I have a specific table width of 776 pixels with three columns. The third column has a fixed width of 216 pixels, but the widths of the other two are unknown. I want the second column to adjust its width based on its content, and whatever space is left aft ...

What is the best way to have text wrap around an icon in my React application?

I am facing an issue while trying to display the note description over the trash icon in a React app. I have tried various methods but can't seem to achieve the desired effect. Can anyone guide me on how to get this layout? Here is what I intend to a ...

Removing items from a list in a Vue.js application

I've encountered an issue with my code. I'm attempting to remove a "joke" from a list, but it consistently removes the joke that was inputted before the one I'm actually trying to delete. I'm struggling to identify what mistake I might ...

Creating a dropdown menu using Vue.js

My latest project involves coding an html page that features a drop-down list using HTML, CSS, and VueJS. The goal is to have something displayed in the console when a specific option from the drop-down list is selected. Here's the snippet of my html ...

Keep elements in position when their bottom edge becomes visible while scrolling

This task may appear intricate, but I will do my best to explain it! I want to create a continuous scrolling article display similar to Bloomberg Politics (), but with a slight twist. My idea is to have the current article's bottom edge stick to the ...

What is the reason for jQuery not detecting that a checkbox has been automatically checked?

I am facing an issue with my jQuery code that checks for when a user clicks on a check button. The problem arises when the page loads and the button is already marked as checked, causing the jQuery to not function properly. $ (function(){ var require ...

Tips for organizing AJAX code to show images and videos in HTML with a switch case approach

I have 2 tables, one for images and one for videos. Within my HTML code, I have three buttons: slide_image, video, and single_image. Using iframes, I am able to load images and videos on the same page. When I click on the slide_image button, it displays im ...

Adjust the height of a <div> element to fit the remaining space in the viewport or window automatically

I am working with the following HTML code: <html> <BODY> <DIV ID="holder"> <DIV ID="head_area">HEAD CONTENT GOES HERE....</DIV> <DIV ID="main_area">BODY CONTENT GOES HERE</DIV> ...

"What's the best way to update the src attribute of an iFrame when a user clicks

I'm new to coding in PHP or JavaScript, so please bear with me if my question isn't quite right. I'm looking for a way to dynamically pass and embed a URL from a link into the src attribute of an iframe. Essentially, I want users to click o ...

Retrieving information from a server within several sections of a Flask application

Currently working on a project with Python using Flask and Jinja2, I am exploring ways to integrate a sidebar. Within the HTML pages, there is this snippet: {% include "sidebar.html" %} My objective for the sidebar file is to showcase a section highlight ...