2

Consider below HTML

<nav id="nav-main">
    <li> 
        <a href="javascript:void(0)"> 
           <img src="http://jsfiddle.net/img/initializing.png">
         <span>Sample Link</span>
       </a>
       Hide Me with CSS
   </li>
</nav>

Is it possible to remove or hide the Hide Me With CSS with CSS?

I tried:

#nav-main li a:after{
    content: "";
}

But no luck.

http://jsfiddle.net/6powxzru/

I can not change the above HTML as this is a dynamically generated code (with a JSP tag).

8
  • First, your HTML is invalid, <li> elements are only valid under an<ul>, or <ol>, parent element. Commented Oct 10, 2015 at 14:45
  • Do you mean this? jsfiddle.net/v5Lna9r5 . You can use :last-child
    – Khanh TO
    Commented Oct 10, 2015 at 14:45
  • 2
    @DavidThomas I think you mean <ol> not <lol>. <lol>
    – Jacob G
    Commented Oct 10, 2015 at 14:47
  • Is this what you need?
    – Harry
    Commented Oct 10, 2015 at 14:47
  • 1
    @Harry yes, Please send it as answer ! Commented Oct 10, 2015 at 14:53

5 Answers 5

5

I can not change the above html as this is a dynamically generated code

Change the font-size

#nav-main li {
  font-size: 0;
}
#nav-main li a {
  font-size: 12px;
}
<nav id="nav-main">
  <li>
    <a href="javascript:void(0)">
      <img src="http://jsfiddle.net/img/initializing.png" />
      <span>Sample Link</span>
    </a>
    Hide Me with CSS</li>
</nav>

2
  • I like it. Very nice :)
    – Harry
    Commented Oct 10, 2015 at 15:00
  • It is good, the only problem is that you should now the exact font-size to set it for a Commented Oct 10, 2015 at 15:01
2

One way to achieve this is to set visibility: hidden for the li and then set it to visible for the a inside the li.

#nav-main li {
  visibility: hidden;
}
#nav-main li a {
  visibility: visible;
}
<nav id="nav-main">
  <li>
    <a href="javascript:void(0)">
      <img src="http://jsfiddle.net/img/initializing.png">
      <span>Sample Link</span>
    </a>Hide Me with CSS</li>
</nav>

2
  • The one problem with this is the text still takes up space in the layout, it just isn't visible.
    – Jacob G
    Commented Oct 10, 2015 at 14:56
  • @JacobGray: Yes but that would depend on how the element is being put to use. If it is left as block by default and the text is small then it shouldn't matter.
    – Harry
    Commented Oct 10, 2015 at 14:58
2

Remove the text node with JS:

var node = document.querySelector('#nav-main > LI > A').nextSibling;
node.parentNode.removeChild(node);
2
  • Thanks and up voted, but I was asking for CSS selector ! Commented Oct 10, 2015 at 14:58
  • Unfortunately, CSS has currently no means to select text nodes. Please note that using visibility does not prevent the hidden part of element from affecting layout. Commented Oct 10, 2015 at 14:59
1

How about you try the following approach (not sure if possible in your case, though):

  • Color li within nav with the background color of the whole container, so the text is not visible any more.
  • After that, you would style individual elements based on colors you want. If the container background is white, you would start with:

    nav-main li{color:#FFF;}

    nav-main li a{color:#000;}

etc.

0

From W3Schools:

The ::after selector inserts something after the content of each selected element(s).

This means any content you add in the after "element" will appear after but still inside the original element, which in your case is the a tag.

Also, you cannot remove text using the content property which was not added using the content property. So, to hide such text which is outside the a but inside the li on a specific event, such as hover, you can instead do something like this.

#nav-main li:after {
    content: "Hide Me With CSS";
}
#nav-main li:hover:after {
    content: "";
}
<nav id="nav-main">
    <li> 
        <a href="javascript:void(0)"> 
           <img src="http://jsfiddle.net/img/initializing.png">
         <span>Sample Link</span>
       </a>
   </li>
</nav>

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.