Adding overflow: hidden; in body should solve your problem. It defines a new context for block formatting, as described in this article: Overflow Magic: Hidden .
jsFiddle Demo (the body tag is automatically added by jsFiddle, so I did not include it in the HTML markup)
UPDATE (thanks to @clairesuzy): This solution does not work if body has padding: 0 . Until I can find a better way, I can only offer to add a wrapper around two divs (at least I deserve now @Marcel downwote :)), which, in my opinion, is cleaner than the solutions posted by the OP. I usually add a wrapper around floating material in any case (it simplifies the processing of older browsers most of the time), most of the time it does not need to be added intentionally, because it is logically and semantically required.
So now I can come up with this:
<body style="padding: 0; margin: 0;"> <div id="container" style="overflow: hidden;"> <div style="float: right;">first</div> <div style="margin-top: 2em;">second</div> </div> </body>
jsFiddle Demo
UPDATE 2 . After thinking about it and reading the comments, I really think that overflow: hidden (or anything other than overflow: visible ) on the container is the right solution. The only exception in which it did not work for me is to set it to the body element, which is very rare. In these rare situations, you can try using position: absolute; top: 0; right: 0; position: absolute; top: 0; right: 0; instead of swimming.
Another possible workaround: I also found that setting display: inline-block; width: 100%; display: inline-block; width: 100%; on body really works.
jsFiddle Demo
kapa Jun 01 2018-11-17T00: 00Z
source share