JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops For Loops

maya sophie
maya sophie
5,754 Points

about div and how did appeared here

I don't see any div in the html code.....where did div came from? I checked css properties and I see a #color div but how div get attached to html code???? the body is the div? or ??? thx

2 Answers

Robbie Thomas
Robbie Thomas
31,091 Points

If it answers your question, the "#" in color is an ID.

Since no other div's are selected in the code, the javascript produces those gray circles with the numbers in them.

Hopefully, I'm making sense here, if not, I'd imagine someone else better than me will comment below.

Tom Stringer
PRO
Tom Stringer
Pro Student 1,709 Points

Hi Maya,

As Robbie alluded to above - the # you see in the CSS is a selector

In this case, the hash (#) is used to select HTML entities that have an ID matching that value, so in this case #color that we see in the CSS is saying "Select any entity that has the ID of 'color' in the HTML and apply the following styles".

Furthermore in the CSS the full rule is actually #color div. This is "scoped" more specifically to select any <div> element within an element that has an ID of "color".

So it would pick up/match

<div id="color"> <!-- Doesn't match this -->
<div>Matches on me!</div>
</div> <!-- Also doesn't match on me -->

But wouldn't match

<div id="color"> </div>

In this way CSS can be used to target very specific things, or very broad-ranges of items. It also makes more sense with the naming as it is called "Cascading Style Sheets" Because as you get more specific with the selectors, they will overwrite more "broad" selectors that have already been written.

The actual <div> itself is being created in the javascript

The javascript can be run from anywhere in the HTML. If you look in to video at the 4:10 mark or so, you'll see they're popping it in to the <body> of the HTML, which means the script that they write is being run (and the HTML is then being generated in a loop) in the body of the HTML.

This wouldn't work if placed in the <head> of the document (reserved for metadata)

I suggest doing a quick course on CSS to get familiar with the scoping and possibilities!