What is the significance of the statement "does it fit or are there no more floats available" in the W3C specifications for floating elements?

.container {
  width: 500px;
}

.box1 {
  display: inline-block;
  width: 50%;
  height: 100px;
  background: red;
}

.box2 {
  float: left;
  width: 50%;
  height: 50px;
  background: yellow;
}

.box3 {
  float: left;
  width: 50%;
  height: 50px;
  background: red;
}
<div class="container">
  <div class="box1"></div>
  <div class="box2"></div>
  <div class="box3"></div>
</div>

Link to JSFiddle

Initially, I assumed that box3 would align just below box2. However, it ended up being positioned much further down. After researching in the W3C specifications, I'm still unclear about the implications.

W3C Floating Elements Reference

If there is not enough horizontal room for the float, it is shifted downward until either it fits or there are no more floats present.

Specifically, I am puzzled by the statement "either it fits or there are no more floats present."

Answer №1

https://i.sstatic.net/72CJR.png

To grasp this concept, it is essential to consider both the excerpt you referenced and the paragraph that comes before it. Together, they articulate:

A floated box is moved to the left or right until its outer edge touches the containing block edge or another float's outer edge. If there is a line box present, the top outer edge of the floated box aligns with the current line box's top.

If there isn't sufficient horizontal space for the float, it is shifted downwards until it fits or no other floats are remaining.

This essentially means:

If there is a line box ...

Initially, the first inline-block image creates a line box equivalent to the image's height vertically aligned with the line box's strut (margin just slightly taller than the image itself). Due to other elements within the block formatting context (the floats), the line box gets enveloped in an anonymous block box.

Hence, the first floated image snugly fits alongside, aligning its top with the top of that line box.

However, accommodating a second floated image while maintaining alignment with the top of the line box and being parallel to the inline-block image poses a challenge. Consequently, the second floated image must be positioned after the line box and the encapsulating anonymous block box. Since there is no subsequent line box,

it is shifted downward until it fits ...

In this scenario within a block formatting context, placing an item after a block box implies immediately following it on the vertical axis. The spot where it fits is chosen for placement.

... or there are no more floats present.

Your example doesn’t engage with this aspect. This provision accounts for instances where a float's width may exceed 100% of its containing block’s width. In such cases, the float cannot fit alongside any preceding items and is thus positioned directly beneath the last floating 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

The height attribute in HTML tables fails to restrict height in Firefox

Is there a way to restrict the height of a table while still being able to scroll through the hidden elements? I've tried a few methods but none seem to work as intended: http://www.example.com/code/example table.ex1 { table-layout: auto; h ...

Is there a way to condense the row navigation links into a dropdown menu button if the browser window size decreases?

I've noticed that on many websites, when you narrow the width of the browser window, the row of navigation links in the top navigation bar collapses into a dropdown menu button to prevent overflow. How can I implement this feature on my website? Righ ...

blurred image effect on bootstrap card hover

My attempt to create a blurred image effect on a bootstrap card hover is not working as expected. The blur effect works fine without the hover. Here is the code I have been using: .card-img { transition: all 1s ease; -webkit-filter: blur(0px); ...

Set the background image based on the attribute

I am facing a challenge with setting different background images to multiple divs. My approach involves using an attribute that contains the path to the specific image a div needs to utilize as its background. However, I'm encountering difficulties wh ...

Unable to accommodate data within the fixed width confines of the GridView cell

I am having a column in my table with lengthy text that requires scrolling to see the end. Is there a way to adjust the content to fit within a fixed-width cell? Here is the UI setup for the GridView: <asp:GridView ID="GridView1" runat="server" OnPag ...

What is the proper way to showcase the hover effect on a nested ul list item in the DOM?

In an attempt to create a menu, I have written the following code: var sheet_nav = document.createElement('style'); sheet_nav.innerHTML = "nav{ margin: 100px auto; text-align: center;}"; document.head.appendChild(sheet_nav); var sheet_nav_ul = ...

What could be causing the unexpected behavior of the flex property?

Check out the code snippet below: @import url("https://fonts.googleapis.com/css2?family=Inter&display=swap"); @import url("https://fonts.googleapis.com/css2?family=Lexend+Deca&display=swap"); * { box-sizing: border-box; margin: 0; padding: ...

If the user decides to change their answer, the current line between the two DIVs will be removed

After clicking on two DIVs, I created two lines. Now, I am facing an issue with resetting the unwanted line when changing my answer. To reset the line, you can refer to the code snippet below: var lastSelection; ...

Arranging two classes in close proximity

I need help aligning two classes that are stacked on top of each other to be displayed side by side. Below is a screenshot of the two classes: https://i.sstatic.net/V0tLL.png The right class is .testm_boxes1, and the left class is .testm_boxes2. Here is ...

Is the hover effect malfunctioning, even though the correct style has been applied?

I'm currently working on a simple design, but I've encountered an issue. When I hover over the number blocks (1, 2, etc.), my hover effect doesn't seem to work. My intention is to hover over the list item and change the entire digit text fro ...

I am attempting to use ajax to navigate to the next page, but I am encountering some issues with

I'm trying to figure out how to use jQuery ajax to redirect to the next page. I'm new to jQuery and ajax, so I'm struggling with the syntax. Any help would be greatly appreciated. I've searched for information on this topic, but nothin ...

Can jqplot be used to create this particular graph?

Can the graph be created using jqplot as shown below? The closest graph type is stacked, but there are issues with coloring and point labels. This jsfiddle showcases my attempt to create a bar chart with its legend and point labels. Is it possible to achi ...

Does Vue3 support importing an HTML file containing components into a single file component (SFC)?

I am working on a Form-Component (SFC) that is supposed to import an HTML file containing Vue components. Form-Component <template src="../../../views/form-settings.html"></template> <script setup> import Button from "./. ...

Tips for containing `overflow` within a flexbox container

I've organized my body space into three sections: header, content, and footer. To achieve a sticky footer effect at the bottom, I used the flex property in my layout. However, I'm struggling to add a scrollbar to my main container box. I've ...

Display a tooltip upon the page's initial load and automatically hide it afterwards

On page load, I want to display a tooltip for a few seconds or hide it when another tooltip is hovered over. I have been trying to achieve this but because my tooltips are nested in multiple divs due to using an Elementor Addon, I am facing some confusion ...

If other options fail, Else If may not be a suitable choice

This is the process: switching from Issue: x==0||1 to x==0||x==1 etc. This code runs from an .html file <body> <div class="" id="pimg1"> </body><script> var x=new Date().getMonth(); if (x == 0||x == 5||x == 2){docu ...

What criteria does the browser use to select the media type for filtering CSS links?

One of the pages on my website includes a link to a stylesheet specifically for printing purposes. <link rel="stylesheet" href="../print.css" type="text/css" media="print" /> While most browsers ignore this link and its styles when rendering for sc ...

Targeting multiple frames in HTML

Have you ever tried clicking on a link and opening three different pages that target three different frames simultaneously? I've managed to open two separate pages targeting two distinct frames using href combined with the onclick function and locatio ...

Problem with Jquery fetching data from specified div not resolved

Here is a working example: var myvar; $.get('formElements.html', function(data) { myvar = data; }); However, this code does not work as intended: var myvar; $.get('formElements.html .className', function(data) { myvar = data; } ...

CSS font attribute stylus variable

Can you help me troubleshoot this issue with the variable in stylus that is setting the font attribute using shorthand syntax? button-font = 100 16px Helvetica Neue', Helvetica, Arial, sans-serif I'm encountering the following error message: ...