2

With CSS, how would I select the text This Text . I wish to remove/hide it.

I can't alter the HTML itself, only change the CSS.

<h2 class="hello">
      This Text
     <span class="mark">05/03/2017 3:29 PM EDT</span>
 </h2>

4 Answers 4

4

If you just intend to hide the text, do something like this

.hello {
  font-size: 0;
}

.mark {
  font-size: 30px;
}
<h2 class="hello">
      This Text
     <span class="mark">05/03/2017 3:29 PM EDT</span>
 </h2>

4
  • Nice, this is way better than mine since you don't have the empty white-space where the text was. Commented May 3, 2017 at 19:42
  • Good idea, though it requires redefining font-size. Commented May 3, 2017 at 19:46
  • @StevenMoseley yes but shouldn't be a problem in a lot of cases. Its always best to define things like font size so they look similar across browsers.
    – mehulmpt
    Commented May 3, 2017 at 19:48
  • @MehulMohan - Not sure what cases you're thinking of. I've never seen a website that doesn't define h2 font-size. In your case, if the base h2 font-size is ever changed, the .mark font-size would also have to be updated. The downside I'm talking about is that you lose font-size cascading by "reset" it in the .hello. Nevertheless, it's a good, creative answer to a problem with no great solution. Commented May 3, 2017 at 19:54
2

Here's another idea, using visibility, which won't override any font-size styling applied elsewhere.

The slight potential downside of this method is that it changes .hello to relative positioning, which may override some layout you've applied to it.

I think this is less likely than overriding font-size, though.

.hello {
    visibility: hidden;
    position: relative;
}

.hello .mark {
    visibility: visible;
    position: absolute;
    left: 0;
}
<h2 class="hello">
    This Text
    <span class="mark">05/03/2017 3:29 PM EDT</span>
</h2>

0

The only way I can think of would be to override the span inside after hiding the outer div. Something like this:

.hello .mark {
  visibility: visible;
}

.hello {
  visibility: hidden;
}
<h2 class="hello">
  This Text
  <span class="mark">05/03/2017 3:29 PM EDT</span>
</h2>

0
0

Both font-size: 0 or visibility: hidden can be used to "hide" the parent.

Combining those methods will have great benefits:

  • visibility: hidden hides the text in a manner that it won't be copied to the clipboard when selected.
  • font-size: 0 will take care of the space that would remain after manipulating the visibility. This could also be fixed by making the parent positioned relatively and the child absolutely + top: 0. However, messing with the positioning will probably cause problems - sooner or later.
  • It can't be a bad idea to define the font size anyway.

.hello {
  visibility: hidden;
  font-size: 0;
}
.mark {
  visibility: visible;
  font-size: 1.5rem;
}
<h2 class="hello">
  This Text
  <span class="mark">05/03/2017 3:29 PM EDT</span>
</h2>


This combines Steven Moseley's and Mehul Mohan's answer.

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