Connect with us

    Hi, what are you looking for?

    Devs

    How to Justify Text in HTML5

    Justifying text is a common task in web design, as it helps to create a neat and professional layout by making the text align evenly along both the left and right margins. In HTML5, text justification is typically handled with CSS (Cascading Style Sheets) rather than directly within the HTML tags.

    HTML

    Justifying text is a common task in web design, as it helps to create a neat and professional layout by making the text align evenly along both the left and right margins. In HTML5, text justification is typically handled with CSS (Cascading Style Sheets) rather than directly within the HTML tags.

    Why Justify Text?

    Justifying text creates a uniform block of text that aligns with both the left and right edges of a container, much like newspaper or book text. This method is often used for improving the aesthetics of paragraphs, making the text appear more polished and organized.


    How to Justify Text Using CSS in HTML5

    You can justify text in HTML5 using the text-align property in CSS. The text-align property controls the horizontal alignment of text within a container. To justify the text, you simply use the value justify.

    Here’s how you can do it:

    Example:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Justify Text Example</title>
        <style>
            /* CSS to justify the text */        p {
                text-align: justify;
            }
        </style>
    </head>
    <body>
        <h1>How to Justify Text in HTML5</h1>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis felis velit. Nulla facilisi. Integer convallis, orci a egestas sodales, orci lorem lacinia lorem, eget pretium eros lorem eu erat. Quisque euismod, lorem sit amet tempor feugiat, mauris erat dictum arcu, sed suscipit justo nulla a dui. Nunc sit amet magna nisi. Integer auctor nec metus eget vulputate. Suspendisse vitae urna eget augue volutpat iaculis non sit amet mi. 
        </p>
        <p>
            Aenean tincidunt, arcu nec tincidunt aliquet, nisl risus maximus nunc, sed bibendum risus velit a augue. Curabitur suscipit, lectus id efficitur mollis, metus lectus tincidunt ligula, id luctus magna risus et sapien. Nulla facilisi. Proin elementum sit amet purus a fermentum. Ut et velit a mi volutpat auctor.
        </p>
    </body>
    </html>
    

    Explanation:

    • HTML Structure:
      • The <h1> tag is used for the heading, and two <p> tags are used for paragraphs of text.
    • CSS:
      • The text-align: justify; style is applied to all <p> elements. This ensures that the text inside these paragraph tags will be justified, meaning it will stretch to align both the left and right margins.

    Alternative: Justifying Text for Specific Elements

    If you don’t want to apply the justification to all paragraphs, but only to specific ones, you can use a class or ID to target particular elements.

    Example using a class:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Justify Text Example</title>
        <style>
            /* CSS to justify only paragraphs with the class 'justify-text' */        .justify-text {
                text-align: justify;
            }
        </style>
    </head>
    <body>
        <h1>How to Justify Text in HTML5</h1>
        <p class="justify-text">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis felis velit. Nulla facilisi. Integer convallis, orci a egestas sodales, orci lorem lacinia lorem, eget pretium eros lorem eu erat. Quisque euismod, lorem sit amet tempor feugiat, mauris erat dictum arcu, sed suscipit justo nulla a dui.
        </p>
        <p>
            This paragraph will not be justified because it does not have the "justify-text" class.
        </p>
    </body>
    </html>
    

    In this example, only the paragraph with the class justify-text is justified, and the second paragraph remains aligned to the left.


    Things to Keep in Mind:

    1. Legibility: Justified text can sometimes create uneven spacing between words, especially if the line length is short. This can affect readability. It’s important to test the justification on different devices and screen sizes to ensure a clean, readable appearance.
    2. Hyphenation: Justified text can also lead to awkward word breaks. To improve text appearance, you can enable hyphenation in CSS by using the hyphens property (though not all browsers support this feature).
      p {
          text-align: justify;
          hyphens: auto; /* Allows automatic hyphenation */}
      
    3. Alternative Alignments: If you decide that justification doesn’t look great in all cases, you can experiment with other text alignments, such as left, right, or center, using the text-align property.

    Conclusion

    Justifying text in HTML5 is simple to achieve using CSS with the text-align: justify; property. It helps create clean, professional-looking text blocks, commonly seen in books, newspapers, and websites. Whether you want to justify text across the entire page or just for specific elements, CSS provides an easy way to implement this feature and tailor it to your needs.

    Photo by Lukas

    Written By

    My name is Juan de Souza, a 25-year-old entrepreneur, investor, blogger, and streamer. My journey in business began at the age of 10, leveraging a dial-up internet connection in Brazil to start my first venture. Currently, I reside in Porto, Portugal, where I have been based since 2025.

    Comments
    Advertisement

    Join Our Discord Server

    Advertisement
    Advertisement

    Recommended Posts for You

    HeartstopperHeartstopper

    LGBTQIA+

    Check out the full review of Netflix's Heartstopper series and find out if the Heartstopper series delivers everything it promises in full!

    Cinema

    “One of the most memorable cartoons of the 20th and 21st centuries. Its story, characters and writing are utterly captivating. “Little Bear” is known...

    Curiosities

    Super Mario World, also known as “Super Mario Bros. 4” in Japan, is without a doubt one of the most popular games of all...

    Cinema

    “Courage the Cowardly Dog” tells the story of Courage, a dog who’s easily scared and lives on a farm with Muriel and Eustace Bagge...

    Cinema

    Who doesn’t know this epic cartoon from Hanna-Barbera, Wacky Races, where eleven competitors vied in every episode for the world title of “World’s Wackiest...

    Cinema

    Sagwa, one of the best cartoons still airing on the Futura channel. In fact, all the shows on Futura are excellent. It tells the...

    Cinema

    “Do you remember the classic cartoon Timon and Pumbaa? It’s an animated TV series produced by Walt Disney Animation, starring the warthog and meerkat...

    Cinema

    At the end of the 1930s, animation was at its peak. It was called the “Golden Age” by many film enthusiasts, where classic characters...

    Cinema

    Woody Woodpecker, one of the most beloved characters of all time. Adored by children, teenagers, and adults alike. The show itself dates back to...

    Curiosities

    Grand Theft Auto V (GTA V) is known for its vast open-world gameplay, where players can explore, complete missions, or just cause chaos. One...

    Others

    “Castelo Rá‑Tim‑Bum” premiered in 1994 on TV Cultura, delighting fans of the original “Rá‑Tim‑Bum” who were tired of its reruns, as well as other...

    Tech

    If you grew up playing console games in the 1980s and 90s and never heard this one, count yourself lucky—very lucky. Here’s the urban...

    DisneyDisney

    Cinema

    Walt Disney, one of the largest and most influential entertainment giants in the world, has a long history of innovation in animation. From its...

    Games

    “Nintendo 64” is Nintendo’s third home video game console. Commonly abbreviated as “N64,” it launched on June 23, 1996 (Japan) and on September 29 in the United States....

    Games

    How about transforming your Minecraft into an even more immersive game? With that in mind, the “Realistico” Mod Pack was developed—a texture pack with...

    Games

    You’ve definitely seen, heard about, or played Just Dance at some point—it’s simply one of the most incredible games ever created and maintained by...

    Games

    Need For Speed Heat has finally hit the streets and is increasingly reminiscent of the franchise’s golden-era titles, such as Need For Speed Underground...

    Games

    It's time to talk about Far Cry 4: a new game in the iconic open-world franchise that first arrived over 10 years ago with paradisiacal...

    Plants vs. Zombies Garden WarfarePlants vs. Zombies Garden Warfare

    Games

    Recently, Plants vs. Zombies Garden Warfare was released—it’s the new game from PopCap, famous for its mobile hits. This time, we have a review...

    What is the Meaning and Function of the Kernel?What is the Meaning and Function of the Kernel?

    Devs

    The kernel is one of the most fundamental components of any operating system (OS), acting as a bridge between the hardware of a computer...