How to align 3 divs (left/center/right) inside another div?
Make use of CSS Flexbox to align your three divs
(left/center/right) within another div
. Apply display: flex; justify-content: space-between;
to the parent div
. This strategy evenly spreads the child divs
across the available space, maintaining space harmony.
Using Flexbox, you persist absolute control over each element's positioning: left-aligned, centered, and right-aligned. This process gives you an adaptive, streamlined layout.
Floats: the fallback squad
In instances where you require a robust float-based layout to satisfy the demands of older browsers or specific design restrictions, follow the strategy below. Crucially, the left and right divs
should float and they must precede the center div
in your HTML. To regulate the layout intact, don't forget to employ a clearfix div
.
Heads up: You need to ascertain the total width of all child elements in your div
garden isn’t overshooting the width of their parent div
.
Embracing responsive design
When embarking on a responsive design journey, don't forget to consider percentage-based widths and incorporate media queries:
This set-up will graciously stack elements on smaller screens, thus upholding usability.
Building for compatibility
Although Flexbox enjoys extensive support, some older browsers may frown at it. To ensure harmony, use Autoprefixer for handling vendor prefixes. Also note that IE versions below 10 don’t speak the Flexbox language. In these situations, either float-based or display: table;
methods can come to the rescue.
Keeping HTML/CSS efficient
Preserving a clean and minimal HTML structure is key. Make substantial changes in your CSS, not HTML. This approach will ensure your markup stays semantic and uncomplicated, exploiting CSS for layout fine-tuning.
Table display: the old school way
In some situations, a traditional tabular layout using display: table;
might be the perfect fit:
This method mirrors a table row encapsulating cells, useful when dealing with tabular data.
Was this article helpful?