Explain Codes LogoExplain Codes Logo

Two divs side by side - Fluid display

html
responsive-design
css-preprocessors
accessibility
Nikita BarsukovbyNikita Barsukov·Sep 26, 2024
TLDR

To align two divs side by side in a responsive manner, deploy the power of display: flex; on the parent container. Give flex: 50%; to both child divs for equal division of width. The core concept in code:

<div style="display: flex;"> <div style="flex: 50%; background-color: lightblue;">Div 1 (#TeamBlue)</div> <div style="flex: 50%; background-color: lightcoral;">Div 2 (#TeamCoral)</div> </div>

No matter the change in screen size, flex: 50%; ensures each div will continue occupying half of the container's width, providing a fluid and adaptable display.

The Fluid Approach: Adaptability is Key

Always keep in mind, fluid design is about adaptability. Elements in a fluid layout are versatile chameleons, adapting to different environments.

Fine-Tuning for Various Screens

To ensure elegant usability across devices, we use media queries to optimize layout for different viewport sizes:

@media screen and (max-width: 768px) { .container > div { // Divs go full-width in a party for mobile device screens! flex: 100%; } }

In this strategy, we stack the divs vertically for smaller screens, providing a tailored user experience.

Preempting Sub-pixel Dilemmas

Let's not forget about the fun spoiler, sub-pixel rendering. To block its party-crashing tendencies, adopt percentage-based widths and fixed pixel padding:

.container > div { // The box-sizing CSS3 property, "box-sizing" to the rescue! box-sizing: border-box; width: 50%; padding: 10px; }

With box-sizing: border-box;, padding gets incorporated in the div's width calculation, ensuring no overflow happens.

Quick Prototyping with CSS Frameworks

Save time and sanity with CSS frameworks like Bootstrap or Foundation. They offer predefined classes and grid systems.

<div class="row"> <div class="col-md-6">Div 1 (#Ninja)</div> <div class="col-md-6">Div 2 (#Samurai)</div> </div>

With Bootstrap grid, .col-md-6 grants each div half of the container width on medium and larger screens.

Advanced Techniques: Beyond Basics

CSS Preprocessors: The Future is Here, Almost

Preprocessors like SASS or LESS supercharge your CSS with variables, nested syntax, and mixins:

$paddingPolice: 10px; .container > div { width: 50%; padding: $paddingPolice; //Sir PaddingPolice keeps the consistency! }

Using variables such as $paddingPolice, you end up with much cleaner styles and more maintainable codebases.

Center Alignment with Minimum Markup

Here's a fun trick: Wrap your content in a div with auto margin for a simple means of centrally aligning:

<div style="margin: auto; width: 80%;"> <!-- Flexbox container parking here, all clear! --> </div>

In this setup, the wrapper centers your content. Inside, the Flexbox handles the side-by-side arrangement. Clean and efficient!

Enhancing with Accessible and Efficient Code

Accessibility First

While framing your HTML structures, don’t forget to include ARIA roles and semantically correct elements. Yep, let's make the Web more inclusive:

<div role="main"> <section role="article" style="flex: 50%;">Article 1</section> <section role="article" style="flex: 50%;">Article 2</section> </div>

ARIA roles, like main and article, provide necessary contextual information and greatly help assistive technologies.

Inline-flex: The Lone Ranger

For tackling alignment issues, Inline-flex jumps in to preserve sanity:

<div style="display: inline-flex;"> <!-- Inline-flex divs, activated! --> </div>

This directs the div to behave like an inline item but retain flex capabilities, all without disturbing the flow.

Minimize for Optimized Performance

For a clean codebase and better performance, minimize your HTML/CSS:

<div class="flex-container"> <div class="flex-item">Div 1</div> <div class="flex-item">Div 2</div> </div>

Together with crisp CSS, this approach favors efficiency, easing debugging and maintenance like a dream!