Explain Codes LogoExplain Codes Logo

Centering floating divs within another div

html
responsive-design
css
flexbox
Alex KataevbyAlex Kataev·Oct 14, 2024
TLDR

You need to drop the use of float, and leverage the power of display: inline-block; in your child divs. This should be done in a parental div setting that utilizes text-align: center;. As a means to bypass whitespace problems, initialize font-size as 0 in the parent, then restore it in the child elements.

<div style="text-align:center; font-size:0;"> <div style="display:inline-block; font-size:initial;">Content 1</div> <!-- Hey, it's me the content --> <div style="display:inline-block; font-size:initial;">Content 2</div> <!-- Content #2, just floating around here --> <div style="display:inline-block; font-size:initial;">Content 3</div> <!-- A wild third content appears! --> </div>

Centering techniques

The modern way: Flexbox

Flexbox offers an elegant way for centering items horizontally and vertically. The properties justify-content: center; and align-items: center; are your partners in crime here.

.parent { display: flex; /* Let's flex those centering muscles! */ justify-content: center; /* Horizontally centering like a boss */ align-items: center; /* Vertically centering like an even bigger boss */ } .child { /* Float? Not here, pal! */ }

Old school: Block centering

For block elements centering like divs, we simply declare a width, then implement margin-left: auto; and margin-right: auto;. Simplicity is key!

div { width: 50%; /* Decidedly defining width */ margin-left: auto; /* Let left margin do some auto magic! */ margin-right: auto; /* Not leaving right margin out of the fun! */ }

Float balancing: clearfix

If floating elements are vital, use a clearfix to halt a parental collapse:

.clearfix::after { content: ""; display: table; /* Becomes a table. No, not that type of dinner table */ clear: both; /* Sorcery for clearing both sides */ }

Customization techniques

Setting dimensions like a pro

Control your landscape - be decisive about width and height for elements:

.parent { width: 80%; /* Properly appointed width */ margin: auto; } .child { width: 30%; /* Responsive design with percentages */ }

Handy helper divs

There are situations where display: inline-block; may not be suitable or preferred. Helper divs with an overflow: hidden; method can lend a hand or consider absolute positioning within a relative container.

Tread lightly with browser compatibility

Advanced features like flexbox befriend modern browsers but frown upon older browsers. Check your compatibility and have a fallback plan.

Responsiveness tactics

The wonder of media queries

Media queries help tweak layouts on varied screens, making sure the centering doesn't go haywire.

@media (max-width: 600px) { .child { width: 100%; /* Screens smaller than 600px get full-width treatment */ } }

Scalability with ems

Using the em measurement for your margins and widths creates a fluid layout that can adapt based on the font size.