Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour when using text-align justify/text-justify and right

I ran into one IE-specific problem that I just can't wrap my head around.

The following HTML and CSS can be seen live in this pen.

:: HTML

<div id="container">
    <div class="dummy">Dummy</div>
    <nav>
        <div id="right">
            <ul>
                <li>Lorem ipsum <img src="http://placehold.it/80x40"> dolor sit amet.</li>
                <li>Anal natrach, ut vas petat, <img src="http://placehold.it/80x40"> doriel dienve.</li>
            </ul>
            <div class="dummy">Dummy</div>
            <div class="dummy">Dummy</div>
        </div>
    </nav>
</div>

:: CSS

/* RESET */
* { margin: 0; padding: 0; vertical-align: top; }
ul { list-style: none; }

/* MARKUP */
#container {
    line-height: 0;
    font-size: 0rem;
    text-align: justify;
    text-justify: distribute-all-lines;
}

#container:after {
    content: '';
    display: inline-block;
    width: 100%;
}

#container > * {
    display: inline-block;
    line-height: 1;
    font-size: 1rem;
    text-align: left;
    text-justify: none; /* does not work */
}

#container nav {
    text-align: right;
}

#right > * {
    display: inline-block;
}

/* COLORS & STUFF */
#container { padding: 10px;  background: #cfc; }
.dummy { padding: 10px; background: #ffc; }
#container nav { padding: 10px; background: #ccf; }
ul { padding: 10px; background: #fcc; }

So, what's the Problem?

The content of the green div is justified, while each child of the very div in turn is given text-align: left;. Those children are: the left dummy div and the bluish nav.

The nav contains a list (red), and two dummies. For the red list's items the text-align is set to right - and there's lies the problem (or at least, there you can see it).

The first image is shifted to the left (and thus overlays/hides some piece of the text). The second image (and thus the whole second list item) is fine. This, however, changes, when changing the text. It seems as if only the image of the longest (meaning widest) item stays where it should be - all other images (if you were to create some more items) are shifted - depending on the list item's width, that is.

Now, why is that so - and how can I fix it?

The following things I found out so far:

  • When setting the li { text-align: left; } the image stays fine in between the two text portions - but I don't get right alignment, of course.
  • When removing text-justify from the #container the image stays fine as well.
  • Setting text-justify either to auto or to none does not seem to work...

Once again: this is just regarding Internet Explorer (9+).

// EDIT
In order to avoid your time being spent on something I'm not interested in, I'll post something more on what I'd like to have.

The final code must

  • keep the current/desired functionality (i.e., justified alignment);
  • work in all major browsers (current version and at least one before that).

The final code must not

  • contain floats;
  • contain absolute/relative positions.

// EDIT
Here is a screenshot of the desired result (Firefox), and one of what I get in IE...

justify-bug

like image 371
tfrommen Avatar asked Aug 07 '13 15:08

tfrommen


People also ask

What happens when you justify align text?

When you justify text, space is added between words so that both edges of each line are aligned with both margins. The last line in the paragraph is aligned left.

Why you should never use justified text?

Justifying text disrupts that even texture. Even if a page layout program is using subtle letter-spacing, or adjusting the width of the letters – these things, too, will make the texture uneven. So, justified text should really be avoided not just on the web, but whenever possible.

What does it mean for text to be right justified?

Alignment determines the appearance and orientation of the edges of the paragraph: left-aligned text, right-aligned text, centered text, or justified text, which is aligned evenly along the left and right margins.

What changes text align justify makes in CSS?

The text-justify property in CSS is used to set the text-align to justify. It spreads the words into the complete line.


1 Answers

Change your text-justify to distribute (Tested in IE10, IE9, Chrome, FF):

text-justify: distribute;

Check out the codepen to see it in action.

like image 67
Lost Left Stack Avatar answered Oct 13 '22 02:10

Lost Left Stack