Home > Web Front-end > CSS Tutorial > How Can I Justify the Last Line of Text in a Div?

How Can I Justify the Last Line of Text in a Div?

Susan Sarandon
Release: 2024-12-16 16:44:13
Original
136 people have browsed it

How Can I Justify the Last Line of Text in a Div?

Aligning the Last Line of a Div with Justified Text

In certain scenarios, it becomes necessary to align the last line of a div with justified text. While this is typically not a standard behavior for divs, a cross-browser method can achieve this desired effect.

Implementation requires a combination of text-align-last: justify; and a variation of the :after pseudo-content method. The CSS combination successfully addresses browser compatibility, including IE6 .

To apply this method:

CSS:

p, h1 {
  text-align: justify;
  text-align-last: justify;
}

p:after, h1:after {
  content: "";
  display: inline-block;
  width: 100%;
}
Copy after login

Note: If your div contains only one line of text, additional CSS is necessary to prevent an extra blank line from appearing:

h1 {
  text-align: justify;
  text-align-last: justify;
  height: 1em;
  line-height: 1;
}

h1:after {
  content: "";
  display: inline-block;
  width: 100%;
}
Copy after login

This technique successfully ensures that the last line of a div aligns with justified text, providing greater flexibility in text formatting for various web design needs.

The above is the detailed content of How Can I Justify the Last Line of Text in a Div?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template