Dynamically Counting in CSS
In the last lesson we discovered how to add dynamically content into HTML using CSS. In this lesson we continue exploring that subject as we expand on to it the capability of adding a counter that we can dynamically use. By the end of this lesson, you will know how to add dynamic counters using CSS.
Video Transcript
00:00incredibly powerful is the thing we're going to be learning about right now and
00:04is your capability of adding counters right into your CSS making it very easy
00:09for you to add titles that have numeric items on it for example in our sample
00:14right now we're going to create a book that is online that has basically
00:18sections and chapters so we're going to have basically section one chapter one
00:24two three four and we're not going to actually put numbers directly but we're
00:27gonna use CSS to automatically add those in amazing let's jump right into it see
00:32it in action this is really really cool stuff in the last lecture we met for the
00:38first time dynamically added content into paragraphs through CSS or really to any
00:45element the next step is adding counters it is really weird really cool if you
00:51don't know programming it's gonna make it even look weirder but we're gonna jump
00:54right into it and hopefully then it will make sense so I'm going to go ahead and
00:59just add a counter reset if you're coming from the world of programming a
01:03counter reset enables you to create a new variable each time you call the counter
01:10reset you could reset the value or create a new variable all that's left
01:15for me to do is to write an ID for example section I could write here
01:19anything I want to really any textual English based characters now that I've
01:25created the section let me go ahead into my h1 and I'm gonna go ahead and ask
01:31from that h1 before to add some content to it just like we did in the last
01:36paragraph but this time around not only will I add some copy I'm gonna go
01:41ahead and also add also a variable I'm gonna add the value that's coming back
01:46from our section to do that I'm gonna call a function called counter and send
01:52into it that variable that we've created and define basically the value that will
01:58be sent so in this case I'm sending here a string of section with the space
02:01then the value that's inside of this variable and then the semicolon which is
02:07a string connecting them all together if I run my application now you will see
02:11that our application now is a section zero and if we scroll all the way to
02:15the bottom where I added a few more paragraphs and headlines you'll see that
02:20we're at section zero again all that's left for us to do to make this work is to
02:25o2 increment the value in the section so to do that I'm gonna go ahead and
02:31right before I add in my content I'm gonna ask my counter to increment and
02:36I'm gonna tell it to section to increment once I've done that that's
02:42really all I had to do and if I go ahead and save this now the values will
02:46actually start counting section one section two so far so easy but let's make
02:52things a little bit more complicated and I've created a few h2s in between
02:57the sections and those are gonna be our chapters what I want to do is I want to
03:01create another variable so I'm gonna go ahead here in the beginning here just
03:05add a space and also add here a chapter now that I have a chapter I want
03:10to go into my h2s and do the exact same thing which ate with h2s now there's a
03:15few extra things I'm gonna have to take care of for one is we want to increment
03:19our chapter now and probably display our chapter instead of our counter now what I
03:25want to do in this case is I really want to showcase the section so I'm gonna go
03:29ahead here and just copy this counter section so we'll have basically the
03:34section that we're in a dot and then the chapter that we are in if I go
03:39ahead and save this we're gonna be almost perfect because I click on refresh
03:43we'll see that oh first of all I put it in the wrong spot because I forgot to set
03:46this to be h2 so let me just save that and we'll click on refresh so we'll have
03:51your we have our sections and now we can see that one dot one one dot two one
03:56dot three and then suddenly when we're in two we're continuing from four the
04:01reason is is because we really need to reset our values again the big question
04:05is where should we reset those values now this is where it becomes a little
04:10bit tricky and a little bit weird I just want to go ahead here and also add
04:13another extra spacing there because I saw it needs it but I want to talk about
04:17that chapter I'm just gonna delete that definition here because if I created
04:22this definition here and really what I want to do is I want to reset it each
04:25time a new headline h1 comes so I would go into any of our regular attributes
04:31not in action but not into before but into a regular one because I don't know
04:35because it's not in the spec but if you put this it's not gonna work if you put
04:38it into one of the qualifiers instead of that I'm gonna go and reset the value
04:44counter reset and I would set that in a defined variable in this place would
04:49be a chapter now the problem with putting it here is that I'm creating a
04:54completely new variable I'm not only resetting it the reason is is because
04:59they're sitting in different places so literally because I put one in my body
05:03and one in my h1 and my h1 is a child of that body I'm really resetting a new
05:09completely new variable so one of the rules that I like telling people when
05:13they're working on this is don't don't reset it in more than one place so in
05:18this case when I'm resetting it this variable is only gonna be defined once
05:22to prevent a scenario where we have a conflict of interest that would be
05:27confusing so we'd only put it in one place but the advantage of putting it
05:31inside of the h1 is each time an h1 is called we would reset the value and as
05:36h1s happen only once and in between each h2 then we would reset the value so if I
05:43go ahead here and I click on refresh we'll see that our two now is two dot
05:46one two dot two as our one is continuing to work as we would expect it to work
05:51now before we wrap up I want to just talk about a couple more things in our
05:56reset counter I could set my reset with a number as well I could put here as many
06:00variables as I want putting or the variable and then another one or a
06:04variable and then an integer a variable and in the integer in the case of zero
06:09if I set my counter reset to be zero what I'm basically doing is I'm basically
06:14setting it to be that default of that reset that we were doing in another
06:19case if I wanted to reset the value to five for example then my first chapter
06:23would start up in chapter six the reason why I would start up in chapter six is
06:28don't forget that we're incrementing the value so if we're starting at five we'd
06:32be incrementing it same applies to our chapter and same applies for our
06:38incrementations we could say hey by the way when we're doing an increment of a
06:42section and we want to increment three at a time and if I increment it three
06:46at a time then we'll see that this time around we'll start from section
06:50three moving on to section six the default would be to increment by one but
06:55we could also say hey we don't want to increment it at all which would probably
06:58be counterintuitive so I would go ahead here and just leave it to the default
07:02or set it to be section one but default will probably do in our case so there we
07:08go those are the ways to work with automatically counted counters and
07:14variables that enable you to add a little bit more details which could be
07:19perfect for books and perfect for things where you have lists that are not
07:23necessarily in the list such as top ten things on a blog where you want to put
07:27the the number right next to each blog post but you don't want to literally put
07:31numbers there because you don't want your editor to need to go through and
07:35change copy each time you want it to be automatic you could set that up very
07:39easily very smartly using CSS and that wraps really our conversation of these
07:49dynamic features that we have with CSS and we'll continue looking at some
07:53advanced stuff further on in the remainder of this chapter
