78

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?

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
elveti
  • 2,316
  • 4
  • 20
  • 27

6 Answers6

119

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>
isherwood
  • 58,414
  • 16
  • 114
  • 157
xpy
  • 5,481
  • 3
  • 29
  • 48
  • 7
    complement of infos : http://www.w3.org/wiki/CSS/Properties/visibility collapse This value causes the entire row or column to be removed from the display, and the space normally taken up by the row or column to be made available for other content. This value is used for row, row group, column, and column group elements. If used on elements other than rows, row groups, columns, or column groups, 'collapse' has the same meaning as 'hidden'. – Milche Patern Jul 09 '13 at 15:13
  • nice but doesn't work with image as an icon before the text – carinlynchin Oct 02 '19 at 16:45
  • 1
    This worked for me, though I did have to add ```line-height:0;``` to xpy's ```#closelink```. Since no text follows this and the image links have their height set, I did not have to add a ```line-height:n``` for the ```#closelink a``` selector. – Parapluie Feb 08 '22 at 20:43
30
.closelink {
  font-size: 0px;
}

.close-link a {
  font-size: 12px;
}
Thịnh Phạm
  • 2,528
  • 5
  • 26
  • 39
  • 2
    this would also keep the border of parent, if present. Worked for me :) – Shishir Arora Dec 15 '15 at 11:16
  • 1
    @Maxime Lorant, this answer worked perfectly, for my situation. Sometimes it's the simpler things in life that people need. Pure CSS for the win! I needed to hide content in a td and then dynamically convert the content and then display it in a child element of the same td parent. – Alexander Dixon Jan 21 '16 at 18:08
  • 3
    This answer worked for me, the accepted solution didn't (it keeps parent space visible). – Fernando Jun 20 '16 at 14:30
  • Worked for me, removes the space also. – kiltek Sep 05 '18 at 10:32
  • This `font-size: 0` works best if you need to 'remove' the space taken up by the text on the element. It worked best for my case than `visibility`. – Reuel Ribeiro Feb 24 '20 at 13:54
5

Try

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

#closelink a {
  position: relative;
  left: 9999px;
}
<div id="closelink">
  <a href="">Close</a> Click to close
</div>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
3

It works but you can use visibility:hidden instead of visibility:collapse

jperelli
  • 6,988
  • 5
  • 50
  • 85
mamath
  • 39
  • 3
  • 5
    `visibility: hidden` keeps the space the element actually would have taken. So, this might not be an expected behavior. – Елин Й. Oct 07 '14 at 08:15
3

To avoid the child element breaking to a new line (as happens with just using visibility: hidden/collapse and visibility: visible), and to avoid drawing a 9999px block in the browser (generally frowned upon as it is unnecessary overhead), try this:

#closelink {
  position: relative;
  visibility: hidden;
}

#closelink a {
  position: absolute;
  left: 0;
  top: 0;
  visibility: visible;
}
<div id="closelink">
  <a href="">Close</a> Click to close
</div>

You can adjust your left: 0 value to provide some padding.

isherwood
  • 58,414
  • 16
  • 114
  • 157
0

There are three methods I could think of:

One

#parent {
  opacity: 1;
}

.child {
  opacity: 0;
}
<div id="parent">
  dkjfdkf
  <span class="child">Annoying text</span>
</div>

Two

.child {
  visibility: hidden;
}
<div id="parent">
  dkjfdkf
  <span class="child">Annoying text</span>
</div>

Three

.child {
  display: none;
}
<div id="parent">
  dkjfdkf
  <span class="child">Annoying text</span>
</div>

Opacity is best if you want the image to always be on the page to keep the structure but, you don't want it to be visible. Hope this was helpful.

isherwood
  • 58,414
  • 16
  • 114
  • 157