Using nth-child css to hide list items or repeated elements

Lets say that i have a list like this:

<div class='my-awesome-list'>
<div class='a-list-item'>yay im a list item</div>
<div class='a-list-item'>yay im a list item</div>
<div class='a-list-item'>yay im a list item</div>
<div class='a-list-item'>yay im a list item</div>
<div class='a-list-item'>yay im a list item</div>
<div class='a-list-item'>yay im a list item</div>
<div class='a-list-item'>yay im a list item</div>
</div>

And we cant edit the actual html for some reason... but we want to only show the 1st two items in the list.

Well css can fix this for you by using the following.

<style>
.my-awesome-list .a-list-item:nth-child(n+3) {
display:none;
}
</style>

we can also apply this to an ordered or un-ordered list like so:

<ol class='my-awesome-list'>
<li class='a-list-item'>yay im an ordered list item</li>
<li class='a-list-item'>yay im an ordered list item</li>
<li class='a-list-item'>yay im an ordered list item</li>
<li class='a-list-item'>yay im an ordered list item</li>
<li class='a-list-item'>yay im an ordered list item</li>
<li class='a-list-item'>yay im an ordered list item</li>
<li class='a-list-item'>yay im an ordered list item</li>
<li class='a-list-item'>yay im an ordered list item</li>
</ol>

which will take on the same as the div version as the class names are the same. 

HTML

<div class='my-awesome-list'>
	<div class='a-list-item'>yay im a list item</div>
	<div class='a-list-item'>yay im a list item</div>
	<div class='a-list-item'>yay im a list item</div>
	<div class='a-list-item'>yay im a list item</div>
	<div class='a-list-item'>yay im a list item</div>
	<div class='a-list-item'>yay im a list item</div>
	<div class='a-list-item'>yay im a list item</div>
</div>

<ol class='my-awesome-list'>
	<li class='a-list-item'>yay im an ordered list item</li>
	<li class='a-list-item'>yay im an ordered list item</li>
	<li class='a-list-item'>yay im an ordered list item</li>
	<li class='a-list-item'>yay im an ordered list item</li>
	<li class='a-list-item'>yay im an ordered list item</li>
	<li class='a-list-item'>yay im an ordered list item</li>
	<li class='a-list-item'>yay im an ordered list item</li>
	<li class='a-list-item'>yay im an ordered list item</li>
</ol>

CSS

.my-awesome-list .a-list-item:nth-child(n+3) {
    display:none;
}

Demo

yay im a list item
yay im a list item
yay im a list item
yay im a list item
yay im a list item
yay im a list item
yay im a list item
  1. yay im an ordered list item
  2. yay im an ordered list item
  3. yay im an ordered list item
  4. yay im an ordered list item
  5. yay im an ordered list item
  6. yay im an ordered list item
  7. yay im an ordered list item
  8. yay im an ordered list item

Other Items in css
Page Views

This page has been viewed 91 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹