1

Let's say I have a div with some small text in it. The intention is that this text should fit into "only" one line. When the container width is big, the text should be justified and when the container width is small, the text should still be in one line and the font size should decrease to preserve being in one line. (Not hiding its over-flow)

I tried text-align: justify; but it doesn't work. since it does not justify the last line. (which in this case, the last line is actually the only line we have.)

Any help one this one is appreciated. Thanks in advance.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ali Baghban
  • 517
  • 1
  • 5
  • 13

1 Answers1

0

there!

If you really want to fit the text in one line, I would do something like this:

div {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1; /* no. of lines to be shown */
  overflow: hidden;
  
  
  /* general styling */
  box-sizing: border-box;
  width: 70%;
  margin-inline: auto;
  padding: 0.25em 0.5em;
  background-color: #ccc;
}

p {
  /* remove default margin */
  margin: 0;
}
<div>
  <p>
    Lorem ipsum dolor, sit amet consectetur adipisicing elit. Dicta odit corporis perferendis odio nesciunt nihil harum magni ipsam quidem saepe, iusto, praesentium impedit vel commodi exercitationem nam possimus architecto fugit.
  <p>
<div>

However, if you don't want your text to be overflown and responsive, you can use the calc() operator to calculate the font-size based on the width of the div.

Alternatively, clamp() is also a good option to go with. But, you need to play around with the numbers.

You can read more about calc() and clamp() operations in MDN Docs.

I hope it helps! :)

  • This solution has the problem of `height`. and for `calc` i should actually test it for a variety of screens and devices. – Ali Baghban Dec 13 '22 at 19:11