What causes divs to be interspersed among other divs when Float is enabled?

I am looking to create a layout for 4 sections, similar to a table, utilizing only divs and CSS. The intended output is this:

Logo-----------Info
Business-------Client

I initially thought it would be simple by using the Float property. However, when I set that property to left and right for the Logo and Info, the Business and Client divs end up appearing between them rather than below them as desired.

Logo----Business----Client---Info

You can find the code at: http://jsfiddle.net/YMh3C/304/

While I want the issue with the code fixed, my main goal is to comprehend why this behavior occurs.

#logo {
  float: left;
  text-align: center;
}

#info {
  float: right;
  text-align: center;
}

#business {
  float: left;
  text-align: center;
}

#client {
  float: right;
  text-align: center;
}
<section>
  <div id="logo">
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
  </div>
  <div id="info">
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
  </div>
  <div id="business">
    <div>{{ temp_business1 }}</div>
    <div>{{ temp_business2 }}</div>
    <div>{{ temp_business3 }}</div>
    <div>{{ temp_business4 }}</div>
    <div>{{ temp_business5 }}</div>
    <div>{{ temp_numerodefactura }}</div>
    <div>{{ temp_fechadefactura }}</div>
  </div>
  <div id="client">
    <div>{{ temp_client1 }}</div>
    <div>{{ temp_client2 }}</div>
    <div>{{ temp_client3 }}</div>
    <div>{{ temp_client4 }}</div>
    <div>{{ temp_client5 }}</div>
  </div>
</section>

Answer №1

To fix the layout issue, you can either insert a div with clear:both right after the info div, or apply the clear:both CSS property to the #business selector.

#logo {
  float: left;
  text-align: center;
}

#info {
  float: right;
  text-align: center;      
}

#business {
  float: left;
  text-align: center;
  clear:both;
}

#client {
  float: right;
  text-align: center;
}
.clear {
  clear:both;
}
<section>
  <div id="logo">
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
  </div>
  <div id="info">
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
  </div>
  <div class="clear"></div>
  <div id="business">
    <div>{{ temp_business1 }}</div>
    <div>{{ temp_business2 }}</div>
    <div>{{ temp_business3 }}</div>
    <div>{{ temp_business4 }}</div>
    <div>{{ temp_business5 }}</div>
    <div>{{ temp_numerodefactura }}</div>
    <div>{{ temp_fechadefactura }}</div>
  </div>
  <div id="client">
    <div>{{ temp_client1 }}</div>
    <div>{{ temp_client2 }}</div>
    <div>{{ temp_client3 }}</div>
    <div>{{ temp_client4 }}</div>
    <div>{{ temp_client5 }}</div>
  </div>
</section>

Answer №2

When you are using the `float` property, it is important to include the `clear: both;` property to ensure that the next section appears on a new line.

To learn more about this topic, please visit https://css-tricks.com/all-about-floats/

In the meantime, you can add an empty div with `clear: both;` after the element with the ID of `info`.

Answer №3

If you want to make adjustments to your current css, a simple solution is to include the property clear: left; in the styling for #business

By doing this, you will ensure that the next floating element appears below the previous one on the left side. It will also affect the positioning of the client div.

#business {
  clear: left;
  float: left;
  text-align: center;
}

Once you have implemented this change, feel free to experiment with padding and margins to achieve your desired layout.

Answer №4

Here is everything you need, try to steer clear of using float and opt for table cell instead

section{
  display:table;
  width:100%;
}
section > div{
  width: 50%;
  text-align: center;
  display: inline-block;
}
<section>
  <div id="logo">
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
  </div>
  <div id="info">
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
  </div>
  <div id="business">
    <div>{{ temp_business1 }}</div>
    <div>{{ temp_business2 }}</div>
    <div>{{ temp_business3 }}</div>
    <div>{{ temp_business4 }}</div>
    <div>{{ temp_business5 }}</div>
    <div>{{ temp_numerodefactura }}</div>
    <div>{{ temp_fechadefactura }}</div>
  </div>
  <div id="client">
    <div>{{ temp_client1 }}</div>
    <div>{{ temp_client2 }}</div>
    <div>{{ temp_client3 }}</div>
    <div>{{ temp_client4 }}</div>
    <div>{{ temp_client5 }}</div>
  </div>
</section>

Answer №5

To achieve the desired layout, you can set a fixed width for your main divs and float them to the left. In this example, I have used a 50% width for each.

#logo {
  float: left;
  text-align: center;
  width:50%;
}

#info {
  float: right;
  text-align: center;
  width:50%;
}

#business {
  float: left;
  text-align: center;
  width:50%;
}

#client {
  float: right;
  text-align: center;
  width:50%;
}
<section>
  <div id="logo">
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
    <div>{{ Logo }}</div>
  </div>
  <div id="info">
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
    <div>{{ Info }}</div>
  </div>
  <div id="business">
    <div>{{ temp_business1 }}</div>
    <div>{{ temp_business2 }}</div>
    <div>{{ temp_business3 }}</div>
    <div>{{ temp_business4 }}</div>
    <div>{{ temp_business5 }}</div>
    <div>{{ temp_numerodefactura }}</div>
    <div>{{ temp_fechadefactura }}</div>
  </div>
  <div id="client">
    <div>{{ temp_client1 }}</div>
    <div>{{ temp_client2 }}</div>
    <div>{{ temp_client3 }}</div>
    <div>{{ temp_client4 }}</div>
    <div>{{ temp_client5 }}</div>
  </div>
</section>

Answer №6

Understanding why this phenomenon occurs involves delving into the mechanics of how float operates. Picture a box with magnets attached - a "left" polar magnet on the left side and a "right" polar magnet on the right side. Additionally, there is a force pulling all the blocks upwards from the top of the box. When you introduce a "left" element into the empty box, it immediately moves to the top-left corner. Then, if you add a "right" element, it goes straight to the upper right corner. If you then add another small enough "left" element that can fit between the previous two, it will stick precisely in that space. This behavior occurs because float elements are naturally drawn towards borders or other adjacent elements.

For a more in-depth scientific explanation, direct your attention to the Float specification.

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

Customize your Jquery UI Calendar with Changing Background Images for Each Month!

How can I change the background of jQuery UI datepicker based on the selected month? I have created 12 classes, each representing a month, and the selected month already has the background. I tried using "onChangeMonthYear: function(year, month, inst) { ...

Applying Bootstrap Style to an Inline Select Element: A Step-by-Step Guide

Currently working on a page layout using Thymeleaf and Bootstrap 5. I have divided the page into two parts, with this section behaving as desired: <main role="main" class="pb-3"> <br> <p>Post office ex ...

Styling is lost in FancyBox popup when loading Partial View

I've been attempting to incorporate a partial view into my MVC project using fancybox. It seems to be loading the content correctly, mostly anyway, as it tends to cut off part of the page and loses all styling from the view upon loading. Even after i ...

The header menu is not appearing on the gray bar in Internet Explorer

Having some issues with IE8, specifically on the website . The website header is not displaying correctly in IE8, but it works fine in Chrome and Firefox. Another issue is that the white area of the site is not 960px width as intended... ...

How to enable the Copy to Clipboard feature for multiple buttons and transition from using an ID to a class identifier

Can someone please assist me? I have a copy to clipboard function that works well for IDs and a single button on my website. However, I need to modify it to work for multiple buttons and values with a class identifier. Unfortunately, I am unsure how to mak ...

Styling containers with Pandoc

Currently, I am utilizing Pandoc for the purpose of transforming Pandoc Markdown documents into HTML5 documents. Within my md input, I incorporate custom divs by employing a special Pandoc syntax, like so: ::: Resources A nice document Informative website ...

Automatically add a table row at the bottom displaying the overall calculation

I have data to display in an HTML table. The data is coming from a third-party server (PHP) using AJAX requests. The data is currently being displayed effectively with the following code: <table id="tbl-product"> <tr> < ...

Creating sequential hover animations with CSS in HTML

I have a total of four divs that I want to hover continuously, one after the other. Here is the code I am currently using: #rij1 .content-overlayz, #rij1 .content-details { animation-duration: 2s; animation-name: stepped-pulse; animation-iterati ...

Choose the immediate sibling located right after a specific element

My goal is to have the second paragraph following a h1 element display a dropcap, with the first being reserved for the author and date. Although I've tried using different CSS combinations like h1 + p::first-letter {}, it only affects the first para ...

The functionality of jQuery field validation is impaired when interacting with Chrome's autofill feature

Despite adding the autocomplete="off" attribute to both the form and inputs using html5, I'm still unable to prevent Chrome autofill from interfering with the form submission process. In addition to this issue, I have implemented a basic jQuery field ...

ReactJS encountering an invalid dropzone element

Encountering the error "invalid dropzone element". I added var Dropzone = require('react-dropzone'); to my webpack.config.js file in hopes of activating the Dropzone element. This is what is included in my JavaScript file, copied from a Gith ...

How can we prevent the HTML escaping of text children in React.createElement?

Let's examine the call to React.createElement: React.createElement('span', null, null, ['&gt;&lt;',]) When executing this code, React will escape the characters &gt; and &lt;. Is there a method to prevent these sp ...

Arrange two divs with a full width of 100% next to each other

Is there a way to create two divs, each taking up 100% of the page width and side by side within a wrapper with overflow:hidden? How can I achieve this? I attempted using inline-block, but it didn't produce the desired outcome. Similarly, when I tri ...

Can you customize the first element within a span tag using a for loop?

I am having an issue where only the last element in a span is being set within a for loop. The code is functioning correctly, but I want to ensure that the first element is the one set as cust_name. success : function(data) { co ...

One-Time Age Verification Popup Requirement

Hi there! I'm facing a challenge with an age verification pop up on my webpage. Currently, the pop up appears on every page a user lands on, but I only want it to show on their first visit. I've tried using cookies to achieve this but haven' ...

Guide to making a Material Design Radial effect animation

I am looking to create a unique toolbar effect by following the material design radial reaction choreography guideline. https://i.stack.imgur.com/6oB8r.gif I want to achieve this using an angular 2 transition, but I need some guidance on how to implement ...

Is there a way to adjust the position of the scrolling bar to be closer to the post area?

I was exploring this page (http://noahsdad.com/state-fair-texas-2011/) and noticed a scrolling sidebar to the left of the content area. I'm looking to adjust it so that it is closer to the content area. Any suggestions on how to achieve this? Thank y ...

Unable to open Google Maps link within React application

I've set up a conditional link based on location, using the following code: <a href={`https://maps.google.com/maps?q=${delivery_branch.latitude},${delivery_branch.longitude}`} target={"_blank"} >{`${delivery_branch.street}, ${d ...

Dynamic input fields with autocomplete functionality

I have attempted to implement the solutions provided elsewhere on this specific issue, but unfortunately they do not seem to be working for me. It is highly probable that I am making some mistake in my approach... My main objective is to achieve what is me ...

If I create an App with Phonegap, HTML5, and Websql, what happens to the app's data when the user restarts their mobile device?

My application utilizes HTML5's WebSQL to store temporary notes and bookmarks, which will later be synchronized with the server. I am curious whether data stored in WebSQL will be lost upon mobile restart or cache clear, similar to how browsers on ph ...