Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behavior with image inside floated container

Tags:

html

css

Given this simple layout:

HTML

<div class="container">
  <div class="imgContainer">
    <img src="http://placehold.it/400x400">
  </div>
  <div>
    This should always stay to the right of the image.
  </div>
</div>

CSS

.container {
  height: 20vh;
}

.imgContainer {
  float: left;
  height: 100%;
}

img {
  height: 100%;
}


Issue #1

Chrome, Firefox, and Opera correctly display it like this:

enter image description here

IE11 incorrectly puts the text 400 pixels to the right, based on the natural width of the image:

enter image description here


Issue #2

As you increase the window's height, the text should stay glued to the right of the image. This works correctly in Firefox.

However, the text overlaps the image in Chrome and Opera:

enter image description here


See the behavior in this Fiddle.

Question: Is there a style I can add that will cause all browsers to behave consistently?

[Note: I discovered this while working on this question. I thought I had a solution, until I realized it wasn't responsive in any browser except Firefox.]

like image 345
Rick Hitchcock Avatar asked Dec 06 '15 00:12

Rick Hitchcock


People also ask

What is a container image?

A container image is a static file with executable code that can create a container on a computing system. A container image is immutable—meaning it cannot be changed, and can be deployed consistently in any environment.

What are floats and why are they useful?

Floats are also helpful for layout in smaller instances. Take for example this little area of a web page. If we use float for our little avatar image, when that image changes size the text in the box will reflow to accommodate:

How do floats affect the element that contains them?

One of the more bewildering things about working with floats is how they can affect the element that contains them (their “parent” element). If this parent element contained nothing but floated elements, the height of it would literally collapse to nothing.

What is the CSS float property?

In web design, page elements with the CSS float property applied to them are just like the images in the print layout where the text flows around them. Floated elements remain a part of the flow of the web page.


1 Answers

The following might do the trick.

Instead of using float, I would suggest using CSS tables.

Apply display: table to .container and set the height as needed.

For the two child elements, .imgContainer and .panel, use display: table-cell and inherit the height from the parent block.

I think this is pretty close to what you need, should work in all browsers (but I did not check...)

.container {
  height: 20vh;
  display: table;
}
.imgContainer, .panel {
  display: table-cell;
  height: inherit;
  vertical-align: top;
}
img {
  vertical-align:top;
  height: inherit;
}
<div class="container">
  <div class="imgContainer">
    <img src="http://placehold.it/400x400">
  </div>
  <div class="panel">
    This should always stay to the right of the image.
  </div>
</div>
like image 79
Marc Audet Avatar answered Nov 15 '22 10:11

Marc Audet