hiding text using "text-indent"
Asked Answered
I

11

14

I'm trying to hide some text inside an <li> element using CSS by setting text-indent: -999px;.
For some reason this doesn't work when I set the direction of the document to "rtl" (right to left - my site is in Hebrew).
When direction is "rtl" the text still shows...
Anyone knows why, and a way around this?

Inna answered 8/5, 2010 at 15:41 Comment(5)
Note: I believe that that does not work in IE6. + 456bereastreet.com/archive/200510/…Oculist
you could you padding / overflow: hidden to hide your content, this works in every browser and any text direction. like this: ´width:0; height:0; padding: 400px 0 0 200px; overflow: hidden ´Florenceflorencia
if you don't get a good solution, you can always try other text hiding techniques :D css-tricks.com/css-image-replacementAlkyl
Have you tried a positive text-indent?Bangkok
i have tried a positive indent. it makes sense that it should work but it doesnt.Inna
A
20

Along with text-indent: -9999px try using display: block;. It solved for me.

Additionally, if you need the li elements to float horizontally (e.g. a horizontal menu), use float: left;.

Amal answered 31/10, 2011 at 19:21 Comment(0)
F
12

What about setting direction:ltr on the elements you're trying to give negative text-indent?

Demo: jsfiddle.net/Marcel/aJBnN/1/

Freckly answered 19/3, 2011 at 5:9 Comment(1)
+1 that's the right answer - works and only changes a property that's irrelevant (since the text gets hidden)Sonora
H
4

My problem was with text-align. In case you modify align mode of the element or parent element(s) wrapping it to left, you must give a negative index for text-indent. Otherwise you must give a positive value.

.case1{text-indent:-999px;text-align:left;overflow:hidden;display:block}

Otherwise

.case2{text-indent:999px;text-align:right;overflow:hidden;display:block}
Hyksos answered 22/4, 2015 at 19:21 Comment(0)
T
2

Try setting text-alignment to match the direction in which you are indenting text.

For example, if you use LTR and want to indent text negatively, besides adding display: block, you should also add left alignment.

Not sure for RTL, but seems logical you should indent it positively and use right alignment.

Tribulation answered 9/2, 2012 at 21:13 Comment(0)
K
2

I found the best way is to make the text a transparent color:

color: rgba(0,0,0,0);

Note: This bug still exists in firefox 12 (text-indent value is ignored on rtl)

Kingsbury answered 17/5, 2012 at 10:16 Comment(0)
P
2
color: transparent;

or

font-size:0px;
Pellikka answered 15/4, 2014 at 19:3 Comment(0)
I
2

I prefer this solution:

.hide_text { text-indent: 100%; white-space: nowrap; overflow: hidden; }
Islington answered 21/4, 2015 at 7:20 Comment(0)
I
1

You can use line-height specifying a large value, say 100px for a 30px high container.

only works if your container is limited in size. you may have to specifically set height if it isn't yet.

Intort answered 19/3, 2011 at 4:55 Comment(0)
P
0

The text-indent: -99px is an old trick, which is not the optimal way to hide text. Why not use visibility:hidden instead?

Petie answered 15/5, 2010 at 23:15 Comment(2)
Presumably this is an attempt to use a background image as content and provide real text for screen readers instead of using an <img> element with an alt attribute.Annieannihilate
i cannot use visibility:hidden because i just need the text to go away. not the whole element in which the text is placed. any solution?Inna
R
0

text-align: right; works for me

Runthrough answered 13/8, 2013 at 5:53 Comment(0)
P
0

Add overflow hidden then it will work fine.

div{
    text-indent: 100%;
    overflow: hidden;
}
<div>
 search
 <input type="text">
 </div>
Piccadilly answered 17/9, 2020 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.