2

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
  • 1,223
  • 9
  • 28
  • 48
  • Under what conditions should it be hidden? I'm assuming you're looking to hide it programmatically – CLockeWork Nov 28 '13 at 10:02
  • 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 Nov 28 '13 at 10:05
  • CSS does indeed have such a feature: http://www.w3schools.com/cssref/sel_nth-child.asp – CLockeWork Nov 28 '13 at 10:22

1 Answers1

3

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
  • 2,087
  • 1
  • 18
  • 26
  • 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

    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 Nov 28 '13 at 11:25
  • Another option is to iterate through them and use jquery's `$(this).contains("");` – nerdwaller Nov 28 '13 at 11:28
  • Good point @Rookie, updated my answer to use the nth-of-type selector instead – CLockeWork Nov 28 '13 at 11:35
  • 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 Nov 28 '13 at 11:51
  • @nerdwaller, is it possible to use JS with Stylish? – Rookie Nov 28 '13 at 11:52
  • @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 Nov 28 '13 at 12:12
  • @CLockeWork, what are these magics such as "nth-of-type" called in there? I cant seem to find a page with those listed. – Rookie Nov 28 '13 at 14:25
  • @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 Nov 28 '13 at 14:37
  • @rookie - Sorry to say, I've never used stylish. I tend to favor userscripts for an application like that. – nerdwaller Nov 28 '13 at 18:06
  • @nerdwaller, application like what? its not like every website has an user-script application. – Rookie Nov 28 '13 at 18:21
  • @rookie - to hide a specific element on a given page, or fix how a specific page looks. – nerdwaller Nov 28 '13 at 19:26
  • @nerdwaller, how can i use those userscripts? I want to do some more stuff to the HTML which i cannot do with CSS. – Rookie Dec 02 '13 at 17:11
  • @Rookie - Take a look at the [Greasemonkey Wiki](http://wiki.greasespot.net/Main_Page), you should be able to find whatever you need there. Also, you can look at source of userscripts on [Userscripts.org](http://userscripts.org). The header is the required piece, beyond that - do whatever you need. – nerdwaller Dec 02 '13 at 17:29