Creating a layout resembling a table using CSS and HTML without actually using the <table> element

Despite searching on various platforms, I have not found a satisfactory solution to my issue.

To demonstrate the problem with using a table, I have attached a screenshot:

The bottom two rows are structured as tr and td within a table. Though the alignment of labels and textfields is perfect, attempting to style a well class around these two rows would result in failure. This is because tables do not allow the presence of a div; they only accept tr and td.

To address this limitation, I decided to separate the first two rows from the table and convert them into pure divs. The outcome can be seen in the grey well above.

<div class='well'>
  <div>
    <div class='block_inline'> ... </div>
    <div class='block_inline'> ... </div>
  </div> 
  <div>
    <div class='block_inline'> ... </div>
    <div class='block_inline'> ... </div>
  </div>
</div>

Although the well class now encompasses the two rows beautifully, the alignment has been disrupted. How can I maintain centering while ensuring vertical alignment of the text-fields?

Answer №1

To achieve a similar effect using div elements, you can utilize the display property with values like table, table-row, and table-cell:

Sample HTML structure:

<div class='container'>
  <div class="row">
    <div class='box'> Content 1 </div>
    <div class='box'> ... </div>
  </div> 
  <div class="row">
    <div class='box'> Content 2 </div>
    <div class='box'> ... </div>
  </div>
</div>​

CSS styling:

div {
    border: 1px solid #333;
}

.container {
    display: table;
    width: 70%;
}

.row {
    display: table-row;
}

.box {
    display: table-cell;
    width: 50%;
}

By structuring your content this way, you can achieve a table-like layout while maintaining semantic markup. This method can also help in resolving issues related to accommodating remaining space columns :)

http://jsfiddle.net/Your_Name/example/

Answer №2

Initially, the structure of this code needs some refinement. Here is a modified version:

<form>
    <fieldset>
        <label for="input_1">Title:</label>
        <input type="text" name="input_1" id="input_1">
    </fieldset>
    <fieldset>
        <label for="input_2">Due Date Time*</label>
        <input type="text" name="input_2" id="input_2">
    </fieldset>

</form>

When it comes to styling, consider the following CSS rules:

fieldset {
    clear: both;
}

label {
    display: inline-block;
    width: 300px;
}

input {
    display: inline-block;
}

It may also be beneficial to explore alternative structures like using fieldsets and definition lists instead of div class="row". Refer to this resource for more insights on that.

Answer №3

I prefer using unordered lists (ULs) for organizing forms. Check out this example: http://jsfiddle.net/BKgB9/

<form>
 <div>
    <ul>
        <li><label>Name:</label><input type="text" /></li>
        <li><label>Email:</label><input type="text" /></li>
    </ul>
 </div>
</form>

div {
 background:#dcdcdc;
 border:1px solid #999;
 padding:20px;
 display:inline-block;
}

div ul li {
 margin-bottom:10px;
}

div ul li label {
 float:left;
 width:85px;
}

Answer №4

For those looking for an alternative solution, consider giving this method a try:

<div class="first">
<div class="line1">
   <label>One</label>
   <select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
    </div>
    <div class="line2">
    <label>two</label>
   <select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
</div>
<div>

Check out the demo: here

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

Adjust the background shade of specific characters within an input text field

When a user inputs a string into a standard HTML input field (type="text"), such as "2013/13/29", and validation reveals that the number 13 is invalid in this context, I would like to visually highlight it by changing its background color to red while tu ...

Here's a new take on the topic: "Implementing image change functionality for a specific div in Angular 8 using data from a loop"

I need to create a list with dynamic data using a loop. When I click on any item in the list, I want the image associated with that item to change to a second image (dummyimage.com/300.png/09f/fff) to indicate it's active. This change should persist e ...

Challenge with CSS Box Shadow

Hey there, I've been attempting to add a shadow effect to a box inside another box but I'm struggling to achieve the desired outcome. <div class="box effect2"> <div class="box effect2"> Box inside a box <div> &l ...

HTML mandatory field not functioning effectively

I am currently experiencing issues with the required validation in my HTML form. It is not functioning as intended. I have two buttons on a single page and I would like for one button to validate textbox1 upon click, while the other button should validate ...

Looking to conceal the children of other parents when clicking on a specific parent element?

Is there a way to hide all child elements of every Parent Element except for the one that has been clicked? For example, when I click on one parent element to toggle its children, all other child items should be hidden. Unfortunately, I can't make any ...

What are some ways to enable text highlighting when it is disabled?

I've encountered an issue with text highlighting in my asp.net web application when using the latest versions of FireFox and Google Chrome. Strangely, pressing CTRL+A also does not work for all input fields. I haven't had the opportunity to test ...

Utilizing display: flex for creating table padding

Hey there! I'm currently working on a webpage layout that includes a logo and a table. To achieve this, I've used display flex for the wrapper element. However, I've noticed that when viewing the page on mobile devices, the padding of the ta ...

What could be causing the filter method to malfunction?

I need help saving the names of students who scored 80 or higher in a variable. I tried using filter, but it's returning the entire object instead of just the names of these students. Here's my code: // Students names/scores let students = [ ...

Retrieving paragraph content by scanning for text within bold tags

I am currently trying to extract text from a paragraph using the code driver.find_element_by_xpath("//*[contains(text(), 'Definition')]").text The issue I am facing is that the HTML is structured as follows: <p> <b> Definition: &l ...

Tips for converting mobile numbers into clickable links using JavaScript

I am working on an HTML page where data is being dynamically loaded, including some mobile numbers. I'm looking for a way to wrap all these mobile numbers in anchor tags using JavaScript. For example: <a href="tel:+91 9999999999">+91 9999999999 ...

The br tag in HTML cannot be utilized in conjunction with JavaScript

I am currently working on a project involving HTML and JavaScript. I have a "textarea" where data is inserted into the database upon pressing the "Enter key." However, I am encountering two issues: Currently unable to save data like "Lorem Ipsum" (the ...

The issue with Bootstrap 4 Card Flex Width not functioning properly seems to be unique to Firefox

I'm currently facing an issue while creating a webpage using Bootstrap 4 and flex. I have Bootstrap cards with specific widths, and it seems to work perfectly fine in Google Chrome and Opera but not in Firefox. Here's the code snippet: <d ...

Module not found in Node.js environment (webpack)

I seem to be encountering an issue with loading any modules. Despite reinstalling my operating system, I continue to face the same error when attempting to use any module. I have tried reinstalling node, clearing the cache, and more. To view the code, pl ...

The top margin is experiencing issues and is not functioning correctly

I'm struggling to identify the problem with this script: http://jsfiddle.net/AKB3d/ #second { margin-top:50px; ... } My goal is to have the yellow box positioned 50px below the top border of the right box. However, every time I add margin-top to ...

What is the best way to adjust the canvas size within a list item?

I have successfully created a 3D model with Zdog that dynamically resizes based on the screen size. The model includes a dome and brim, which rotate smoothly as intended. Here is the code snippet: const TAU = Zdog.TAU; let hat = new Zdog.Illustration({ ...

Tips for Omitting Hours, Minutes, and Seconds from AngularJS Date Display

Currently, I am retrieving data from a JSON file using the $http.get request. The format in which the date is received in my JSON file is as follows: "date": "2016-10-24 15:14:53" I would like to convert this date format to display as: October 10 2016 (e ...

Ways to maximize the value of an inline class

I have this unique inline class that needs modification <DIV title="AST Infrastructure" class="ms-acal-item ms-acal-color4" style="BORDER-LEFT-WIDTH: 0px; HEIGHT: 17px; BORDER-RIGHT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 1px; POSITION: absolute; LEFT: 22px; ...

Problem with JQUERY Galleria CSS positioning alignment specifically in Firefox, Chrome works without issues

I recently incorporated jquery Galleria into my website and I am currently facing an alignment issue with the div element gallery-container. Interestingly, it appears correctly aligned in Chrome but is shifted to the right in Firefox. You can view the webs ...

The integration of jQuery Masonry with margin-right for a seamless and

I have a row with two columns, where the first column has a right margin and the second one does not. I would like to use jQuery Masonry to eliminate any empty spaces. However, it seems that the margin right does not interact well with Masonry. Is there a ...

Interactive menu backdrop determined by div element

I am working on a website with a menu structured like the following: Home -- Work -- Pricing -- Contact Us -- About This website uses a one-page layout and has enabled Scrollspy on Bootstrap. I need to make the active menu background dynamic depending o ...