83

Is there a way of hiding an element's contents, but keep its :before content visible? Say I have the following code:

HTML:

<span class="addbefore hidetext">You are here</span>

CSS:

.addbefore:before {
    content: "Show this";
}
.hidetext {
    // What do I do here to hide the content without hiding the :before content?
}

I've tried:

  • using display: none and setting display: inline on :before, but both are still hidden
  • using width: 0; overflow: hidden;, but then additional space seems to be added (?)
  • using color: transparent;, but then, of course, the content of the span still takes up space
  • using text-indent: -....px, but
    1. this is frowned upon by search engines and
    2. it seems not to work for span elements (?)

Any other ideas as to how I might do this?

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
Jon Gjengset
  • 4,078
  • 3
  • 30
  • 43

5 Answers5

136

Clean Solution

You could use visibility: hidden, but with this solution, the hidden content will still take up space. If this doesn't matter to you, this is how you would do it:

span {
    visibility: hidden;
}

span:before {
    visibility: visible;
}

Hackish Alternative Solution

Another solution would be to set the font-size of the span to zero* to a really small value. Advantage of this method: The hidden content won't take up any space. Drawback: You won't be able to use relative units like em or % for the font-size of the :before content.

span:before {
    content: "Lorem ";
    font-size: 16px;
    font-size: 1rem; /* Maintain relative font-size in browsers that support it */
    letter-spacing: normal;
    color: #000;
}

span {
    font-size: 1px;
    letter-spacing: -1px;
    color: transparent;
}

Example on jsfiddle.

Update (May 4, 2015): With CSS3, you can now use the rem (Root EM) unit to maintain relative font-sizes in the :before element. (Browser support.)


*A previous version of this post suggested setting the font size to zero. However, this does not work as desired in some browsers, because CSS does not define what behavior is expected when the font-size is set to zero. For cross-browser compatibility, use a small font size like mentioned above.

anroesti
  • 11,053
  • 3
  • 22
  • 33
  • 1
    Hmm.. That last one is actually pretty good.. I'm using this to add icons with an icon font before text, so setting the font size in pixels might be a good idea anyway.. Thanks! – Jon Gjengset Feb 06 '11 at 10:58
  • Actually, it seems as though setting font-size: 0 isn't possible..? In Chrome at least, the jsfiddle you attached still shows "Ipsum", just in very small letters.. – Jon Gjengset Feb 06 '11 at 11:00
  • Hmm, in my Firefox it does work. You could try setting a really, really low value, like `0.000000000001em`, or you could try setting `0em`. – anroesti Feb 06 '11 at 11:02
  • I'd love to give a thumbs up, but apparently I don't have enough reputation.. =) Also, 0em and 0.000001em don't work either... – Jon Gjengset Feb 06 '11 at 11:13
  • @Wolfy87 Have you read anything I wrote in the first post? Or the part about :before being a child of the targeted element from Sotiris? – Jon Gjengset Feb 06 '11 at 11:20
  • This is strange... By the way, I wouldn't have asked you for a thumbs up, if I knew that it doesn't work. :) I've updated the second solution, i hope this will work. – anroesti Feb 06 '11 at 11:21
  • 4
    The `visibility`-approach does not work in IE unfortunately. The `letter-spacing`-approach works, but the correct value for `letter-spacing` is `normal`, not `auto`. – acme Mar 05 '13 at 11:12
  • 1
    One major drawback: You cannot use this if you do not know the original font-size, e.g. because all your sizes are relative. – ThiefMaster Dec 19 '13 at 15:52
  • `font-size: 0.01em` for the span and `font-size: 100em` for the pseudo element should also do the right thing without having to reset to the root `rem` value – EoghanM Feb 26 '19 at 17:56
  • The `visibility` approach breaks tab focus for form elements. – Nathan Osman Sep 22 '19 at 01:21
10

For better browser support:

Wrap the text that should be hidden within an additional span element, and apply classes to that span to hide the text you wish to be hidden.

HTML:

<span class="addbefore">
  <span class="visuallyhidden">This text will not show.</span>
</span>

CSS:

.addbefore:before {
  content: "Show this";
}

.visuallyhidden {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

The .visuallyhidden class used above is from the current version of HTML5 Boilerplate: https://github.com/h5bp/html5-boilerplate/blob/master/css/main.css

The advantages of this solution:

  • Semantic HTML
  • Complete browser support
  • No problems with tiny text like other small font-size solutions.
  • The hidden content won't take up space

See it in action here: http://jsfiddle.net/tinystride/A9SSb/

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
tinystride
  • 144
  • 1
  • 6
  • Bootstrap uses the class `sr-only` for items that are visually hidden, but still important for screen readers. – jeffjenx Aug 14 '15 at 04:12
2

I took a similar approach as suggested here with visibility, but that still has a content box.

My solution is to simply use font-size to hide the target text.

span {
    font-size: 0;
}

span:before {
    font-size: 16px;
}
Brenden
  • 7,708
  • 11
  • 61
  • 75
1

Building on @anroesti's excellent hack, here's a solution if you need to apply in unknown contexts in terms of font size and color, i.e. you are not sure if resetting to color:black;font-size:1rem; will not mess things up:

<span abbrev-content="Intl.">International</span>

@media only screen and (max-width: 700px) {  /* very narrow viewports */
    span[abbrev-content] { font-size: 0.001em; visibility: hidden; }
    span[abbrev-content]::before { 
        content: attr(abbrev-content); 
        font-size: 1000em; 
        visibility: visible; 
    }
}

If your span content is a paragraph and not just a word, you may also need the negative letter-spacing.

EoghanM
  • 25,161
  • 23
  • 90
  • 123
-3

I don't think it's possible with pure css and html. Looking at this example http://jsbin.com/efeco4 you will see that what is inside content property of css, is wrapped by the element. So any manipulation of the element will affect the css content also.

So an alternative thought could be to use jquery, to empty the html content inside tag div with class hidetext without affect the content of css. A sample code could be this:

$('.hidetext').empty();

Example: http://jsbin.com/efeco4/2

Sotiris
  • 38,986
  • 11
  • 53
  • 85
  • I personally would recommend using CSS over JavaScript, as some users may have that **deactivated**. – anroesti Feb 06 '11 at 10:51
  • @andre_roesti Well for the 1% of users that have deactivate javascript, I think it's time to take a step further – Sotiris Feb 06 '11 at 11:03
  • 1
    I think depending on the target group more than just 1% have deactivated JavaScript, and there are [good Reasons](http://programmers.stackexchange.com/questions/26179/why-do-people-disable-javascript) for doing that. – anroesti Feb 06 '11 at 11:06