<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chloe Ji</title>
    <description>The latest articles on DEV Community by Chloe Ji (@chloe_ji_2d218024cd0360d0).</description>
    <link>https://dev.to/chloe_ji_2d218024cd0360d0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1233602%2Fb314d34d-8901-4213-ab06-7b552d33791d.jpg</url>
      <title>DEV Community: Chloe Ji</title>
      <link>https://dev.to/chloe_ji_2d218024cd0360d0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chloe_ji_2d218024cd0360d0"/>
    <language>en</language>
    <item>
      <title>CSS text balancing : text-wrap : balance</title>
      <dc:creator>Chloe Ji</dc:creator>
      <pubDate>Fri, 15 Dec 2023 04:43:04 +0000</pubDate>
      <link>https://dev.to/chloe_ji_2d218024cd0360d0/css-text-balancing-text-wrap-balance-42k5</link>
      <guid>https://dev.to/chloe_ji_2d218024cd0360d0/css-text-balancing-text-wrap-balance-42k5</guid>
      <description>&lt;p&gt;As developers, we often grapple with the unpredictability of headlines — their final size, font, and even the language can be unknown. &lt;/p&gt;

&lt;p&gt;Varies content length can make the final UI feel unbalanced or missing something. Here is what I mean:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_c0hDDFK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87voiztah3nqx357r9qi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_c0hDDFK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87voiztah3nqx357r9qi.png" alt="Image description" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CSS code of the headline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.unbalanced {
  max-inline-size: 50ch;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the highlighted word in the headline is on its line, which seems odd.&lt;/p&gt;

&lt;p&gt;So, how can we achieve an even distribution of text based on its length without the need to manually adjust the size of the element?&lt;/p&gt;

&lt;p&gt;The answer is using &lt;code&gt;text-wrap: balance&lt;/code&gt;, a new Chrome Canary feature that dynamically balances the text on resize.&lt;/p&gt;

&lt;p&gt;By automatically calculating the number of words and dividing them equally between two lines, we can reach a more appealing headline by using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.balanced {
max-inline-size: 50ch;
text-wrap: balance;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;(For a more visual example, please check: &lt;a href="https://codepen.io/Chloe-Ji/pen/yLZdOxm"&gt;Live demo of text-wrap: balance&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When used with &lt;code&gt;max-width&lt;/code&gt; and &lt;code&gt;min-width,&lt;/code&gt; &lt;code&gt;text-wrap: balance&lt;/code&gt; ensures that the text remains legible and elegantly laid out regardless of screen size.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For max-depth set:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kpRomF27--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw6o99p41iay45nps63b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kpRomF27--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw6o99p41iay45nps63b.png" alt="Image description" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For min-depth set:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4q7vkKOA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1elrz9ezo03w0zhqer2u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4q7vkKOA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1elrz9ezo03w0zhqer2u.png" alt="Image description" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical scenarios of using &lt;code&gt;text-wrap: balance&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Page Headline&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Without balancing:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S-vZCQVS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qs5tvupy1922po118dui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S-vZCQVS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qs5tvupy1922po118dui.png" alt="Image description" width="684" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;With balancing:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m6_B6ctJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44p8vpyhvvkzuiwmmear.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m6_B6ctJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44p8vpyhvvkzuiwmmear.png" alt="Image description" width="674" height="316"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.h1,h2,h3,h4,h5,h6 {
  text-wrap: balance;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Card Content&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Without balancing:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6flbD8JY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zh58vt0ev412dufc7l1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6flbD8JY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zh58vt0ev412dufc7l1.png" alt="Image description" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;With balancing:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b33f0SQG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olp7oqvmqgx63odbu6uq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b33f0SQG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olp7oqvmqgx63odbu6uq.png" alt="Image description" width="800" height="389"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.card_content {
  text-wrap: balance;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tooltip&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We often use a tooltip to show critical information to the user. It might be a few words or multiple lines.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Without balancing:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZNZH7aHE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2rme9dyhvzlnnrr656j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZNZH7aHE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2rme9dyhvzlnnrr656j.png" alt="Image description" width="800" height="625"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;With balancing:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zk4pILDa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqsojyav6t511tyyej8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zk4pILDa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqsojyav6t511tyyej8y.png" alt="Image description" width="800" height="599"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.tip_content p {
  text-wrap: balance;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;em&gt;FAQ&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another example where I see a good potential for &lt;code&gt;text-wrap: balance&lt;/code&gt; is in a FAQ list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Without balancing:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OieFBM0M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rzbs3flu6aj29nyetcr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OieFBM0M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rzbs3flu6aj29nyetcr.png" alt="Image description" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;With balancing:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j73XQpKc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5alu6i38b3skh7scw9d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j73XQpKc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5alu6i38b3skh7scw9d.png" alt="Image description" width="800" height="417"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.FAQ_header {
  text-wrap: balance;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Limitations
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;text-wrap: balance&lt;/code&gt; property works within the confines of the set max-width and min-width, providing a visually consistent text block. However, these fixed boundaries can sometimes lead to suboptimal text distribution, especially in containers with much white space or various content lengths.&lt;/p&gt;

&lt;p&gt;For example, headings inside a card (or any container with borders or shadows).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oWn7iy5V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zejnnoza7umjqvnehkvm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oWn7iy5V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zejnnoza7umjqvnehkvm.png" alt="Image description" width="602" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The red shadow represents the white space area, which is not appealing in some UI designs.&lt;/p&gt;

&lt;h1&gt;
  
  
  Potential solutions
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Dynamic whitespace handling &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By 'unsetting' the white-space property, remove any specific instructions that might inhibit text wrapping. By doing so, you allow text-wrap: balance to perform optimally,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.balanced {
white-space: unset;
text-wrap: balance;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Element width manipulation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fit-content property instructs the element to fit its content's width snugly, accommodating the text's balanced form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.element {
text-wrap: balance;
width: fit-content;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pairing could potentially adjust the element's width based on the balanced text, creating a 'shrink-wrap' effect.&lt;/p&gt;

&lt;p&gt;I’m sure these ideas might be replaced with something better, but you got the idea.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
