Removing an element that is floated inside a parent element with other floated and non-floated siblings in Chrome

Can someone help me with this code issue?

<!doctype html>
<title>Test case</title>
<div style="float: left">
    <div style="float: left;">Short.</div>
    <div></div>
    <div style="overflow: hidden;">ALongText.</div>
</div>

I am facing an unexpected result in Chrome (44.0.2403.107 m), It displays like:

Short.ALon

I thought the parent div would adjust to contain all three children elements:

Short.ALongText.

Alternatively, I expected the third div to be pushed down as it does not fit within the container:

Short.
ALongText.

Any idea why Chrome is cutting off the div?

Please note that the width of the parent element matches the "natural size" of the last child div, excluding the floating child.

Answer №1

The parent Div is not adjusting to accommodate all three children because it is floated to the left. When an element is floated without a specified width, it will take on the maximum width of its child elements. In this case, the last child element has the widest width. I have provided some examples below to demonstrate this concept more clearly. Click here to view the examples

<div style="color:black"><!-- No float-->
<div style="float: left;">Short.</div>
<div></div>
<div style="overflow:hidden">ALongText.</div>
</div>

 <div style="float:left;color:red"><!-- First element has max width -->
<div style="float: left;">ALongText.</div>
<div></div>
<div style="overflow:hidden">Short.</div>
</div>

 <div style="float:left;color:green"><!-- Second element has max width -->
<div style="float: left;">Short.</div>
<div></div>
<div style="overflow:hidden">ALongText.</div>
</div>

Answer №2

You have the option to remove the empty DIV

<title>Test case</title>
<div style="float: left">
    <div style="float: left;">Short.</div>
    <div style="overflow: hidden;">ALongText.</div>
</div>

Alternatively, you can add 'float:left' to the same DIV

<div style="float: left">
    <div style="float: left;">Short.</div>
    <div style="float: left;"></div>
    <div style="overflow: hidden;">ALongText.</div>
</div>

Answer №3

Citing from http://www.w3.org/TR/CSS21/visuren.html#block-formatting:

Floats and block boxes with 'overflow' other than 'visible' create new block formatting contexts for their contents.

Within a block formatting context, boxes are arranged one after another vertically from the top of a containing block. The vertical spacing between sibling boxes is controlled by the 'margin' properties. [...]

Within a block formatting context, each box's left outer edge aligns with the left edge of the containing block [...]. This rule applies even when floats are present [...], unless the box establishes a new block formatting context (in which case the box itself may become narrower due to the floats).

The third box creates a new context, so according to the linked section:

[...] an element in the normal flow that establishes a new block formatting context (such as an element with 'overflow' other than 'visible') should not overlap the margin box of any floats in the same block formatting context. If necessary, implementations should adjust the placement of the element to avoid overlap with preceding floats, but may position it alongside the floats if there is enough space. They have the option to reduce the width of the element's border box contrary to what is specified in section 10.3.3. CSS2 does not specify the conditions under which a UA can place the element next to the float or how much the element can be narrowed.

In this particular scenario, Chrome chose to narrow the element instead of clearing it. One way to influence Chrome's behavior is by removing the second div or adjusting the padding of the third element.

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

When setting the margin to auto, it perfectly centers elements on servers and IE, but on Chrome off the server, it appears to be aligned to the

There seems to be an issue with the display of my page in Chrome - it starts at the middle and ends on the right side. However, when I view it on my server or try it on Internet Explorer 11, it appears centered. Why is that? This problem only arose after ...

Difficulty maintaining consistent line widths while setting up a CSS border grid on a table

What is the most effective method for creating a table with a 1px grid (internal and external)? Could it be possible that Chrome and Edge have issues with borders in certain scenarios? On this Codeply example, the standard approach is taken - setting bord ...

Just a snippet of a webpage shown

My website focuses on online magazines. I'm looking for a way to selectively display specific parts of articles that are in regular HTML format with a global CSS stylesheet applied. There are no images, only text. How can I extract certain segments of ...

Creating an error message display function using HTML and JavaScript when incorrect input is detected

I am having trouble creating an HTML5 and JS program that will show an error message if the user input is empty. Despite my efforts, I can't seem to get the "Error Message" to display. As a beginner, the video tutorial I'm following doesn't ...

What is the method for a HTML button to trigger the execution of a Selenium (Java) code located in a remote location through a

I need to run a Selenium Code written in Java from a NAS (Network Attached Storage) or another connected machine. My goal is to create a web page with a button that, when clicked, triggers the execution of the Selenium script located on the connected mac ...

A step-by-step guide on injecting HTML code dynamically within a Django application

I have a project that involves creating an online encyclopedia. To achieve this, I must generate a webpage for each entry file written in Markdown. Converting Markdown to HTML is essential before uploading them to the site. Instead of relying on external l ...

Incorporate a curved progress line into the progress bar

Currently, I am working on creating a customized progress bar: .create-progress-bar { padding: 0 !important; margin: 25px 0; display: flex; } .create-progress-bar li { display: flex; flex-direction: column; list-style-type: none ...

Locking Bootstrap 4.3 Navbar at the Page's Summit with a See-Through Effect

I have extensively researched and searched online for information before reaching out here. I hope my question is not redundant. My challenge lies in creating a static navbar at the top of my Bootstrap 4.3 page. Despite multiple attempts, it seems to elud ...

Fixing the bug in the circle text animation

I followed the instructions in this tutorial and implemented the following code: Splitting(); * { margin: 0; padding: 0; box-sizing: border-box; font-family: monospace; } body { display: flex; justify-content: center; align-items: center; ...

The header that sticks on scroll is annoyingly blocking the content

I managed to create a sticky on-scroll header/navigation bar successfully. Here is how it looks now However, I encountered an issue. When it reaches the top, it automatically 'covers' the top part of my content, which has text on it, and I don& ...

I'm looking to redirect a 404 page in my PHP code - where is the best place to do so?

I found the correct code for redirecting 404 errors when a page is not found during redirection. However, I am unsure of where to implement this code in my PHP file. Should I place it within a specific tag on my home page? When I tried placing it at the ...

Creating a Date Computation Tool that Adds Additional Days to a Given Date

I have a challenge where Date 0 represents the start date, Date 1 is the result date after adding a certain number of days (NDays). I am looking for help in JavaScript to calculate Date0 + NDays = Date1. Please assist me as my coding knowledge is quite l ...

Only the initial external CSS ID is functional, while the second one is inactive

I am facing an issue with my table where I have applied an external CSS that uses alternating shaded rows. My aim is to have specific table cells with different background and font colors - one cell with a green background and red font, and another with a ...

What is the best way to implement the Active list element feature in my menu bar?

The current list element is : <li class="nav__item"><a class="nav__link nav__link--active " href="#"... The standard list element is: <li class="nav__item"><a class="nav__link " href=&quo ...

When clicking on an HTML link pointing to a cloud, it does not open in the same frame but instead opens in a new window

I am currently working on an HTML5 application that features a screen divided into two parts. The left part of the screen contains a few links, and upon clicking one of these links, the resulting content should open in the right side frame within the same ...

I am having trouble with my image not resizing properly within the flexbox when using height and max-height properties

In my flexbox setup, I have two columns, each taking up 50% of the width. My goal is to insert a large square image in the right column, with its height being determined by the contents' height in the left column. (Additional content will be added to ...

Trouble displaying certain HTML code and its output on the browser?

I am in the process of developing a user profile page. I have designed a form with various fields, but the code within the section is not displaying correctly in the browser. I attempted to inspect the elements, but the code within that section remains inv ...

What are the best ways to integrate jQuery into a React application?

I am in the process of developing a responsive menu for my react app, but I'm facing some challenges as a newcomer to react. In order to achieve the desired functionality, I am trying to incorporate jQuery for menu toggling. However, when I attempt to ...

Does Firebase only send a single input value instead of a Span?

I am currently facing an issue with pushing data into my firebase database. I have successfully incorporated one user input field into the process. However, now I am attempting to include a span value in my database, which changes based on a slider that co ...

IE11 not running Angular code inline as expected

Currently in the process of developing an AngularJS application, I came across a strange issue that I have never encountered before. It may be a simple fix, but I am unsure of the exact terminology to search for the right solution, so I apologize in advanc ...