Creating a two-column layout in CSS using div elements instead of tables allows for

Why is it so challenging to create a two-column form layout using CSS?

All I'm trying to achieve is:

Name:         John Smith
Age:          25
Description:  A long text that should wrap if it exceeds the border, while still aligning with the first line.
Location:     Los Angeles

Below is my HTML code (with some embedded Razor):

<div class="section">
    <label>Name:</label>
    <span>@person.Name</span>
    <label>Age:</label>
    <span>@person.Age</span>
    <label>Description:</label>
    <span>@person.Description</span>
    <label>Location:</label>
    <span>@person.Location</span>
</div>

Here is my CSS code:

.section
{
    padding: 5px;
    border-radius: 7px;
    border: 1px solid #aaa;
    margin:0 auto;
}
.section label
{
    display:block;
    font-weight:bold;
    text-align:right;
    width:50%;
    float:left;
}

.section span
 {
     display:block;
     text-align:left;
     width:50%;
     float:right;
 }
 

Although this setup almost works, there are issues with the border collapsing and strange wrapping below the form.

What could be causing these problems?

Thank you in advance.

Answer №1

Applying overflow:hidden; to the .section

The presence of floats causes the elements within .section to appear as if they are "empty" due to floating outside of it. This issue is resolved by using the overflow hack.

An alternative solution would be to eliminate the float property from one of the elements inside the .section.

Answer №2

Initially, I tried implementing @graup's solution but had to make some adjustments because it wasn't compatible with wrapped text.

After some tweaking, this is the CSS code that worked best for me:

.section
{
    width:800px;
    overflow: hidden;
}
.section label
{
    display:block;
    width:400px;
    float:left;
}

.section span
{
    width:400px;
    display: inline-block;
}

Answer №3

Here's another approach:

.section label {
    display: block;
    float: left;
    width: 200px;
}
.section span {
    display: block;
    margin-left: 200px;
}
.section span:after {
    content: "";
    display: block;
    clear: left;
}

This method relies on a fixed width for the left column, but it can also adapt nicely with percentages!

I'm uncertain about whether older versions of IE, like IE7, support the :after selector.

Check out the demo here: http://jsfiddle.net/y4NcC/

For more details and tips on supporting legacy IE browsers, visit:

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

Verify if a personalized angular directive is able to display or conceal an element

I have a custom directive that toggles the visibility of an element based on the value of another service. I attempted to write a test to verify if the directive functions as intended. Initially, I used the ":visible" selector in my test, but it consistent ...

Issue with displaying jqplot in a jQuery page

Currently, I'm utilizing jqmobile pages for switching between various pages in an html5 application. One of these pages features a jqplot chart. Below is the code snippet: <div data-role="page" id="page-two" data-title="Page 2"> &l ...

Revamp and Enhance Your Layout Using Bootstrap Cards

I am working with a bootstrap card and trying to control the visibility of the .task__content part using CSS transforms. Specifically, I want to hide it with transform: scaleY(0) and reveal it on hover over the .task element. However, I am encountering a s ...

What is causing this div to automatically assume a width of 900 pixels?

There is only one specific instance where I have set a div width to 900 px, and it is located further up the DOM within a div called #Ba. This is where the width is being derived from. My intention is for it to simply take on the width of its containing e ...

Unable to output value in console using eventListener

Hey everyone, I'm new to JavaScript and trying to deepen my understanding of it. Currently, I am working on an 8 ball project where I want to display the prediction in the console. However, I keep getting an 'undefined' message. const predi ...

Adding icons to form fields based on the accuracy of the inputs provided

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Assignment 2 - Website Bui ...

I would like for this message to appear periodically following the initial execution

I have developed a unique welcome feature using HTML and CSS that I would like to showcase intermittently. --------------------------- My Desired Outcome --------------------------- Initially, this feature should be triggered once (when a user first acce ...

Creating dynamic CSS in .Net Core using Tag Helpers instead of inline styles

Is there a method to dynamically insert CSS into the stylesheet without using inline HTML with TagHelpers? While SetHtmlContent generates dynamic HTML, is there a similar method for CSS? It's commonly recommended to keep CSS and HTML files separate. ...

"Managing output buffers, styling with CSS, and sending emails with PHP

I am currently using a script to send emails that result in a couple of HTML tables: $from = "example.com <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82efebe7ef83898ce0ede1e9e3e6d2edeee8">[email protected]</a ...

Translate data from two "contact form 7" date fields into JavaScript/jQuery to display the date range between them

NEW UPDATE: I was able to get it working, but the code is a bit messy. If anyone can help me clean it up, I would greatly appreciate it. <div class="column one-fourth" id="date-start">[date* date-start date-format:dd/mm/yy min:today+1days placeholde ...

What is the best way to attach multiple files in PHP using mail.php and mime.php from Pear library?

I am having trouble attaching multiple files. When I try to attach more than one file, only one file gets sent in the email even though I attached multiple files. if(isset($_POST['upload'])){ include('config.php'); include_once ...

Generate a random word using Typed.js

Can Typed.js generate a random word for output? $(function(){ $(".element").typed({ strings: ["Lorem", "Ipsum", "Dolor"], typeSpeed: 0 }); }); The output should be one of the following words. (Lorem / Ipsum / Dolor) ...

Tips for adjusting container wrapping to accommodate smaller window widths in bootstrap 4

Bootstrap 4 is still new to me, and I haven't had much experience working with Bootstrap 3 either. One issue I've encountered is that when I apply the class col-(breakpoint)-(span) to div elements, they don't automatically align in a single ...

Is it possible to implement a delay period or prompt for user confirmation before the transition can be repeated?

When using CSS to code for an image or box to move when hovered over, there can be unexpected results. If the mouse is positioned within the click area at the beginning of the transition but not at the end, the effect may repeat indefinitely and stutter if ...

Elastic video player embedded in a flexbox

I'm having an issue trying to make a responsive YouTube embed work inside a flex container alongside other content. The problem is that the container for the YouTube embed doesn't resize properly and ends up overlapping with other elements. To be ...

The text in the image caption has been drastically reduced along with the size of the image

Currently, I am dealing with the following code: <div style="min-width: 1000px;"> <p> Some text </p> <div class='div_img_left'> <img src="images/sth.png" alt="missing image" style="max-width ...

Demystifying Vertical Alignment: Everything You Need to Know

Struggling with vertical alignments has proven to be more complicated than expected. Despite reading numerous resources on stackexchange, a common understanding of the process remains elusive. After gathering some guidelines, it turns out that vertical-al ...

Renaming file names for Bootstrap 4.5 demonstrations

Trying to utilize a Bootstrap 4.5 example, but running into issues when renaming the HTML or CSS files - it breaks the formatting link for the page. Is there a way to duplicate the original example files and modify the names (such as "my_page.html" or "m ...

Adjust the size of bootstrap card titles to match up with the image on the page

I am facing an issue where the title of the second card is pushing the image down. Is there a way to dynamically resize the card title to align all the images properly? Any help would be appreciated. https://i.sstatic.net/PRRHl.png Bootstrap Card ...

I'm curious, are there any html rendering engines that can display text-based content using curl-php?

When utilizing PHP cURL to interact with webpages, I often find myself needing to use regular expressions if the page contains AJAX and JavaScript elements. Does anyone have any recommendations for rendering HTML pages and extracting the text-based render ...