{"id":2555,"date":"2024-09-05T18:00:30","date_gmt":"2024-09-05T21:00:30","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=2555"},"modified":"2024-09-05T17:40:06","modified_gmt":"2024-09-05T20:40:06","slug":"center-flexbox-column-between-two-variable-width-columns-common-toolbar-scenario","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/center-flexbox-column-between-two-variable-width-columns-common-toolbar-scenario\/","title":{"rendered":"Center Flexbox Column Between Two Variable-Width Columns (Common Toolbar Scenario)"},"content":{"rendered":"<h2>The Issue<\/h2>\n<p>I often run into the scenario where I'm building a toolbar with items on the left, in the middle, and on the right. The items on the left and right can often be of varying width, and as their widths change dynamically, the item in the middle moves to the left or right while it centers itself between the outer edge of the left\/right items.<\/p>\n<p>For example, a toolbar with some buttons on the left, a date input in the middle, and a message on the right might look like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/benjaminray.com\/codebase\/wp-content\/uploads\/2024\/09\/2024-09-05-toolbar-example-1.png\" alt=\"An example toolbar\" \/><\/p>\n<p>But if there is only one button and a really long message, the date input shifts more to the left:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/benjaminray.com\/codebase\/wp-content\/uploads\/2024\/09\/2024-09-05-toolbar-example-2.png\" alt=\"An example toolbar\" \/><\/p>\n<p>This is not a great user experience, especially if the outer column widths change dynamically.<\/p>\n<details>\n<summary>HTML\/CSS Before Applying Solution<\/summary>\n<pre><code class=\"language-css\">.toolbar {\n  display: flex;\n  justify-content: center;\n  align-items: center;\n}\n<\/code><\/pre>\n<pre><code class=\"language-html\">&lt;div class=\"toolbar\"&gt;\n  &lt;div class=\"button-col\"&gt;\n    &lt;button&gt;New Event&lt;\/button&gt;\n    &lt;button&gt;Edit&lt;\/button&gt;\n    &lt;button&gt;Copy&lt;\/button&gt;\n  &lt;\/div&gt;\n  &lt;input type=\"date\" value=\"2024-08-01\" \/&gt;\n  &lt;div class=\"message-col\"&gt;\n    &lt;span&gt;\u2713 Event saved!&lt;\/span&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<\/details>\n<p>&nbsp;<\/p>\n<h2>The Solution<\/h2>\n<p>We can easily solve this one by applying <code>flex-grow: 1<\/code> and <code>flex-basis: 0<\/code> to the outer columns as follows:<\/p>\n<pre><code class=\"language-css\">.toolbar {\n  display: flex;\n  justify-content: center;\n  align-items: center;\n}\n\n.button-col,\n.message-col {\n  flex-grow: 1; \/* See here *\/\n  flex-basis: 0; \/* And here *\/\n}\n<\/code><\/pre>\n<pre><code class=\"language-html\">&lt;div class=\"toolbar\"&gt;\n  &lt;div class=\"button-col\"&gt;\n    &lt;button&gt;New Event&lt;\/button&gt;\n    &lt;button&gt;Edit&lt;\/button&gt;\n    &lt;button&gt;Copy&lt;\/button&gt;\n  &lt;\/div&gt;\n  &lt;input type=\"date\" value=\"2024-08-01\" \/&gt;\n  &lt;div class=\"message-col\"&gt;\n    &lt;span&gt;\u2713 Event saved!&lt;\/span&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>And now our date input is centered, regardless of the width of the outer columns!<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/benjaminray.com\/codebase\/wp-content\/uploads\/2024\/09\/2024-09-05-toolbar-example-3.png\" alt=\"An example toolbar\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/benjaminray.com\/codebase\/wp-content\/uploads\/2024\/09\/2024-09-05-toolbar-example-4.png\" alt=\"An example toolbar\" \/><\/p>\n<h2>Dealing With Wrapping<\/h2>\n<p>On smaller screens, the outer columns will start to wrap when they bump up against the center column. You can set different flex-grow values at different screen widths to optimize the behaviour. You can also set  <code>white-space: nowrap<\/code> on a specific column (e.g. the buttons column) to prevent it from wrapping, which pushes the wrapping behaviour to the other column (e.g. the messages column).<\/p>\n<h2>Credit:<\/h2>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/a\/65610410\/4669143\">StackOverflow Answer<\/a><\/li>\n<li><a href=\"https:\/\/codepen.io\/gamliela\/pen\/BaLVrWg\">CodePen used in StackOverflow Answer<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The Issue I often run into the scenario where I&#8217;m building a toolbar with items on the left, in the middle, and on the right. The items on the left  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,48],"tags":[50,49,51],"class_list":["post-2555","post","type-post","status-publish","format-standard","hentry","category-css","category-html","tag-centering","tag-flexbox","tag-toolbar"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-Fd","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/2555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=2555"}],"version-history":[{"count":11,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/2555\/revisions"}],"predecessor-version":[{"id":2570,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/2555\/revisions\/2570"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=2555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=2555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=2555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}