2

I have a html like this:

<div class="portfolio-descr">
    <span class="posted-in"></span>                             
    <h3><a href="">Name</a></h3>           
    Some text <!-- HIDE THIS -->
</div>

I want to hide Some text but not the <h3> tag, purely with CSS like this:

.portfolio-descr:not(h3) {
  display:none;
}

But all the elements are hidden.

6
  • 1
    can't you wrap Some text in some tag? P.S you probably meant to write .portfolio-descr *:not(h3) { ... } Commented Dec 18, 2016 at 5:26
  • No, I can not. That why I have to use except in css Commented Dec 18, 2016 at 5:29
  • 1
    Possible duplicate of css hide text node but show its children Commented Dec 18, 2016 at 5:34
  • If you cannot change the HTML, please add that fact to your question. Commented Dec 18, 2016 at 7:07
  • The question which was suggested as a dup target has a number of good solutions, including using margins and/or a font size of zero. Commented Dec 18, 2016 at 7:20

3 Answers 3

3

If you use display property it will affect both parent and its children, but you can use visibility property to achieve your goal. (I added some text to .posted-in span to show the result better)

.portfolio-descr{
  visibility:hidden;
}

.portfolio-descr>*{
  visibility:visible;
}
<div class="portfolio-descr">
        <span class="posted-in">abc</span>                             
        <h3><a href="">Name</a></h3>           
        Some text
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

But I believe this will leave empty space where the "Some text" was, and that may not be what the OP wants.
2

put space between parent class and descendant h3 otherwise h3 considered as part of parent class portfolio-descr

.portfolio-descr > :not(h3) {
    display:none;
}

Correct HTML :

Put some text inside inline/block html element as per need

<div class="portfolio-descr">
    <span class="posted-in"></span>
    <h3><a href="">Name</a></h3> <span>Some text</span>
  </div>

Working plunker

2 Comments

Please check plunker.Its working .Please inspect html wheather there any other css overwriting
I do not believe this will hide "Some text" as the OP wants.
0

If you want to hide just the text but not h3 and span tags,one of the options is to make the font-color and background to be the same

check this snippet

.portfolio-descr:not(h3):not(span) {
  color: white;
  background: white;
}
<div class="portfolio-descr">
  <span class="posted-in"></span> 
  <h3><a href="">Name</a></h3> Some text
</div>

Hope it helps

1 Comment

I don't think this does what you think it does. The .portfolio-descr:not(h3):not(span) selects an element which both has the .portfolio-descr class, and is itself neither an h3 nor a span. Your code only works because the h3 does not inherit background color.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.