26

To format paragraphs, I use text-align: justify, but I have the problem that there are big spaces between words. In Firefox and older IE, one solution was to use text-justify: distribute, but Chrome doesn't support it.

How can I solve this problem across browsers?

Example of big spaces:

p {
  width: 200px;
  text-align: justify;
}
<p>
  9 mln litų bendrovei „Lietuvos geležinkeliai“ kainavo aukso medalis, laimėtas Lietuvos pramonininkų konfederacijos apdovanojimuose „Metų gaminys 2012“ už Vilniaus kuro bazės, skirtos geležinkelių riedmenims, modernizavimą. 
</p>

0

10 Answers 10

49

Give negative values as you prefer for word-spacing..

ex:

text-align:justify;
word-spacing:-2px;

Works for me and Hope this helps :)

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately it works partially, it does not work for all lines, some lines maintain large spaces.
28

Use:

word-break: break-all;

And Ok!

3 Comments

Using this with text-align: justify; worked for me
In my case (there is a text in a paragraph) this will break the words. Not so good for the continous reading.
Partially worked for me, characters that don't fit will go on a new line, alone
10

Consider using hyphenation (manual, CSS, server-side, or client-side JavaScript), see e.g. answers to Can I use CSS to justify text with hyphenating words at the end of a line? Hyphenation tends to help a lot when there are long words in the text.

You can still keep text-justify: distribute, as it can improve the result on supporting browsers, and it can be expected to gain support, as it in the CSS standardization track (in CSS Text Module Level 3 WD).

2 Comments

Thank's, but I steel have problem width Chrome
Chrome does not do hyphenation yet, so for Chrome, you are limited to the other options. Hyphenator.js seems hyphenate Lithuanian OK; demo: cs.tut.fi/~jkorpela/js/hyphlt.html8
8

I got something satisfying by combining both methods:

  • enable hyphens (using two variants for compatibility)
  • negative word spacing (no more than -0.05em otherwise text get condensed)

div.justify {
    text-align: justify;
    hyphens: auto;
    -webkit-hyphens: auto;
    word-spacing: -0.05em;
}

div.font {
    font-size: 110%;
}
<div class="justify font">In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. It is also used to temporarily replace text in a process called greeking, which allows designers to consider the form of a webpage or publication, without the meaning of the text influencing the design.</div>

1 Comment

Tnx, only hyphens: auto; helped to my justified texts to not have extreme gaps. Other solutions did not work. Tnx.
6
text-align: justify;
text-justify: distribute;
text-align-last: left;

hope this will help you

Comments

4
hyphens: auto;
text-align: justify;
word-spacing: -0.5px;

Add these three lines to your text field css rule and see, while it may not give you perfect result, it will surley come close to what you're trying to achieve.

Comments

0

what worked for me was adjusting the font-size

1 Comment

This is at best a workaround and at worst a design compromise. I don’t see what value this answer offers when there are a number of established technical options already provided here.
-1

How do you want to format the paragraphs? Do you mean the width, height, letter spacing or word spacing?

Have you tried using the text-align CSS tag?

text-align:center

Or the word-spacing CSS tag?

word-spacing:10px

4 Comments

He's using text-align:justify
I'm about that space between words are not same 1 is biggest that 2
@Lopezito Perhaps try using the CSS word spacing property then. It defines the spaces between words. developer.mozilla.org/en-US/docs/CSS/word-spacing
Those aren't "CSS tags"! They're CSS Properties
-2

I have an alternate solution to get rid of the big spacing between word first you should know one thing that text-align:justify is used only when you are rendering your text component on wider screen so in my case I was using it on card custom component so I simply increase the width of my card component and this helps me hope it will help you to.

Card.js

import React from 'react';
import styles from './Card.module.css'

const Card = (props) => {
    return (
        <div className={styles.card}>
            {props.children}
        </div>
    );
} ;


export default Card;

Card.module.css

.card {
          height: 30rem;
          width: 25rem;
          margin: 0 20px 10rem;
          text-align: justify;
         
    

}

Calling card component in HOC

import React, { useEffect, useState } from "react";
import projectStyles from "./project.module.css";
import Card from "../UX/Card";
import axios from "axios";

const Project = () => {
  const [loadedProjects, setLoadedUsers] = useState([]);

  useEffect(() => {
    const fetchUsers = async () => {
      try {
        const responseData = await axios("api/projects");

        setLoadedUsers(responseData.data.projects);
      } catch (err) {}
    };
    fetchUsers();
  }, []);

  const displayHandler = (
    <div className={projectStyles.projectHolder}>
      {loadedProjects.map((project,index) => {
        return (
          <Card key={index}>
            <img src={project.image} alt="project" height={225} width={345}  style={{marginBottom:"20px"}}/>
            <p style={{fontSize:'25px' , fontWeight:'bold'}}>{project.title}</p>
            <p className={projectStyles.body}>{project.info}</p>
            <h4>Technologies Used</h4>
            <ul key={project.image}>
              {project.technology.map((tech) => {
                return <li key={tech}>{tech}</li>;
              })}
            </ul>
          </Card>
        );
      })}
    </div>
  );

  return <div>{loadedProjects ? displayHandler : 'Fetching Projects...'}</div>;
};

export default Project;

Comments

-2

I don't know how anyone didn't think of this but this worked for me.

text-align: start;

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.