Home > Web Front-end > CSS Tutorial > How Can I Make Text Perfectly Fill a Container\'s Width Using CSS?

How Can I Make Text Perfectly Fill a Container\'s Width Using CSS?

Patricia Arquette
Release: 2024-11-25 10:44:11
Original
658 people have browsed it

How Can I Make Text Perfectly Fill a Container's Width Using CSS?

Stretching Text to Fit Container Width

Enhancing text aesthetics within a constrained div is a common challenge. When text content varies, it's desirable to maintain a polished and balanced visual appearance. This article explores how to achieve this seamless integration using a combination of CSS and a clever technique.

Solution: Text-Align Justification and a Subtle Trick

The CSS property text-align:justify evenly distributes text within a div, aligning it along both the left and right margins. However, standard justification maintains specified letter spacing. For a truly snug fit, a small hack adds a transparent span with a 100% width.

div{
  background-color:gold;
  text-align:justify;
}

span{
  background-color:red;
  width:100%;
  height:1em;
  display:inline-block;
}
Copy after login
<div>
  Lorem ipsum sit dolor
  <span> </span>
</div>
Copy after login

This trick essentially creates a "dummy" space that fills the remaining width of the div, forcing the text to expand and tightly align within its container.

The above is the detailed content of How Can I Make Text Perfectly Fill a Container\'s Width Using CSS?. 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