CSS Container Not Expanding with Floating Sidebar
I ran into a classic CSS layout issue while working on a new template. I borrowed a base template from OSWD.org and modified it, but the main container wouldn’t expand to contain the sidebar (#side) when it had more content. The container expanded fine for the main content area (#content), but the sidebar seemed to overflow.
After inspecting the code, I realized the sidebar was floated but not cleared properly. The container only contained the main content, so it collapsed around it. The fix was to use a clearfix on the container.
The Problem
The container had two floated children: a wide main content div and a narrower sidebar. The sidebar was floated left or right, but the container didn’t have any clearing mechanism, so it didn’t wrap around the sidebar. This is a common issue when using floats for layout.
The Solution: Clearfix
I applied a clearfix to the container. The modern way is to use the ::after pseudo-element:
.container::after {
content: "";
display: table;
clear: both;
}
Alternatively, you can use overflow: hidden on the container, but that can clip content or cause scrollbars.
Modern 2026 Trends & Best Practices
Today, most developers use Flexbox or CSS Grid for layouts, which avoid these float issues entirely. For example:
.container {
display: flex;
gap: 20px;
}
.side {
flex: 0 0 300px;
}
.main {
flex: 1;
}
Or with Grid:
.container {
display: grid;
grid-template-columns: 1fr 300px;
gap: 20px;
}
These methods are more robust and easier to maintain. If you’re maintaining legacy code, clearfix is still valid, but for new projects, consider using modern layout techniques.

