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

Strange outcomes observed with array created from HTML code

html : <input type="text" name="ps[part1['test']]" value="testing multiple array"> Current result : [ps] => Array ( [part1['test'] => testing multiple array ) Expecting result : [ps] => Array ( ...

What is the best way to determine which button was clicked in Django?

My HTML has two buttons - one for accepting and the other for rejecting requests. I am trying to figure out how to determine which button was pressed along with the email of the applicant (tutor_application.tutor.chalkslateuser.email). Despite multiple a ...

refers to the spacing between elements

I currently have 4 nested divs. My goal is to ensure that the margins between each div are equal, maintaining the same distance from the left edge of the parent div to the first nested div, between each pair of nested div, and from the last nested div to t ...

Having trouble showing table data in Angular

My goal is to showcase data from a table created using Spring-Boot Below is my model.ts: export class Quiz1 { QuestionId?: any; Question?: string; OptionsA?: string; OptionsB?: string; OptionsC?: string; OptionsD?: string;} He ...

The listbox is experiencing display issues when viewed in the Chrome browser

Hey, I'm having an issue with adding background color and border to my Listbox. There seems to be a white strip on the right and bottom of the options. The layout rearranges properly when hovering over each item. <select> <option>H ...

Angular - ui-router states are not being detected

I'm currently working on a Spring and Angular JS web application project. The structure of the project is as follows: app.state.js (function() { 'use strict'; angular .module('ftnApp') .config(stateConfig); stateConfig. ...

How to dynamically change the position of an input field within a form using JavaScript and jQuery

I have a form displayed on my website that looks like this: input a input b input c I am wondering if it is achievable to rearrange the order of inputs using jQuery, like so: input a input c input b However, it is important that the data is still ...

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 ...

Executing a function by click event using the onclick attribute from a file located in the public directory of my project (Next

I'm new to using Next.js and I have a question about how to utilize onclick to execute a function from an external file located in my public folder. Below is my index.js file: import Head from "next/head" import Script from "next/scrip ...

Retrieve the content from an HTML table cell enclosed within a division tag

I am currently facing an issue where I need to extract the value from a specific div in order to proceed with the program. The value that I am trying to retrieve is '12345678'. <td id="TransaccionTesoreriaRetencionItems_NroComprobanteRete ...

Unlocking the JSON array of data in JavaScript: A step-by-step guide

Hey there, I need help extracting an array from a JSON data structure. Here's an example of my JSON object: { Data1 : { Data2 : "hello", Data3 : "hi" }, Data4 : { Data5 : "this is Karan", } } I'm looking ...

The edges of the cube are not joined together

Looking for a 3D transform cube that can freely flip left and right, with the button fitting into it perfectly. I've found the CSS code to achieve this, but when I set the width of the ".cube" below 200px, the borders of the cube don't connect. I ...

Selenium Scrolling: Improving Web Scraping Efficiency with Incomplete Data Extraction

I have been attempting to extract product data from a website that utilizes JavaScript to dynamically render HTML content. Despite using Selenium, implementing scrolling functionality to reach the end of the page, and allowing time for the page to reload, ...

Altering the color of a div based on a specified value condition

I am in need of assistance with a div structure similar to this: <div id="namacoc" tabindex="1" class="filled-text" placeholder="Input your name" contenteditable ></div> <div id="position" tabindex="1" class="filled-text" placeholder="Input ...

Using a Mixin as an Argument in Another Mixin in LESS CSS

Is it possible to pass the declaration of one mixin or style to another mixin as an input parameter? Consider the following example involving animation keyframes. This is how keyframes are typically defined in pure CSS: @-moz-keyframes some-name { fr ...

Use jQuery to trigger a click event when an element is in focus, unless it was clicked to

I am currently developing a website using the MDL framework. One issue I encountered is that there is no default select form element provided. After some research, I found a solution by utilizing a menu component that displays when the input is clicked. Th ...

Is it possible to continuously refresh a DIV element?

After spending the past 4 or 5 hours scouring Stack and various other resources, I am still unable to find a solution to my problem. The issue at hand involves an Iframe within a page that displays 5 lines of information fetched from a database. This info ...

The search for 'post_sharing' with parameters ('',) did not yield any results. No matching patterns were found

Whenever I try to click on the option Share this Post in show_more.html, I encounter an error. show_more.html <p> <a href="{% url 'mains:post_share' post.id %}">Share This Post</a> </p> urls.py path('< ...

Displaying a URL inside a div upon clicking a specified href link in an Angular application

After hours of searching on various search engines, I am still unable to find the solution to my problem. My goal is to display the URL in a div once it has been clicked using $http for dynamic links. Here's an example of what I'm trying to achi ...

Steps to activate overflow on `react-bootstrap` NavDropdown

I'm having a bit of trouble with using react-bootstrap's NavDropdown in my NavBar to display a list of data. Sometimes, the list extends beyond the view and gets cut off at the bottom. I want to add overflow: auto to the list to make it scrollabl ...