HTML/CSS float: left; is not working properly

First, it might be the issue that you are adding margin and paddings, which add size to the box even though the box has a width of 420px, you have to calculate margins and paddings to, or use * { box-sizing: border-box; }, which will calculate the width of that element as a sum of all the sizes (padding, margin, width) and not exceed the width.

Second, the order of the elements are important when floating, put main always on top of the sidebar, since elements start as top to bottom, when you float a top element, the bottom element if its wider, will collapse and align with the main box.

Screenshot for reference:

Leave a Comment