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

Create a new file using PHP by submitting a form in HTML

Currently, I am developing a PHP script that is designed to modify a specific file for the user. This script retrieves the contents of the file and places them in a textarea within a form. How can I ensure that any changes made in this textarea are saved b ...

incorporating numerous interconnected javascript files within a single html document

I have a pair of JavaScript files (file1, file2). File1 utilizes a class that is defined in file2. Can I include these files in an HTML document like this: <script type="text/javascript" src="file1.js"></script> <script type="text/javascrip ...

User information in the realm of website creation

Can someone provide insight on where user data such as photos, videos, and posts is stored? I doubt it is saved in an SQL database or any similar system. ...

Adjust the image size in the jumbotron to fit perfectly without the need for scrolling

I'm in the process of creating a website with a prominent jumbotron section on the landing page. I am looking to adjust the height of the image within the jumbotron without cutting off any parts of the picture, and I do not want it to scroll to displa ...

Deactivated dropdown menu options with the help of Twitter's bootstrap framework

Using markup, I have created a dropdown menu utilizing Twitter Bootstrap. <ul class="nav pull-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu <b class="caret"></b>< ...

Angular ng-show does not seem to evaluate properly

After receiving the JSON array object below: "Tools": [ { "name": "Submit a Claim", "position": 1, "isOn": true, "alert": null }, { "name": "My Recurring Claims", "position": 2, "isOn": true, "alert": null }, { "name": "Online Enrollment ...

In AngularJS, I've created a collection of checkboxes with a submit button ready to go

One of my goals is to make the cancel button available only when at least one job is selected. Below is my HTML View <table> <tr ng-repeat="x in jobs" ng-class-odd="'odd'" ng-class-even="'even'"> <td style="widt ...

Incorporating components into Vue while utilizing Flask poses a challenge

Currently, I am working on a project that involves Flask and Vue. I am attempting to add a carousel to my website, but I am running into difficulties with creating and importing components for Vue when following tutorials. Here is my current file structure ...

What is the best way to maintain two distinct background effects that each require different pixel sizes?

I am attempting to create a gradient that goes from the top-right corner to the bottom-left corner, and I also want to add a grid of dots. However, the method I am using to create the grid of dots is through a radial gradient which requires a small backgro ...

Issues with custom logo and menu/sidebar toggle on Wordpress

Hello everyone, I am currently facing some difficulties with my new WordPress blog that utilizes the Skilt-Theme. Specifically, I'm having trouble with the custom-logo and sidebar-toggle features. When there is no logo present, you can see the layout ...

Automatically save a dropdown menu selection

Is there a way to automatically save the data selected in a drop down list without having to click on a save button? public ActionResult SelectFeedBack(int id) { YelloAdminDbContext db = new YelloAdminDbContext(); ViewBag.FeedBack ...

Gliding along a div and concealing it out of view, making it seem like it has disappeared

I attempted to slide down the ".preloader" div after a 2-second delay using JQUERY, but I couldn't get it to work as expected. I didn't want to use a toggle button for this effect. I also experimented with creating an animation using @keyframes, ...

Using Django to dynamically fill out a form with data retrieved through ajax

My registration form is pretty straightforward, but I need some help with filtering data. There are three additional fields in the form: University Course Module The goal is to allow the user to select a University, and based on that selection, populate ...

What is the process for assigning a value to the body in a div element?

Within a div, there is a body element structured like this: <div id = "TextBox1"> <iframe id = "TextBox1_1"> #document <html> <head></head> <body></body> </html> </iframe> </div> I have attempte ...

Achieving the perfect alignment: Centering a paragraph containing an image using JQuery

I need help centering the background image of my <p> tag on the webpage. Script $(function() { $('ul.nav a').bind('click', function(event) { var $anchor = $(this); $('html, body').stop().animate({ ...

The functionality of the Bootstrap4 accordion is not functioning according to my expectations

My goal is to create a unique e-commerce checkout page design. Each panel would be opened sequentially, with the next panel unfreezing when the action button of the current panel is clicked. However, I seem to be making a mistake as it is not working as in ...

Is there a way for me to utilize the Bootstrap carousel without it adjusting the height as the user transitions from slide 1 to slide 2?

body{ background-color: #CAF7E3; } .container-fluid{ padding: 3% 15%; } .navbar{ font-family: 'Montserrat'; font-weight: bold; } .navbar-brand{ font-size: 2.5rem; } .nav-item{ margin-right: 2rem; } #title{ padding: 8% 15%; backgr ...

Switching hover behavior on dropdown menu for mobile and desktop devices

I have implemented a basic JavaScript function that dynamically changes HTML content based on the width of the browser window. When in mobile view, it removes the attribute data-hover, and when in desktop view, it adds the attribute back. The functionalit ...

Tips for creating responsive elements with a z-index of 1 in CSS

I have implemented the code provided in a codepen tutorial, but I replaced the world map image with a USA map. However, the dots are not responsive due to the z-index=1 specified on them. I experimented with vh, vw, and percentages, yet when I resize my s ...

Creating a unique Tag Name for Dynamic Components in Angular

I want to customize a component @Component({ selector: '[my-component]', template: `<i>1</i><p>content</p><b>2</b>` }) export class MyComponent { public tagName: string; } Then there's another comp ...