Explain Codes LogoExplain Codes Logo

Best way to do a split pane in HTML

html
responsive-design
css-grid
flexbox
Alex KataevbyAlex Kataev·Jan 26, 2025
TLDR

Easily create split panes in HTML by implementing CSS Flexbox. Employ display: flex; in your container styling and flex: 1; to each section for equal width division:

<div style="display: flex; height: 100vh;"> <div style="flex: 1; background: #add8e6;">Left Pane</div> <div style="flex: 1; background: #90ee90;">Right Pane</div> </div>

This snippet results in two stable panes that share the viewport's total height and adjust automatically with the window size.

Elements of Resizing Flexbox Panes

User-Controlled Pane Sizing

Making your panes resizable grants user control to adjust their sizes. Use the CSS resize property wrapped within a class:

.resizable { overflow: auto; resize: horizontal; width: 50%; min-width: 100px; /* Because no one likes infinitesimal panes 😉 */ }

To ensure smoother user interaction during dragging, set user-select:none on draggable areas:

.draggable-area { cursor: col-resize; user-select: none; }

Resizing Logic with JavaScript

JavaScript ensures advanced resizing functionality linked to event handlers that control mouse-driven resizing. Be sure to prevent your panes from going stealth mode (i.e., sizing down to zero) with appropriate dimension checks:

const dragElement = (element) => { element.onmousedown = dragMouseDown; // ... (Trust me, JavaScript doesn't bite!) };

Grid Layouts: The Next Level

For more intricate designs, consider the power of CSS Grids, offering fine-tuned control over columns and rows:

.container { display: grid; grid-template-columns: 1fr 1fr; /* Divide and conquer! */ }

Styling: Beautify Segment Division

For better UX, emboss your separator with a background tint or image. Put up a min-width to set a reasonable minimum pane size, and use cursor: col-resize or cursor: row-resize indicators for clear visual interactivity.

Mastering Horizontal and Vertical Layouts

Handling horizontal and vertical separators requires distinct styles. With offsetLeft and offsetTop, get precise control over separator's position and adapt pane dimensions dynamically using mouse event handlers.

Ensuring Compatibility

While Simpledrag library serves as an alternative for draggable elements, verify browser compatibility by testing across platforms. Reflect on factors like screen width when setting resizable limits.

Dive into Split.js: A Lightweight Guide

Getting Started

Split.js offers a nourishing small-plate special for resizable panes. Compatible with older browsers, it's an efficient and independent tool for pane sizing, working without jQuery. Here is a quick serving:

Split(['#left', '#right'], { sizes: [50, 50], minSize: 100, gutterSize: 10, cursor: 'col-resize', });

Master Flexbox and CSS Grid with Split.js

Blend Flexbox or CSS Grid with Split.js to create responsive and dynamic layouts. Tip: percentage widths are your secret recipe for flexible pane sizing.

Respecting User Interactions

When embarking on draggable or resizable features, ensure pane integrity. How? Look below:

  1. Set acceptable limits with min-width or min-height. (Prevent your pane from trying to become John Cena - invisible!)
  2. Elegantly adjust pane sizes on window resizing via resize events.