Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0160a27
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#Automatic sub menu counters with CSS | ||
|
||
|
||
|
||
I really think we don't use the CSS counter functions enough. | ||
They don't even use it in the [CSS spec](http://www.w3.org/TR/CSS21/generate.html to add automatic numbering to the list of links! | ||
|
||
We can even use this CSS property to automatically count our sub menu items. | ||
This is really helpful because the count will automatically be updated in case we add more items dynamically (Ajax for instance). | ||
|
||
The markup is basic, no need to use spans or any other elements for the numbers: | ||
|
||
<pre><ul> | ||
<li> | ||
<a href="#">link</a> | ||
<ul> | ||
<li>Item</li> | ||
<li>Item</li> | ||
<li>Item</li> | ||
... | ||
</ul> | ||
</li> | ||
... | ||
</ul></pre> | ||
|
||
And then in the CSS you just have to: | ||
|
||
/* reset the counter for every sub menu */ | ||
ul > li > ul { counter-reset: items; } | ||
|
||
/* increment the counter */ | ||
ul > li > ul > li { counter-increment: items; } | ||
|
||
/* Display the total amount of items using the "after" pseudo-element" */ | ||
ul > li > ul:after { content: counter(items); } |