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

Issues arise when using the "placeholder" attribute within a <p:inputText tag while the inputText is devoid of any value

A current project involves implementing JSF Mojarra 2.3.9.SP02 alongside PrimeFaces 7.0 on Wildfly 17 using the Sapphire template provided by PrimeFaces. However, I've encountered a significant issue with a specific <p:inputText element within my f ...

What could be causing the malfunction of this Bootstrap button dropdown?

Initially, I attempted using regular HTML for the dropdown button but encountered issues. As a result, I switched to jsfiddle to troubleshoot. Despite my efforts, the dropdown feature still refused to work. If you'd like to take a closer look, here&a ...

VUE component disregarding CSS styles

Check out my awesome VUE component below: <template> <div> <div class="bottom-footer"> {{msg}} </div> </div> </template> <script> export default { ...

hide the scroll button when at the top of the page and hide it when at the bottom

Looking to create a vertical scroll that hides the up button when at the top and hides the down button when at the bottom? Jquery can help with this, but sometimes it's tricky to get it just right. Take a look at an example here. Below is the code sn ...

Troubleshooting mat-accordion title problem on mobile devices

Despite reading numerous blogs and conducting extensive searches on Google, I am still unable to resolve the CSS issue related to Angular material below. Your assistance in fixing this problem would be greatly appreciated. Desktop view: https://i.stack.im ...

What are some techniques for disguising text on a website to give the illusion of being in English but is actually impossible to read

I am seeking a way to obscure text by rendering it completely unintelligible. While this task is easily accomplished in handwriting, I require a method for achieving this on a webpage using the Verdana font. One idea I had was to create a Verdana-style f ...

Stop text from being selected across all browsers in Firefox

My current CSS includes: *{ -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } If you visit http://jsfiddle.net/KyF5x/ and click be ...

Troubleshooting Problem: Adjusting the hover border color for TextField in ReactJS Material UI

I'm having trouble setting the hover border color of a TextField to a theme variable. It seems that in order for the hover borderColor to work, it needs the "!important" tag. However, I am unsure of how to incorporate this into the theme variable. Th ...

Tips for concealing the final click (add) tab after it has been clicked and revealing the hidden (add) button when the user clicks on the remove button

I have created a form where users can add tests. Everything is working smoothly but I want the previous "Add Another Test" field to be removed when the user clicks on it and a new field to be shown. Everything is running well, but the issue is that when t ...

Auto play feature malfunctioning in Onsen-UI Carousel attribute settings

When utilizing onsen UI in my mobile web application, I encountered an issue with the autoplay property not functioning properly within the carousel. <ons-page> <ons-toolbar> <div class="left"> <ons-toolbar-button on ...

Troubleshooting: Django update causing issues with CSS background image display

Recently, I made an upgrade to my Django 1.10 (Python 3.5) app to Django 1.11 (Python 3.6). Most of the functionalities are still intact, however, I am facing a peculiar issue where my CSS background images are no longer rendering (even though they work fi ...

Tips for implementing bslib package pop up window within a table displayed using rhandsontable in R Shiny

I am trying to implement the template from Laurent's answer on R Shiny - Popup window when hovering over icon in my code below, which involves integrating a popup window into a rhandsontable table. My goal is to display the popup window as depicted in ...

What is the best way to apply "display: none;" on a span element nested within a title attribute?

I am utilizing the social-share-button gem to share blog posts on social media. The website has been internationalized, making it bilingual (English and German). Everything is functioning correctly, but I encounter an issue with the social share buttons wh ...

Can anyone assist me with this HTML/jQuery code?

Despite my efforts, I've been unable to achieve success. The challenge I'm facing is with duplicating controls for adding images. I have a button and a div where the user can choose an image by clicking the button. The selected image appears in ...

Text "movement/vibration" in screen when adjusting dimensions in Firefox for Mac

I am currently involved in a project centered around showcasing a variety of fonts to users for them to experiment with, allowing them to adjust size and spacing using sliders. While the functionality works seamlessly on Chrome and Safari, a peculiar issu ...

Creating responsive data tables in HTML

My e-shop features a product description block using the code snippet below. While it looks great on larger screens, such as a 17" desktop monitor, it appears poorly on smaller smartphone screens. Friends have suggested that I learn Bootstrap/Flexbox and ...

Margin ambiguity in a vue.js application

I am facing an issue with my Vue.JS project setup. I want the App.vue to occupy the entire page and have different routes displayed within App.vue using router-view. But, when I try to add a margin to the content of my Game component, the margin seems to ...

Discover more in React about using ellipses (...) with dangerouslySetInnerHTML

What is the best method for limiting the number of HTML lines retrieved using dangerouslySetInnerHTML in a React application and creating a '... Read More' expander for it? I attempted to use react-lines-ellipsis, but unfortunately the 'tex ...

After refreshing the page, users are redirected back to the login page

On my website, I created a main page and an index.html page that redirects to the main page through a login panel. The issue I am encountering is that when I log in successfully on the main page and then refresh the page, it takes me back to the login pag ...

The specified font-size in my CSS is being ignored by Chrome

I'm a beginner in the world of html/css and was tasked with creating a html webpage. I set a specific font-size for my webpage, only to be surprised when my browser (Google Chrome) adjusted it on its own. Even though I specified the font-size as 14px, ...