-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Columns Block: Improve readability on smaller screens #30480
Comments
@dashkevych this is a very good optimization request. I think that should be revised urgently, because the correct breaking of many columns does not work for smaller screens. |
This is a pretty tricky topic... Or you could get fancier and convert columns from /**
* Filters the content of a single block.
*
* Takes care of the "grid-template-columns" property for columns
* so we can properly use a css-grid layout instead of css-flexbox.
*
* @access public
* @param string $block_content The block content about to be appended.
* @param array $block The full block, including name and attributes.
* @return string Returns $block_content with our modifications.
*/
function aristath_convert_columns_to_grid( $block_content, $block ) {
if ( 'core/columns' === $block['blockName'] ) {
$grid_template_columns = [];
$min_column_width = '7em';
foreach ( $block['innerBlocks'] as $column ) {
$column_width = "minmax({$min_column_width}, 1fr)";
if ( isset( $column['attrs'] ) && isset( $column['attrs']['width'] ) ) {
$column_width = "minmax({$min_column_width}, {$column['attrs']['width']})";
if ( is_numeric( $column['attrs']['width'] ) ) {
$column_width = "minmax({$min_column_width}, {$column['attrs']['width']}fr)";
}
}
$grid_template_columns[] = $column_width;
}
$style = 'grid-template-columns:' . implode( ' ', $grid_template_columns ) . ';';
// If all columns are equal, use "repeat".
if ( 1 === count( array_unique( $grid_template_columns ) ) ) {
$style = "grid-template-columns: repeat(auto-fill, {$grid_template_columns[0]});";
}
$block_content = str_replace(
'class="wp-block-columns',
'style="' . $style . '" class="wp-block-columns',
$block_content
);
}
return $block_content;
}
add_filter( 'render_block', 'aristath_convert_columns_to_grid', 10, 2 ); but that would require more tweaks in the theme's styles to accommodate the new styles, remove margins and replace them with a At the end of the day, it all depends on your theme's styles... Gutenberg provides some sane defaults, but that's all. If a user adds 6 columns, then they are expected to add content in them that makes sense in 6 columns. |
Hi, I just want to share my solutions about this issues. BackgroundSometimes, UI that "scrolls horizontally on mobile" may be preferable to “stack on mobile”. example: https://buildui.com/ Proof of conceptLet me share my solution for using "Pricing Table". https://wordpress.org/patterns/pattern/pricing-table/ Before
After
Solution
2. Add .wp-block-columns.is-not-stacked-on-mobile {
overflow-x: auto;
}
.wp-block-columns.is-not-stacked-on-mobile>.wp-block-column {
box-sizing: border-box; /* 👈 this is not necessary but it is easy to check width in chrome dev tools */
flex-shrink: 0;
} Pros of this solution
**Updated SolutionI have noticed the following cases as problems with this solution ❌ CASE1: (each column width*3 + gap) < wide width(1000px)Each Column width:
🤔 CASE2: (each column width*3 + gap) > wide width(1000px)
✅ CASE3: (each column width*3 + gap) == wide width(1000px)Each Column width: => This is perfect without scroll in desktop. wide width( Cons of this solution
This is exactly what @aristath pointed out.
Thanks. |
Since the Columns block no longer provides a class with information about number of columns, I am wondering what would be a recommended way to control the Columns block with various number of columns in smaller screens.
The thing is that a grid with three, four, five or six columns might look good on large screens; however, on smaller screens the grid becomes unreadable.
Example
Here is an example of various grids on large screens:
If we view these Columns blocks on iPad PRO (12.9inch), we can see that some grids (with five and six, and even four columns) become unreadable due to a limited space inside the columns.
If we open the same layout even on a smaller screen, for example iPad PRO (10.5inch), the layout becomes worse:
Question
What is a recommended way to control the width of columns in a grid based on the number of columns in it and the screen resolution?
The text was updated successfully, but these errors were encountered: