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%;
}
Chrome, Firefox, and Opera correctly display it like this:
IE11 incorrectly puts the text 400 pixels to the right, based on the natural width of the image:
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:
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.]
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.
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:
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With