I have several <p> elements in my HTML body. I only want to show the first two paragraphs, and set display:none to all paragraphs after. Why does the following code not work?
<html>
<head>
<style type="text/css">
p:gt(2) { display:none; }
</style>
</head>
<body>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
</body>
</html>
My code still shows all 4 paragraph elements in Chrome web browser.
How do I correct my code to achieve the objective I originally stated?
If they’re siblings the easiest approach with some backwards compatibility would be:
JS Fiddle demo.
You could also use:
JS Fiddle demo.
References:
:nth-of-type()pseudo-class.+) combinators.~) combinators.