ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Layout Reflow Optimization with CSS Grid and Flexbox

CSS

Goal -- WPM

Ready
Exercise Algorithm Area
1/*
2* Responsive Layout using CSS Grid and Flexbox.
3* Optimizes for layout reflow by using appropriate properties.
4*/
5
6.container {
7display: grid;
8grid-template-columns: 1fr; /* Default: single column */
9gap: 1rem;
10padding: 1rem;
11max-width: 1200px;
12margin: 0 auto;
13}
14
15@media (min-width: 768px) {
16.container {
17grid-template-columns: repeat(2, 1fr); /* Two columns on medium screens */
18}
19}
20
21@media (min-width: 1024px) {
22.container {
23grid-template-columns: repeat(3, 1fr); /* Three columns on large screens */
24}
25}
26
27.item {
28background-color: #e0e0e0;
29padding: 1.5rem;
30border-radius: 8px;
31box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
32/* Using flexbox for internal item layout if needed */
33display: flex;
34flex-direction: column;
35justify-content: space-between;
36min-height: 150px;
37}
38
39.item h3 {
40margin-top: 0;
41color: #333;
42}
43
44.item p {
45color: #555;
46line-height: 1.6;
47}
Algorithm description viewbox

Layout Reflow Optimization with CSS Grid and Flexbox

Algorithm description:

This CSS code provides a responsive layout structure that transitions from a single column on small screens to multiple columns on larger screens. It leverages CSS Grid for the main layout structure and media queries to adjust the `grid-template-columns` property. Flexbox is used internally within each grid item to manage content alignment and distribution. This approach minimizes layout reflows by relying on efficient CSS properties and avoiding JavaScript-based manipulations for basic responsiveness.

Algorithm explanation:

This solution combines CSS Grid and media queries to create an efficient and responsive layout. The `.container` is set to `display: grid` with `grid-template-columns: 1fr;` as its default, ensuring it occupies a single column on small viewports. Media queries (`@media (min-width: 768px)` and `@media (min-width: 1024px)`) are used to redefine `grid-template-columns`, changing the layout to two and then three equal columns respectively as the viewport width increases. The `gap` property provides consistent spacing between grid items. Inside each `.item`, `display: flex` is used with `flex-direction: column` and `justify-content: space-between` to ensure content within an item is well-aligned and fills the available vertical space, even if content lengths vary. This strategy is performant because it relies on native CSS layout engines, which are highly optimized for reflows and repaints. By using `fr` units and `repeat()`, the grid definition is concise and scalable. The `max-width` and `margin: 0 auto` on the container center the layout on larger screens.

Pseudocode:

Set the main container to `display: grid`.
Define a single column layout as the default.
Use media queries to change `grid-template-columns` for larger screen sizes.
Apply `gap` for spacing between grid items.
Use Flexbox within grid items for internal content alignment if necessary.