132

I have a div with overflow:hidden, inside which I show a phone number as the user types it. The text inside the div is aligned to right and incoming characters are added to right as the text grows to left.

But once the text is big enough not to fit in the div, last characters of the number is automatically cropped and the user cannot see the new characters she types.

What I want to do is crop the left characters, like the div is showing the rightmost of its content and overflowing to the left side. How can I create this effect?

overflowing phone number to left

1

7 Answers 7

182

Have you tried using the following:

direction: rtl;

For more information see
http://www.w3schools.com/cssref/pr_text_direction.asp

8
  • 23
    Warning : this does not work for a calculator display with special characters like / and * .
    – Max
    Feb 9, 2013 at 1:23
  • 11
    It also doesn't work for locales with non US number formatting, like " " for thousand separator. This is not the correct solution Feb 13, 2015 at 0:18
  • 14
    This property is not meant for alignment, and it will change the order of words inside as well. F.e. 14:00–15:00 will turn to 15:00–14:00 in Firefox.
    – Andy
    Mar 21, 2016 at 14:58
  • 3
    Doesn't this reverse the order of the characters too? Aug 3, 2016 at 14:05
  • 8
    Yes, you need to wrap the contained elements in another element with direction: ltr rule to reverse the effect. Oct 25, 2016 at 8:20
64

I had the same problem and solved it using two divs. The outer div does the clipping on the left and the inner div does the floating to the right.

.outer-div {
  width:70%;
  margin-left:auto;
  margin-right:auto;
  text-align:right;
  overflow:hidden;
  white-space: nowrap;
}

.inner-div {
  float:right;
}

:

<div class="outer-div">
  <div class="inner-div">     
    <p>A very long line that should be trimmed on the left</p>
  </div>
</div>

You should be able to put any content inside the inner div and overflow it on the left.

3
  • 10
    I wanted to see this working, so I made this JSFiddle to test it and it works great! Great answer! Thanks! Jan 6, 2016 at 19:18
  • 4
    This is a great answer as direction rtl has all sorts of side effects. If you want to ensure the inner div is left aligned and only truncated to the left if the div is exceeded, set the .outer-div to max-width: 100% and display: inline-block. See here
    – Luke
    Aug 11, 2016 at 0:28
  • 2
    Thanks for the JSFiddle WebWanderer. I updated it to use overflow: visible so the content can leak out of the left hand side. Sep 8, 2016 at 5:57
11

This worked like a charm:

<div style="direction: rtl;">
  <span style="white-space: nowrap; direction: ltr; display: inline-block;">your short or long comment<span>
</div>
1
  • Not only does this work, but also this solution does not "corrupt" your outer container with unwanted properties. Be careful and do not forget the inline-block display. Thank you! Sep 20, 2021 at 16:43
10

You can do float:right and it will overflow to the left, but in my case I need to center the div if the window is larger than the element, but overflow to the left if the window is smaller. Any thoughts on that?

I tried playing around with direction:rtl but that doesn't appear to change the overflow of block elements.

I think the only answer is to float it right, with a div to the right of it that's also floated right, then set the width of the div to the right to half the remaining window space with jquery.

1
  • Agree. Float the parent to the right and ensure no intervening containers have overflow set to hidden.
    – Lisa
    May 7, 2012 at 6:53
10

easily done, <span> the numbers and position the span absolute to the right inside an element with overflow hidden.

<div style="width: 65px; height: 20px;
            overflow: hidden; position: relative; background: #66FF66;">
    <span style="position: absolute; right: 0;">05451234567</span>
</div>

:)

rgrds jake

9

Here is an way easier solution using flexbox. It also works on pseudo elements.

p {
  display: flex;
  justify-content: flex-end;
  white-space: nowrap;
  overflow: hidden;

  font-size: 2em;
  width: 120px;
  background: yellow;
}
<p>156189789123</p>

2
  • Is it possible to add "..." to the beginning, such as is done with text-overflow:ellipsis?
    – justdvl
    Sep 25, 2022 at 15:21
  • @justdvl p::before { content: "..."; }?
    – code
    Oct 6, 2022 at 23:56
0

Modified HTML markup and added some javascript to WebWanderer's jsFiddle solution.

https://jsfiddle.net/urulai/bfzqgreo/3/

HTML:

<div id="outer-div">

    <p>ipsum dolor amet bacon venison porchetta spare ribs, tongue turducken alcatra doner leberkas t-bone rump ball tip hamburger drumstick. Shoulder strip steak ribeye, kielbasa fatback pig kevin drumstick biltong pork short loin rump. Biltong doner ribeye, alcatra landjaeger tenderloin drumstick t-bone pastrami andouille. Sirloin spare ribs fatback, bresaola strip steak alcatra landjaeger kielbasa cupim doner. </p>

</div>

CSS:

#outer-div {
  width:100%;
  margin-left:auto;
  margin-right:auto;
  text-align:right;
  overflow:hidden;
  white-space: nowrap;
  border:1px solid black;
}

JS:

let outer = document.getElementById("outer-div");
outer.scrollLeft += outer.scrollWidth;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.