Create A Form Using HTML Tables

Create A Form Using HTML Tables

Creating forms using HTML tables is a method that was commonly used in the early days of web development to align form elements. Although modern CSS techniques such as Flexbox and Grid have largely replaced the need for tables in layout design, understanding how to create a form using HTML tables can still be beneficial for those maintaining legacy code or working within specific constraints. In this article, we will explore how to create a form using HTML tables, providing detailed examples with complete, standalone HTML code.

Basic Form Structure Using Tables

To start with, let’s create a simple form structure using an HTML table. This form will include basic elements such as text inputs and a submit button.

Example 1: Simple Form with Text Inputs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 1</title>
</head>
<body>
    <form action="http://how2html.com/submit" method="post">
        <table>
            <tr>
                <td><label for="name">Name:</label></td>
                <td><input type="text" id="name" name="name"></td>
            </tr>
            <tr>
                <td><label for="email">Email:</label></td>
                <td><input type="email" id="email" name="email"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Adding Different Input Types

Forms often require different types of inputs, such as passwords, radio buttons, and checkboxes. Let’s see how to include these in our table-based form layout.

Example 2: Form with Password Input

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 2</title>
</head>
<body>
    <form action="http://how2html.com/login" method="post">
        <table>
            <tr>
                <td><label for="username">Username:</label></td>
                <td><input type="text" id="username" name="username"></td>
            </tr>
            <tr>
                <td><label for="password">Password:</label></td>
                <td><input type="password" id="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Login"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Example 3: Form with Radio Buttons

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 3</title>
</head>
<body>
    <form action="http://how2html.com/submit" method="post">
        <table>
            <tr>
                <td>Gender:</td>
                <td>
                    <input type="radio" id="male" name="gender" value="male">
                    <label for="male">Male</label>
                    <input type="radio" id="female" name="gender" value="female">
                    <label for="female">Female</label>
                </td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Incorporating Textareas and Select Elements

For longer text inputs and options, textareas and select elements are used. Here’s how to include them in a table-based form.

Example 4: Form with Textarea

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 4</title>
</head>
<body>
    <form action="http://how2html.com/feedback" method="post">
        <table>
            <tr>
                <td><label for="comments">Comments:</label></td>
                <td><textarea id="comments" name="comments" rows="4" cols="50"></textarea></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit Feedback"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Example 5: Form with Select Dropdown

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 5</title>
</head>
<body>
    <form action="http://how2html.com/register" method="post">
        <table>
            <tr>
                <td><label for="country">Country:</label></td>
                <td>
                    <select id="country" name="country">
                        <option value="usa">United States</option>
                        <option value="canada">Canada</option>
                        <option value="uk">United Kingdom</option>
                        <!-- Add more options as needed -->
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Register"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Styling Forms with CSS

While tables provide a way to align form elements, CSS can be used to enhance the appearance of the form. Let’s add some basic styles to our form.

Example 6: Styled Form with CSS

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 6</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        td {
            padding: 10px;
            border: 1px solid #ddd;
        }
        input[type="text"],
        input[type="email"],
        input[type="password"],
        textarea,
        select {
            width: 100%;
            padding: 8px;
            margin: 4px 0;
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        input[type="submit"] {
            width: 100%;
            background-color: #4CAF50;
            color: white;
            padding: 14px 20px;
            margin: 8px 0;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
        label {
            display: block;
        }
    </style>
</head>
<body>
    <form action="http://how2html.com/submit" method="post">
        <table>
            <tr>
                <td><label for="firstname">First Name:</label></td>
                <td><input type="text" id="firstname" name="firstname"></td>
            </tr>
            <tr>
                <td><label for="lastname">Last Name:</label></td>
                <td><input type="text" id="lastname" name="lastname"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Advanced Form Elements

Forms can also include more advanced elements like file inputs, hidden fields, and buttons with event handlers. Let’s see how to add these to our table-based forms.

Example 7: Form with File Input

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 7</title>
</head>
<body>
    <form action="http://how2html.com/upload" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td><label for="file">Upload File:</label></td>
                <td><input type="file" id="file" name="file"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Upload"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Example 8: Form with Hidden Input

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 8</title>
</head>
<body>
    <form action="http://how2html.com/process" method="post">
        <table>
            <tr>
                <td><label for="username">Username:</label></td>
                <td><input type="text" id="username" name="username"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="session_id" value="XYZ123"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Example 9: Form with Button and Event Handler

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 9</title>
    <script>
        function showAlert() {
            alert('Button clicked!');
        }
    </script>
</head>
<body>
    <form action="http://how2html.com/action" method="post">
        <table>
            <tr>
                <td><label for="info">Information:</label></td>
                <td><input type="text" id="info" name="info"></td>
            </tr>
            <tr>
                <td colspan="2"><button type="button" onclick="showAlert()">Click Me!</button></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Using Tables for Form Layout with Multiple Columns

Sometimes, forms need to be structured in a more complex way, such as having multiple columns for inputs. HTML tables can be very useful for this purpose.

Example 10: Multi-column Form Layout

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form with HTML Tables - Example 10</title>
</head>
<body>
    <form action="http://how2html.com/register" method="post">
        <table>
            <tr>
                <td><label for="first_name">First Name:</label></td>
                <td><input type="text" id="first_name" name="first_name"></td>
                <td><label for="last_name">Last Name:</label></td>
                <td><input type="text" id="last_name" name="last_name"></td>
            </tr>
            <tr>
                <td><label for="email">Email:</label></td>
                <td><input type="email" id="email" name="email"></td>
                <td><label for="phone">Phone:</label></td>
                <td><input type="tel" id="phone" name="phone"></td>
            </tr>
            <tr>
                <td colspan="4"><input type="submit" value="Register"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Output:

Create A Form Using HTML Tables

Conclusion

Using HTML tables to create forms can be a practical approach, especially when dealing with legacy systems or when a specific layout is required that is easier to achieve with tables. While modern CSS techniques offer more flexibility and efficiency for layout tasks, the examples provided in this article demonstrate that HTML tables still have their place in web form design. Each example is a complete, standalone HTML document, ensuring that you can test and modify each one independently to better understand how HTML tables can be used to structure forms.

Like(0)

HTML Articles