Hide HTML element with CSS if it has specific text inside it?

1

0

It's hard to manipulate websites if they don't have own class for each element.

For example, how could I hide the second element from this code below?

<p>
    <p>te</p>
    <p>st</p>
    <p>ing</p>
</p>

The text never changes inside the element.

I'm using Stylish with Firefox to edit the CSS/HTML for websites.

Rookie

Posted 2013-11-28T09:58:19.867

Reputation: 1 073

Under what conditions should it be hidden? I'm assuming you're looking to hide it programmatically – CLockeWork – 2013-11-28T10:02:30.013

It should be hidden if the HTML element has "st" text inside it, and its the second element there. I dont know any better way to tell which element to hide, unless CSS has a feature to point at n'th element somehow? – Rookie – 2013-11-28T10:05:35.533

CSS does indeed have such a feature: http://www.w3schools.com/cssref/sel_nth-child.asp

– CLockeWork – 2013-11-28T10:22:02.223

Answers

2

I don't know of a way of using logic (identifying a value in a tag and doing something particular) using just CSS and HTML, you'd need Javascript or some such for this. But if you're looking to hide the second p element in the text block you can do this using the nth-of-type CSS selector:

Wrap your p tags in a div and give the div a class.

<div class="HideChild">
    <p>te</p>
    <p>st</p>
    <p>ing</p>
</div>


Then in your css create a selector like this:

.HideChild p:nth-of-type(2)
{
display: none;
}


Wrapping the p tags in a div and using a class means you can reuse this function for multiple text blocks on your page. If you want to change which line is hidden change the number after nth-child, and if you want the page to show a gap where the line should be replace display: none with visibility: hidden.

CLockeWork

Posted 2013-11-28T09:58:19.867

Reputation: 1 887

Seems to work, but it doesnt count which type of element it is, lets say there are bunch of links inside the class HideChild, and some <p> tags that i want to hide. then when the amount of links changes, the nth-child function doesnt know which element to hide anymore. – Rookie – 2013-11-28T11:25:25.763

Another option is to iterate through them and use jquery's $(this).contains(""); – nerdwaller – 2013-11-28T11:28:17.260

Good point @Rookie, updated my answer to use the nth-of-type selector instead – CLockeWork – 2013-11-28T11:35:40.350

Works flawlessly now! Where do you find all these features of CSS? Is there any site with list of all CSS commands and i could learn them one by one? – Rookie – 2013-11-28T11:51:00.000

@nerdwaller, is it possible to use JS with Stylish? – Rookie – 2013-11-28T11:52:36.650

@Rookie; http://www.w3schools.com/ Will teach you everything you need to know, and has reference pages for everything too. It covers HTML, CSS, JavaScript, SQL, PHP... It's how I learned anyway :D

– CLockeWork – 2013-11-28T12:12:31.643

@CLockeWork, what are these magics such as "nth-of-type" called in there? I cant seem to find a page with those listed. – Rookie – 2013-11-28T14:25:40.707

@Rookie, these are CSS Selectors http://www.w3schools.com/cssref/css_selectors.asp (and they can revolutionize the way you build your sites :D)

– CLockeWork – 2013-11-28T14:37:47.003

@rookie - Sorry to say, I've never used stylish. I tend to favor userscripts for an application like that. – nerdwaller – 2013-11-28T18:06:40.123

@nerdwaller, application like what? its not like every website has an user-script application. – Rookie – 2013-11-28T18:21:27.447

@rookie - to hide a specific element on a given page, or fix how a specific page looks. – nerdwaller – 2013-11-28T19:26:53.173

@nerdwaller, how can i use those userscripts? I want to do some more stuff to the HTML which i cannot do with CSS. – Rookie – 2013-12-02T17:11:50.333

@Rookie - Take a look at the Greasemonkey Wiki, you should be able to find whatever you need there. Also, you can look at source of userscripts on Userscripts.org. The header is the required piece, beyond that - do whatever you need.

– nerdwaller – 2013-12-02T17:29:03.703