Avoiding Overlapping DIVs: Tips to Keep Your Elements in Place

Instead of using table-based layout, I have decided to go with a DIV-based layout in order to streamline the markup and improve page load speed. However, as I am not an expert in CSS, I find it quite challenging. Below are the CSS classes I am currently using to create rows that mimic a table structure, with one column for labels and another for textboxes.

.FormItem
{
    margin-left: auto;
    margin-right: auto;
    width: 604px;
    min-height: 36px;
}
.ItemLabel
{
    float: left;
    width: 120px;
    padding: 3px 1px 1px 1px;
    text-align: right;
}
.ItemTextBox
{
    float: right;
    width: 480px;
    padding: 1px 1px 1px 1px;
    text-align: left;
}

,

<div class="FormItem">
    <div class="ItemLabel">
            <asp:Label ID="lblName" runat="server" Text="Name :"></asp:Label>
        </div>
        <div class="ItemTextBox">
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            <p><span>User Name</span></p>
        </div>
</div>
<div class="FormItem">
        <div class="ItemLabel">
            <asp:Label ID="lblComments" runat="server" Text="Comments :"></asp:Label>
        </div>
        <div class="ItemTextBox">
        <asp:TextBox ID="txtComments" runat="server"></asp:TextBox>
            <p><span>(optional)Comments</span></p>
        </div>
</div>

If the height of ItemData DIVs exceeds the min-height set for FormItem DIVs, the alignment between ItemText and ItemData may be compromised. Here is an example of the issue:

<div class="FormItem">
    <div class="ItemLabel">
        <asp:Label ID="lblName" runat="server" Text="Name :"></asp:Label>
    </div>
    <div class="ItemTextBox">
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <p><span>User Name</span></p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
    </div>
</div>
<div class="FormLabel">
    <div class="ItemText">
        <asp:Label ID="lblComments" runat="server" Text="Comments :"></asp:Label>
    </div>
    <div class="ItemTextBox">
    <asp:TextBox ID="txtComments" runat="server"></asp:TextBox>
        <p><span>(optional)Comments</span></p>
    </div>
</div>

I have experimented with various CSS attributes such as position, float, clear, but have been unable to resolve the issue. Any assistance would be highly appreciated.

Answer №1

Upon being floated, nodes are removed from the standard layout flow, resulting in their lack of awareness towards other nodes (and they will not reduce in size when colliding).

Answer №2

Take a look at this related design:

Sleek column-based form layout

Answer №3

Creating a two-column layout offers a variety of techniques and pitfalls to be aware of.

For a basic approach when dealing with simple layouts resembling tables, you can consider the following structure:

<div class="outer">
  <div class="row"><label>Name:</label><div>Content</div></div>
  <div class="row"><label>Name2:</label><div>Content</div></div>
</div>

You have the option to style it like this:

.outer { width: 800px;}
.outer .row { float: left; width: 100%; overflow: visible;}
.outer .row label { width: 100px; float: left; }
.outer .row div { width: 700px; float: left; margin: 0;}

This setup results in a two-column layout, positioning the labels on the left side and the content inside div elements on the right. It's important for both elements to float left to maintain consistent alignment regardless of container width.

To achieve a more flexible layout that adjusts smoothly during resizing, advanced techniques are necessary. Trying to replicate table-like rendering without using an actual table element may require utilizing properties like display: table, display: table-cell, but bear in mind they are not supported in IE 6 or 7.

In scenarios where you aim for more fluidity, replacing div with a p might be beneficial. The key lies in grouping all left-hand-side content under one container to ensure proper alignment with corresponding labels.

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

Using various alignment options for images on mobile devices

I've designed a theme with two columns, one for text and the other for images. I've structured it in HTML to display as: Text - Img Img - Text Text - Img Img - Text Text - Img Img - Text However, for mobile responsiveness, I want it to display ...

Tips for creating consistent spacing between elements using Tailwind CSS version 3

I am currently utilizing Tailwind CSS in conjunction with next.js for my project. I have a total of 8 images, each varying in size, and my goal is to display 4 images per row on medium and large devices, while displaying 2 images per row on small devices. ...

Utilize TypoScript to inject HeaderData prior to implementing Style Definitions

My goal is to include my Google Tag Manager script before the style definitions in order to optimize the loading order and prioritize font-family rendering. Currently, I achieve this by: page.includeCSS { file1 = fileadmin/templates/css/bootstrap.css page. ...

Color picker can be utilized as an HTML input element by following these steps

After trying out various color pickers, I was not satisfied with their performance until I stumbled upon Spectrum - The No Hassle jQuery Colorpicker. It perfectly met my requirements. <html> <head> <meta http-equiv="content-type" content="t ...

The URL "http://packagist.org/p/provider-3.json" was unable to be retrieved due to a bad request error (HTTP/1.1 400 Bad Request)

Encountering a 400 bad request issue when trying to connect over HTTP, despite the package only being installable via HTTP. Attempting to override in composer.json to force HTTPS as suggested by others for a workaround, but that solution doesn't seem ...

Utilizing vue-router to create two separate navigation bars where only one link is highlighted as active

In my setup, I have implemented two sections with navigation structured as follows: <ul class="navigation-list"> <li v-for="(route, index) in navRoutes"> <router-link :to="route.path"> <span>{{ route.name }}</span> ...

Is it possible to trigger a reflow prior to initiating a lengthy JavaScript operation?

Ready to face the criticism, I understand that this question has been asked many times before, and I am aware that there are likely more efficient ways to achieve what I'm trying to do... In a JavaScript function, I have a process that can take up to ...

I'm not sure what the issue is with my navbar animation not functioning properly

My navbar has been styled with a transition that should ease in and out, but for some reason it's not working as expected. Even when I hover over the li a elements, the ease-in-out animation is not displaying, and I'm struggling to figure out wha ...

I found myself pondering the method to achieve the slightly blue tinted box that sits behind the image

Can you assist me in achieving this specific look? I have my MUI app bar and home page set up, but I'm unsure how to create the blue-ish box behind the image. Should I place the container within my app bar or integrate it into my homepage file? Additi ...

What is the best way to effectively incorporate Ruby into the CSS attribute of a HAML %li element?

Greetings everyone, I am new to the world of development and seeking guidance from experienced individuals. I have been trying to solve a coding issue for quite some time now. I am currently enrolled in a programming course at Code Academy based in Chicago ...

CSS hover effect ceases to function after the button has been clicked once

I am facing a dilemma with styling that I can't seem to resolve. There is a basic toggle feature on my page with two options -- the user can select either Toggle1 or Toggle2, resulting in different data being displayed dynamically based on the active ...

A guide on configuring Flexbox to consistently display three columns

For the website I'm working on, I've utilized Flexbox to properly align the items. Within the div .main, I have organized the content in separate divs. These are the CSS properties that I've applied: .main{ margin: 0 auto; /*max-height: ...

Unable to show the table row color using Angular in Internet Explorer

I've customized a table by changing the row color based on a property value, and I'm using ng-repeat to display the rows. Here's my table: <table class="tab table span6 tabhover" style="margin:0;" > <thead> ...

Adjust the appearance of elements depending on whether the user has activated dark mode or

What is the most efficient way to implement both dark and light themes in my Vue app? One option is to create a 'dark.scss' file and use the '!important' property to override styles defined in components. Another option is to utilize pr ...

What does the term "root element" refer to in the Material-UI documentation?

I am finding the terminology used in material ui confusing and would appreciate if someone could clarify it for me. For example, let's consider this information from https://material-ui.com/guides/api/: Spread Undocumented properties supplied ar ...

Challenges with looping in Jquery animations

I've been working on an animation function that I want to run in a loop. So far, I've managed to get it to work for one iteration, but I'm struggling to figure out how to repeat the loop a specific number of times. Below is the code for my a ...

Achieving a tidy layout with child divs inside a parent div

I'm struggling to figure out how to arrange objects with margins inside a parent div. Initially, I thought using float would do the trick, but I quickly realized it wasn't the solution. This results in an unsightly amount of space. The divs that ...

Rearrange two elements by flipping their positions using CSS

I'm working with the following div that contains an input and a label: <div class="js-form-item "> <input type="checkbox" id="checkboxes-11" class="type-checkboxes form-checkbox"> <label for="option-a-checkboxes-11" class=" ...

Step-by-step guide on creating a draggable navigation bar:

I am encountering an issue creating a droppable menu. I am currently working on Menu 1 but struggling to figure out how to make the drop-down menus appear on the right side. <link href='http://fonts.googleapis.com/css?family=Oswald:300,400' r ...

Unable to close Bootstrap sidebar menu on mobile devices

I implemented a sidebar menu for mobile that opens when you click on the hamburger icon, with a dark overlay covering the body. The issue I encountered is that on mobile devices, the visibility of the menu does not change when clicked outside of it. It wor ...