<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Azubuike Duru - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Azubuike Duru - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 15 May 2026 22:29:29 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/azubuikeduru/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use the CSS text-wrap Property to Create Balanced Text Layouts on Your Websites ]]>
                </title>
                <description>
                    <![CDATA[ An inconsistent text layout can really ruin the look of your website’s design. Maybe a heading has an extra word that wraps to another line, or in a paragraph some lines are longer than others. This can leave the whole thing looking messy and hard to... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-css-text-wrap-property/</link>
                <guid isPermaLink="false">67fd1442fd23722b0a451053</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Responsive Web Design ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Azubuike Duru ]]>
                </dc:creator>
                <pubDate>Mon, 14 Apr 2025 13:57:22 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744638131989/38357168-abda-4f7b-8c4f-568f64b586df.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>An inconsistent text layout can really ruin the look of your website’s design. Maybe a heading has an extra word that wraps to another line, or in a paragraph some lines are longer than others. This can leave the whole thing looking messy and hard to read.</p>
<p>Before now, developers used tags like <code>&lt;br&gt;</code> or <code>&lt;span&gt;</code> to manually adjust word spacing. But what happens in cases where you also need to consider the responsiveness of the website? Well, the new <code>text-wrap: balance</code> property in CSS can now automatically calculate and divide the lines in such a way that every word looks organized and even. Like this:</p>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_4C8BE890CB3AB8AD50C286E15DBA884FF164212E142B1E75C767C0221DB183E7_1743742346617_Desktop+-+1-3.png" alt="Heading before and after applying 'text-wrap: balance' for improved wrapping." width="600" height="400" loading="lazy"></p>
<p>In this article, you’ll learn how the <code>text-wrap</code> property works and how to use it in your code. You’ll also see a few examples along the way.</p>
<p>Without wasting time, let's get right into it.</p>
<h3 id="heading-table-of-contents">Table of Contents</h3>
<ul>
<li><p><a class="post-section-overview" href="#heading-understanding-the-problems">Understanding the Problems</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-introducing-text-wrap">Introducing text-wrap</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-text-wrap">What is text-wrap?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-basic-syntax-of-text-wrap">Basic Syntax of text-wrap</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-text-wrap-vs-max-width-when-to-use-each">text-wrap vs. max-width : When to Use Each</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-when-to-use-max-width-vs-text-wrap">When to Use max-width vs text-wrap</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-browser-support-and-considerations">Browser Support and Considerations</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-practical-implementation-step-by-step-guide">Practical Implementation: Step-by-Step Guide</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-understanding-the-problems">Understanding the Problems</h2>
<p>Apart from making words harder to read when your text is unevenly displayed, there are other issues uneven or poorly displayed text can cause on your website as a whole. Some of these are:</p>
<ul>
<li><p><strong>Responsive Design</strong>: We wouldn't have any issues if we could manually design for every screen size and carefully place and space the lines of our text exactly as we wanted users to read them. Unfortunately, we can't do this, which is why making designs responsive is always crucial. When text adjusts from one screen size to another, lines break, and what looks good on a desktop may look terrible on a tablet view.</p>
</li>
<li><p><strong>Headings and Short Paragraphs</strong>: Since paragraphs and small blocks of text have many words, there is a high probability of words ending in a very unbalanced way. Often, we see a heading line ending with just one word, forming an awkward shape in the overall design.</p>
</li>
<li><p><strong>Dynamic Content</strong>: If your website contains dynamic content (such as cards of various sizes, product descriptions, or blog posts), having no <code>text-wrap</code> might make your layout break or appear unpredictable.</p>
</li>
</ul>
<h2 id="heading-introducing-text-wrap">Introducing <code>text-wrap</code></h2>
<p>In the last section, we looked at the problems attributed to uneven text distribution. In this section, you’ll see how <code>text-wrap</code> is a game changer in how you organize your text.</p>
<p>Before <code>text-wrap</code>, developers relied heavily on <code>max-width</code>, <code>text-align</code>, or <code>&lt;br&gt;</code> to control text lines. The new <code>text-wrap</code> CSS property was created to help break text naturally across lines without making it look out of place, preventing the need for trial and error in checking if text fits.</p>
<h3 id="heading-what-is-text-wrap">What is <code>text-wrap</code>?</h3>
<p><code>text-wrap</code> is a CSS property that can help you adjust and space text automatically, break lines evenly without using the rigid <code>&lt;br&gt;</code> tag, and not have to rely on <code>text-align</code>, which didn't work on all screen sizes.</p>
<p>With <code>text-wrap</code>, your headings and paragraphs are sure to look good. Instead of having some lines that look longer than others, <code>text-wrap</code> neatly arranges everything to appear visually appealing.</p>
<h3 id="heading-basic-syntax-of-text-wrap">Basic Syntax of <code>text-wrap</code></h3>
<p>There are two major ways to apply text-wrap to your text. These values are:</p>
<p><code>text-wrap: balance</code><strong>: (The Smart Heading Balancer)</strong></p>
<p>This is the smart way of telling the browser to adjust the text evenly, regardless of the screen size.</p>
<p>Code snippet:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">text-wrap</span>: balance;
}
</code></pre>
<p>In this code, the heading text lines will split naturally without any weird short lines.</p>
<p><code>text-wrap: pretty;</code> <strong>(The Smart Paragraph Balancer)</strong></p>
<p>Just as <code>text-wrap: balance</code> works best for headings, <code>text-wrap: pretty</code> is the best for styling long paragraphs.</p>
<p>Code snippet:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">p</span>{
  <span class="hljs-attribute">text-wrap</span>: pretty;
}
</code></pre>
<p>The <code>p</code> in this code will make sure the paragraphs maintain natural readability.</p>
<p><strong>Other values include:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Value</strong></td><td><strong>Description</strong></td></tr>
</thead>
<tbody>
<tr>
<td><code>wrap</code></td><td>Default state</td></tr>
<tr>
<td><code>nowrap</code></td><td>Prevents text from wrapping to the next line</td></tr>
<tr>
<td><code>stable</code></td><td>Ensures things are kept in place until the content changes itself</td></tr>
</tbody>
</table>
</div><h2 id="heading-text-wrap-vs-max-width-when-to-use-each"><code>text-wrap</code> vs. <code>max-width</code> : When to Use Each</h2>
<p><code>max-width</code> was always the go-to option for developers in the past. While this method works in most cases, it won’t stop uneven text distribution. Let's compare it with the new <code>text-wrap</code> CSS property so you can know when to use each one.</p>
<p><strong>Using</strong> <code>max-width</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">400px</span>;
}
</code></pre>
<p>The <code>max-width</code> here forces the heading not to exceed a 400px width. This can be good for controlling short body paragraphs but can cause unevenness for headings when dealing with multiple screen sizes.</p>
<p><strong>Using</strong> <code>text-wrap</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">text-wrap</span>: balance;
}
</code></pre>
<p>Here, the browser dynamically breaks the heading text in a balanced way, preventing any weird-looking single lines.</p>
<h3 id="heading-when-to-use-max-width-vs-text-wrap">When to Use <code>max-width</code> vs <code>text-wrap</code></h3>
<p>Use <code>text-wrap: balance</code> when you want a more natural text read without any weird line breaks.</p>
<p>Use <code>max-width</code> when you need to control the text width and don't necessarily care how the lines break.</p>
<p>Use both if you want a more natural read within a confined width limit.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">500px</span>;
  <span class="hljs-attribute">text-wrap</span>: balance;
}
</code></pre>
<p>This ensures the heading stays within a 500px limit while maintaining an even text distribution.</p>
<h3 id="heading-browser-support-and-considerations">Browser Support and Considerations</h3>
<p>Currently, Chrome and Edge are the only main browsers that support the new <code>text-wrap</code> property. If you are building a project that should work on browsers like Safari and Firefox, you will need to use traditional text formatting methods like <code>text-align</code>, <code>&lt;br&gt;</code>, or <code>max-width</code> instead.</p>
<p>Fallback snippet:</p>
<pre><code class="lang-css"><span class="hljs-keyword">@supports</span> (<span class="hljs-attribute">text-wrap:</span> balance) {
  <span class="hljs-selector-tag">h1</span> {
    <span class="hljs-attribute">text-wrap</span>: balance;
  }
}
</code></pre>
<p>The <code>@support</code> is a pro tip to apply this style to only supported browsers.</p>
<h2 id="heading-practical-implementation-step-by-step-guide">Practical Implementation: Step-by-Step Guide</h2>
<p>Now that you’ve seen how important <code>text-wrap</code> can be and how to use it, let’s put that knowledge into practise and see real examples, compare the before and after screens of using it (and not), and check how it reacts in responsive designs as well.</p>
<h3 id="heading-1-applying-text-wrap-balance-to-headings">1. Applying <code>text-wrap: balance</code> to Headings</h3>
<p>In this section, we will see how the heading lines in the topic <strong>"How to Use CSS Text Balance: A Simple Trick for Smoother, Cleaner Designs"</strong> will break across different sizes and how it looks when <code>text-wrap: balance</code> is applied.</p>
<p><strong>Without</strong> <code>text-wrap: balance</code>:</p>
<p>HTML</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"title"</span>&gt;</span>How to Use CSS Text Balance: A Simple Trick for Smoother, Cleaner Designs<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
</code></pre>
<p>CSS</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2.5rem</span>;
  <span class="hljs-attribute">font-weight</span>: bold;
}
</code></pre>
<p>Without any special application to the headings, it will just adjust freely.</p>
<p>Result:</p>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_4C8BE890CB3AB8AD50C286E15DBA884FF164212E142B1E75C767C0221DB183E7_1743740541468_Screenshot+2025-04-04+at+5.21.36AM+1.png" alt="Heading without 'text-wrap: balance', showing uneven wrapping." width="600" height="400" loading="lazy"></p>
<p><strong>With</strong> <code>text-wrap: balance</code>:</p>
<p>CSS</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2.5rem</span>;
  <span class="hljs-attribute">font-weight</span>: bold;
  <span class="hljs-attribute">text-wrap</span>: balance;
}
</code></pre>
<p>Now, the browser automatically adjusts the line breaks to ensure a more even distribution.</p>
<p>Result:</p>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_4C8BE890CB3AB8AD50C286E15DBA884FF164212E142B1E75C767C0221DB183E7_1743740708025_Screenshot+2025-04-04+at+5.24.26AM.png" alt="Heading with 'text-wrap: balance', showing smart readable behavior and wrapping" width="600" height="400" loading="lazy"></p>
<h3 id="heading-2-using-text-wrap-pretty-on-short-paragraphs">2. Using <code>text-wrap: pretty</code> on Short Paragraphs</h3>
<p>You’ve now seen how <code>text-wrap: balance</code> handles headings, so lets also take a look at how it breaks the lines evenly in your text paragraphs. As I mentioned above, the value <code>pretty</code> is mostly used for paragraphs or short block of words. Here’s how it works and appears on a block of text.</p>
<p><strong>Without</strong> <code>text-wrap: pretty</code>:</p>
<p>HTML</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"subText"</span>&gt;</span> Do you know that inconsistent text layout can ruin the look of your website design? Maybe a heading has an extra word that takes up another line or in a paragraph some lines are longer than others whereby leaving the whole thing looking messed up and hard to read.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>CSS</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.subText</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.2rem</span>;
  <span class="hljs-attribute">line-height</span>: <span class="hljs-number">1.5</span>;
}
</code></pre>
<p>In this code, text adjustment will behave normally without any smart contraints.</p>
<p>Result:</p>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_4C8BE890CB3AB8AD50C286E15DBA884FF164212E142B1E75C767C0221DB183E7_1743739991569_Screenshot+2025-04-04+at+5.11.08AM.png" alt="Paragraph without 'text-wrap: pretty', showing uneven wrapping." width="600" height="400" loading="lazy"></p>
<p><strong>With</strong> <code>text-wrap: pretty</code>:</p>
<p>CSS</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.subText</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.2rem</span>;
  <span class="hljs-attribute">line-height</span>: <span class="hljs-number">1.5</span>;
  <span class="hljs-attribute">text-wrap</span>: pretty;
}
</code></pre>
<p>The code above makes the line breaks evenly in a way that it will be easier for someone to read through.</p>
<p>Result:</p>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_4C8BE890CB3AB8AD50C286E15DBA884FF164212E142B1E75C767C0221DB183E7_1743739834917_Screenshot+2025-04-04+at+5.09.49AM.png" alt="Paragraph with 'text-wrap: pretty', showing smart readable behavior and wrapping" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-text-wrap-works-in-responsive-design">How <code>text-wrap</code> Works in Responsive Design</h3>
<p>When you use <code>text-wrap</code> on your text, you need not to worry how it is going to appear on various screen sizes. The below video shows you what I mean by that:</p>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_4C8BE890CB3AB8AD50C286E15DBA884FF164212E142B1E75C767C0221DB183E7_1743738297774_ScreenRecording2025-04-04at4.37.18AM-ezgif.com-video-to-gif-converter.gif" alt="A gif showing how 'text-wrap' smoothly adjusts on a responsive screen " width="600" height="400" loading="lazy"></p>
<h3 id="heading-using-media-queries-for-extra-control">Using Media Queries for Extra Control</h3>
<p>Combine <code>media-queries</code> and <code>text-wrap</code> to have a great special kind of control on how your text appears on various screens.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2.5rem</span>;
  <span class="hljs-attribute">text-wrap</span>: balance;
}

<span class="hljs-comment">/* On smaller screens, reduce font size and apply text-balancing */</span>
<span class="hljs-keyword">@media</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">600px</span>) {
  <span class="hljs-selector-tag">h1</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
    <span class="hljs-attribute">text-wrap</span>: balance;
  }
}
</code></pre>
<p>This code ensures your heading text adapts and remains clean across multiple device sizes.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>How text displays is something every good developer should pay attention to. It plays a big role in user experience. By using <code>text-wrap</code>, you can ensure that your website layouts don't look messy or difficult to read.</p>
<p>One of the best things about using <code>text-wrap</code> in your text formatting is that it just works every time. You don’t need to bother with <code>&lt;br&gt;</code> tags, tweak <code>max-width</code>, or fight with text alignment.</p>
<p>Even though it's not yet supported by all browsers, adding it to your next project will future-proof your design so it’s always intact and good-looking.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
