Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide text node in element, but not children

Tags:

css

textnode

I'm having a little trouble with CSS and can't seem to find a solution. I have this HTML

<div id="closelink">
  <a href="">Close</a>
  Click to close
</div>

Now I want to hide the text «Click to close» only, without hiding neither the div, nor the link within it.

Can this be done?

like image 312
elveti Avatar asked Mar 04 '13 07:03

elveti


People also ask

How do you hide text in an element?

To hide text from a screen reader and hide it visually use the hidden attribute. You can also use CSS to set display: none or visibility: hidden to hide an element from screen readers and visually.

How do I hide text messages without tags?

visibility:hidden , color:transparent and text-indent will do this. the font-size:0 option will work better I think. It should be remembered that some of these solutions will only hide the text. They will still leave a space for it on the screen.

How do I hide an element without removing DOM?

Try setting display:none to hide and set display:block to show. not all items should be display: block , some will have to be set to display: inline (such as <span> or <a> or <img> DOM elements). @pranay the question says to hide them not to remove them from the DOM.


3 Answers

The visibility attribute can be overriden on children elements, so you are able to do this:

#closelink {
  visibility: collapse;
}

#closelink a {
  visibility: visible;
}
<div id="closelink">
  <a href="">Close</a> Click to close
</div>
like image 106
xpy Avatar answered Oct 21 '22 12:10

xpy


.closelink {
  font-size: 0px;
}

.close-link a {
  font-size: 12px;
}
like image 34
Thịnh Phạm Avatar answered Oct 21 '22 12:10

Thịnh Phạm


Try

#closelink {
  position: relative;
  left: -9999px;
}

#closelink a {
  position: relative;
  left: 9999px;
}
<div id="closelink">
  <a href="">Close</a> Click to close
</div>
like image 36
Dipesh Parmar Avatar answered Oct 21 '22 12:10

Dipesh Parmar