Creating a sophisticated form design with multiple input fields using DIV elements in HTML

I am looking to create a multi-column form layout that allows each row to have a different number of fields. My initial approach was using tables and the colspan attribute within td tags for layout structure. However, I have learned that using tables for layout is not recommended and would like to update my code to use div elements instead.

Could someone provide me with a good example on how to achieve a similar layout while following best practices? My main challenge is ensuring that the columns have varying widths.

Thank you.

Answer №1

I apologize for any validity issues with this code snippet, as well as the lack of a clear layout with proper margins.

Example

Check out this example on jsFiddle

HTML Markup

<div>
    <div class="w50">
        <span class="label">Name</span>
        <input type="text" value="Test" />
    </div>
    <div class="w50">
        <span class="label">Surname</span>
        <input type="text" value="Test" />
    </div>

    <div class="w100">
        <span class="label">Contact</span>
        <input type="text" value="Test" />
    </div>

    <div class="w50">
        <span class="label">Age</span>
        <input type="text" value="Test" />
    </div>
    <div class="w50">
        <span class="label">Email</span>
        <input type="text" value="Test" />
    </div>

    <div class="w70">
        <span class="label">Phone</span>
        <input type="text" value="Test" />
    </div>
    <div class="w30">
        <span class="label">Time</span>
        <input type="text" value="Test" />
    </div>

    <div class="w50">
        <span class="label">Age</span>
        <input type="text" value="Test" />
    </div>
    <div class="w50">
        <span class="label">Email</span>
        <input type="text" value="Test" />
    </div>
</div>

Cascading Style Sheet (CSS)

* {
    margin: 0;
    padding: 0;    
}
.label {
    width: 60px;
    display: inline-block;
}
.w30, .w50, .w70, .w100 {
    height: 20px;
    float: left;
    display: inline-block;
    width: 100%;
}
.w30{
    width: 30%;
}
.w50{
    width: 50%;
}
.w70{
    width: 70%;
}
.w100{
    width: 100%;
}

Answer №2

To tackle this problem effectively, it's essential to devise a grid system. For instance, I've devised a 5% based grid setup in my example. You can observe an illustration of some identical components by clicking on the following link: this fiddle.

#container { font-size: 12px; width: 700px; }

.row { float: left; margin: 5px 0; width: 100%; }

.w10 { width: 10%; }
.w15 { width: 15%; }
.w20 { width: 20%; }
.w25 { width: 25%; }
.w30 { width: 30%; }
.w35 { width: 35%; }
.w40 { width: 40%; }
.w50 { width: 50%; }
.w60 { width: 60%; }
.w70 { width: 70%; }
.w80 { width: 80%; }
.w90 { width: 90%; }
.w100 { width: 100%; }

.item { box-sizing: border-box; float: left; }

input, select, option { margin: 0; }

Furthermore, I have sorted the items into rows to create an organized, grid-like appearance.

<div id="container">
    <div class="row">
        <div class="item w15">/* Entity Name</div>
        <div class="item w35">Maricopa County Community College District</div>
        <div class="item w50">*Domain: USPF, SLG, Special Districts, Community College</div>
    </div>
    <div class="row">
        <div class="item w15">/* Doctype</div>
        <div class="item w10">NLP?</div>
        <div class="item w20">Filename/Keywords</div>
        <div class="item w20">*Source Frequency</div>
        <div class="item w35">
            <input type="radio" name="freq" checked="checked" />
            <label>Daily</label>
            <input type="radio" name="freq" />
            <label>Weekly</label>
            <input type="radio" name="freq" />
            <label>Monthly</label>
        </div>
    </div>
    <div class="row">
        <div class="item w15">
            <input type="checkbox"/>
            <label>Audit</label>
        </div>
        <div class="item w10">
            <input type="checkbox"/>
        </div>
        <div class="item w20">
            <input type="text"/>
        </div>
        <div class="item w20">*Every</div>
        <div class="item w15">
            <input type="text" class="w20" value="1"/>
            <label>Days</label>
        </div>
        <div class="item w20">
            <select>
                <option value="utc-6">UTC -6</option>
            </select>
        </div>
    </div>
</div>

In essence, creating a specific structure with a row-based grid design is key to achieving your desired layout.

Answer №3

Tables get a bad rap sometimes.

The main reason why tables are often not recommended for layout is that they load the content only when everything in the table has loaded on the page. On the other hand, divs display their content as soon as it's available.

In your case, your form seems quite intricate to me and showing partial content of this form while the page is still loading may not be ideal. It would be better to have all the fields of your form visible at once.

Moreover, if you're dealing with tabular data (which seems to be the case here), using tables is actually recommended.

Therefore, I would suggest considering using a table for your form. One additional benefit of tables is that you don't have to worry too much about aligning your content.

Answer №4

If you're looking to showcase different widths using CSS, one approach is to create multiple classes representing the various widths you want to display. While this method may not result in strictly flexible columns, it can be thought of as flexible rows where you have to consider arranging content horizontally instead of vertically.

For each row of content, you would assign specific width classes like so:

<div class="row">
    <div class="left width-50"></div>
    <div class="right width-50"></div>
</div>
<div class="row">
    <div class="left width-70"></div>
    <div class="right width-30"></div>
</div>
<div class="row">
    <div class="left width-100"></div>
</div>
....
....

I hope this suggestion proves helpful for your needs.

Answer №5

Take a look at this:

HTML

<html>
<head>
    <link rel="stylesheet" href="contactForm.css"></link>
</head>
<body>

<div id="contactform">


    <div id="first">
        <div id="name">
            <div id="description">Name</div> 
            <input type="text" name="textName">
        </div> 
        <div id="surname">
            <div id="description"> Surname</div> 
            <input type="text" name="textSurname"> 
        </div>
    </div>

    <div id="second">
        <div id="contact"><div id="description">Contact</div> <input type="text" name="textContact"></div>
    </div>

    <div id="third">
        <div id="age">
            <div id="description">Age</div> 
            <input type="text" name="textAge">
        </div> 
        <div id="e-mail">
            <div id="description">E-mail</div> 
            <input type="email" name="textEmail">
        </div>
    </div>

    <div id="fourth">
        <div id="phone">
            <div id="description">Phone</div> 
            <input type="text" name="textPhone">
        </div> 
        <div id="time">
            <div id="description">Time</div> 
            <input type="date" name="textTime">
        </div>
    </div>

</div>

</body>

</html>

CSS

#contactform {width:500px; height:500px;}

#contactform div {float:left; padding-top:5px;}
#first, #second, #third, #fourth {width:100%;} 

#first #description {width:30%;}
#name, #surname {width:50%;}
#surname #description {margin-left:11px;}
#first input {width:65%;}

#second #description {width:15%;}
#contact {width:100%;}
#second input {width:85%;}

#third #description {width:30%;}
#age, #e-mail {width:50%;}
#e-mail #description {margin-left:11px;}
#third input {width:65%;}

#fourth #description {width:30%;}
#phone, #time {width:50%;}
#time #description {margin-left:11px;}
#fourth input {width:65%;}

Output

Hope this helps.

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

Styling CSS: Adjusting font sizes for placeholders and text boxes

I am struggling with styling an input field to have a font size of 45px while the placeholder text inside it should be 18px and vertically aligned in the middle. Unfortunately, my current code is not working in Chrome and Opera. Can anyone offer a solutio ...

Script for automated functions

I have implemented 4 functions to handle button clicks and div transitions. The goal is to hide certain divs and show one specific div when a button is clicked. Additionally, the background of the button should change upon hovering over it. Now, I am look ...

Embedding a CSS file into a PHP class

I've encountered a little issue that I can't seem to solve on my own. I've put together a "webengine" using multiple classes. The primary class responsible for obtaining a theme's internals is called "Logic". This Logic class contain ...

Changing Images with Jquery on Click Event

This section of the HTML document contains an image link that is designed to switch between two images when clicked. The images in question are timeline-hand and hand-clicked. Clicking on the image should change it from one to the other and vice versa. Ho ...

Steps for executing ASP code pulled from the database

In my ASP project, I have a single page where clicking on different links retrieves corresponding HTML from the database and loads it. Some links are always present on the page (static HTML), while other divs display dynamic content fetched from the DB. Wi ...

Using CSS styles in emails can lead to an unexpected horizontal scrolling effect

I am currently working on an application using Symfony, and I have implemented a basic confirmation email with minimal CSS styling. However, the email template is causing horizontal scrolling due to the layout. Below is the HTML code of my email template: ...

Is there a way to ensure my chat view functions properly without relying on partial views?

I am currently working on integrating a private chat feature using SignalR in my project, and I have encountered an issue while creating a View with accessible model values to pass to the server. Specifically, I have a list of doctors, and when a user clic ...

When using Vue with CSS3, placing an absolute positioned element within a relative wrapper can cause issues with maintaining the

Just starting out with Vue and diving into the world of CSS3! I'm currently working on a component that you can check out here: https://codesandbox.io/s/yjp674ppxj In essence, I have a ul element with relative positioning, followed by a series of di ...

Is there a way to view images embedded in local .msg files in a web browser in HTML format?

Does anyone know how to display embedded pictures in local .msg files when opening them in a web browser using HTML format? Currently, when I open the .msg file with embedded pictures, all I see is a blank space indicating "picture not found." Interestin ...

Clearing data validation in HTML after an error has been resolved.Is there a way to clear

I need some help with a cloud page I am creating. I have implemented an alphabetic data validation in HTML to only allow users to input letters, which is working as expected. However, the issue I am currently facing is that the data validation error messa ...

Transforming ASCII Source Files into JPEG Images

I specialize in publishing technical books, available in print, PDF, and Kindle/MOBI formats, with EPUB coming soon. One challenge I have encountered is that the Kindle does not support monospace fonts, which are important for source code listings. The wo ...

What is the best way to adjust the responsiveness of my AppDrag page for mobile optimization?

My landing page is all set up, but I'm looking to tweak the font sizes and spacing for mobile users. Can these changes be made using design tools instead of digging into the code? ...

What is the best way to customize a div depending on the validation status of all reactive form fields within it?

I am facing a challenge with a rather complex form that contains multiple fields. Some of these fields are used to create logical blocks, and I would like to emphasize the surrounding div if any of these included fields are invalid. Can you suggest the bes ...

What is the best way to showcase the following values using Vue.js?

{"status":true,"data":[{"ref_id":"22","agent_id":"68","p_id":"84","description":"i am interested"},{"ref_id":"24","agent_id":"68","p_id":"84","description":"For more information about Bootstrap and Bootstrap Glyphicons, visit our Bootstrap Tutorial.For mor ...

How to choose a single checkbox in Ionic 2 and retrieve its value

On my single screen, I have about 20 questions and answers retrieved from a database. To display the options, I am using checkboxes but struggling to implement the desired functionality. What I want is to allow only one checkbox selection out of 4 options ...

Error: Unable to modify the div content - Uncaught TypeError: Unable to assign value to innerHTML of null

Attempting to update the contents of a div using JavaScript and innerHTML, but encountering an issue. After implementing the code to change the div's content, the functionality stopped working. The syntax has been double-checked. Tools being used in ...

Transform a dropdown menu into an inverted footer

I stumbled upon a unique dropdown menu design that I want to tweak and reverse, making it an innovative "dropup" menu that opens from the bottom to the top. Currently, this is what I have: HTML <div class="get-started"> <a href="#" id="get-s ...

Dynamic Material UI TextField underline width using JSX in React

I recently encountered an issue while trying to add padding to my TextField component. I applied the padding to the root 'style' property, but it caused the underline to overflow beyond the designated area to the right by the same amount as the p ...

Transitioning the height of a Vue component when switching routes

I've been struggling to implement a transition slide effect on my hero section. The height of the hero is set to 100vh on the homepage and half of that on other pages. Despite trying various methods, I haven't been able to get it working properly ...

Top method for showcasing only unique values from various div tags with identical class names

Is there a way to show only unique values from multiple divs with the same class name? <div class="categories">cat 1</div> <div class="categories">cat 1</div> <div class="categories">cat 2</div> <div class="categorie ...