Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome and Edge floating middle image below first and last, is this correct?

It seems that in Microsoft's edge and google chrome the floating doesn't rearrange the divs properly, if you have three divs floated left and the page is scaled in a width between 444 and 436 px the third div goes to the second div's position instead of going below it. This "bug" does not occur in firefox.

I made a JSFiddle to be tested http://jsfiddle.net/e47jckrh/

HTML

<div id="d1">
    <p>1</p>
</div>
<div id="d2">
    <p>2</p>
</div>
<div id="d3">
    <p>3</p>    
</div>

CSS

div{
  float: left;
}

Down below there is a visual representation of how it should behave

Full page Layout

Full page

Correct div floating when page width is more than 444px

correct >444

Wrong div floating order when page width is between 444 and 436px

wrong

Correct div floating when page width is less than 444px

correct <444

It may be just me thinking there is something wrong, but i assume the firefox behavior to be the correct one.

like image 495
user3847141 Avatar asked Sep 27 '22 13:09

user3847141


People also ask

Where are floating elements usually used?

Its most common use is to wrap text around images. However, you can use the float property to wrap any inline elements around a defined HTML element, including lists, paragraphs, divs, spans, tables, iframes, and blockquotes.

Which of the following float left or float right is mandatory?

The element must float on the right side of its containing block. The element must not float. The element must float on the start side of its containing block. That is the left side with ltr scripts, and the right side with rtl scripts.

How do you float an image in HTML?

To use a floating image in HTML, use the CSS property float. It allows you to float an image left or right.

In what direction does float will work IMG float right?

The elements after the floating element will flow around it. The elements before the floating element will not be affected. If the image floated to the right, the texts flow around it, to the left and if the image floated to the left, the text flows around it, to the right.


2 Answers

I've edited your fiddle here: http://jsfiddle.net/e47jckrh/5/

You didn't float all your elements as you'd suggested in the question. So I added float: left; to the div numbered 3 and set all 3 to display: inline-block;. And using display: table; and display: table-cell; with vertical-align: center; is a really old way to get something to align vertically.

These 3 lines work for most things:

position: relative;
top: 50%;
transform: translateY(-50%);
like image 58
Jay Avatar answered Oct 12 '22 23:10

Jay


Okay. So #d3 shouldn't be on the right side at all because it isn't floated, but you used display:table. This gives it the effect of being floated with the other two divs for some reason.

Since it uses display: table #d3's margins are off the page but don't count as "not fitting" and don't force it to the next line.

#d2 is floated though so its margins DO affect it. So in the small range you're experiencing this #d1 + #d2 won't fit because of their margins contributing to an overall larger size width but #d1 + #d3 will because #d3 is not being affected by its margin on the right side giving the combo an overall smaller size width.

Simple solution is to float #d3 as well:

#d3 {
    background-color: #ede4ad;
    border: 3px dotted #6e5b3c;
    clear: right;
    float: left;
}
like image 41
Alejalapeno Avatar answered Oct 12 '22 23:10

Alejalapeno