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: noneand settingdisplay: inlineon: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- this is frowned upon by search engines and
- it seems not to work for span elements (?)
Any other ideas as to how I might do this?
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:Hackish Alternative Solution
Another solution would be to set the
font-sizeof the spanto 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:beforecontent.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:beforeelement. (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.