Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Div with a floating Div inside, won't fit to the content

Tags:

html

<div style="background-color:black">
   <div style="float:right">
       Test message
   </div>
<div>

This will show 'Test message' with white background because the parent div didn't fit the content.

How can make the div fit to the content? (without using table, or display:table since it's not supported in IE)

like image 209
EBAG Avatar asked Feb 03 '26 18:02

EBAG


2 Answers

You can also do this:

<div style="background-color:black; overflow: auto;">
   <div style="float:right">
       Test message
   </div>
<div>

It's a cleaner way to do the clearfix :)

like image 115
Nick Craver Avatar answered Feb 06 '26 06:02

Nick Craver


A common solution is to put a <br style="clear: both;"> after the floating element to clear the float and cause the parent element to wrap the elements.

<div style="background-color:black">
   <div style="float:right">
       Test message
   </div>
   <br style="clear: both;">
<div>

Apart from the problem, I'd suggest to put the styles in a separate CSS file, if not realised yet.

like image 23
BalusC Avatar answered Feb 06 '26 06:02

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!