When creating UI components for the web, you want to avoid jumping content where one component pushes another away. However, this is not always as easy as it seems.
A SIMPLE TABS IMPLEMENTATION
Take a tabs component as an example. You have multiple content containers and if you want to avoid jumping of the content you need to make sure that the whole component is large enough to contain the largest of the content containers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<divclass="tabs"><divclass="tabs__header"><buttonclass="tabs__title active">Tab1</button><buttonclass="tabs__title">Tab2</button></div><divclass="tabs__content-wrapper"><divclass="tabs__content active"><p>Molestiae voluptatem autem molestias quidem. In cupiditate id eligendi debitis. Repellat vel quod aut magni quo.</p></div><divclass="tabs__content"><p>Eos veritatis occaecati provident vel. Voluptate consectetur deserunt quia iusto asperiores quibusdam voluptas aut. Culpa tenetur consequatur ea sunt sit impedit. Rerum voluptas sed enim et asperiores. Labore facere facilis sunt illo ut praesentium.</p><p>Ex aut explicabo esse in consequuntur ad. Pariatur molestias minima facilis. Aut sed a quia. Dicta eum architecto et cumque sed.</p></div></div></div><divclass="other">Other Content</div>
Until now, we resorted to using JavaScript to determine the maximum height of the content and setting it on the component.
But with the dawn of CSS Grid there is an easier pure CSS solution to the problem.
The trick is to put all content containers into one CSS Grid cell.
The grid will then make sure that the cell is big enough for every content container.
Even though the browser support for CSS grid is still mediocre,
we can utilise @supports to gracefully degrade to the original solution without height retainment.
The same solution also works for similar use cases where
I want to dynamically switch the content but retain the overall height
like details containers or fading from one component into another.
About the author
Philipp Mitterer is a frontend dev with an affinity for user experience, performance and code quality, always seeking new challenges.