How to make ‘text-overflow: ellipsis’ work for inputs in IE

A simple explanation how to make overflow: ellipses work in Internet Explorer.

Months ago I had to stylize inputs with fixed width, but very long content. I wanted to show an ellipsis (“…”) to represent the clipped text.

in-chrome

<div class="long-value-input-container">
	<input type="text" value="sdghkjsdghhjdfgjhdjghjdfhghjhgkdfgjnfkdgnjkndfgjknk" class="long-value-input" readonly />
</div>  

To do so i used this CSS rule:

		.long-value-input {
			width: 200px;
			height: 30px;
			padding: 0 10px;
			text-overflow: ellipsis;
			white-space: nowrap;
			overflow: hidden;
		}
		

It worked just fine in Chrome, but in IE the ellipsis were not shown.

in-ie

After several days in struggle I found a simple solution. To display them the input had to have the readonly attribute. But what if I wanted to write in it? I decided to use javascript to toggle its attribute value everytime when click and blur events are fired.

		// making the input editable
		$( '.long-value-input' ).on( 'click', function() {
		    $( this ).prop( 'readonly', '' );
		    $( this ).focus();
		})

		// making the input readonly
		$( '.long-value-input' ).on( 'blur', function() {
		    $( this ).prop( 'readonly', 'readonly' );
		});
		

Working example:
https://github.com/marianadgeorgieva2/How-to-make-text-overflow-ellipsis-work-for-inputs-in-IE.git

12 thoughts on “How to make ‘text-overflow: ellipsis’ work for inputs in IE

  1. Max says:

    I’ve scoured the internet. This is the only working “hack” I’ve found to get ellipsis working in IE input fields.

    However, there’s a significant (in my opinion) drawback: when selecting/focusing the input field, you have to click twice to get it to be editable. This is a dealbreaker for me since I have a bunch of fields that will be need editing. Bummer 😦

    Like

      • Krish Smith says:

        Hi Mariana,

        This issue is reproducible in all IE version including IE 11 also. I just run your code from github. If we remove readonly element completely from the DOM on click, it seems to be working.

        Like

  2. Chris N. says:

    I couldn’t believe that this problem was all because of a missing readonly attribute… I spent hours trying every possible solution before I came across this one. Thanks a lot for the informative post!

    Like

Leave a comment