-1

I'm really lost on this concept. I've read up on the DOM and the definitions of nodes but I still don't really get it in a practical sense. For instance,

<div>
  <p></p>
</div>

Would <div> be the parent and <p> be the child?

Marcus
  • 53
  • 1
  • 8
  • 2
    `

    ` is the child. Think of it like which ever is nested is the child and the one above it is its parent.

    – Vladimir Bogomolov Apr 05 '19 at 06:49
  • Thanks, do you have a link to some document where I could read more about that? I tried MDN and w3c but am having trouble finding relevant information – Marcus Apr 05 '19 at 06:51
  • 3
    Take a look at this: https://www.w3.org/TR/WD-DOM/introduction.html – Donny Apr 05 '19 at 06:53

3 Answers3

4

Exactly, in your code <div> would be the parent and <p> the child. Here is a piece of code

<html>
<title>example</title>

<body>

<head>

    <div> </div>

</head>

</body>
</html>

In the code above <div> is the child of <head>, <head> child of <body> and <body> child of <html>. Starting from <html> parent of <body>, <body> parent of <head> and so on.

Chris Claude
  • 973
  • 12
  • 25
3

Yes, <div> is the parent and <p> is the child

To understand it a bit more let's add another <p>

<div>
    <p id='first-paragraph'></p>
    <p id='second-paragraph'></p>
</div>

Now <p id='first-paragraph'> and <p id='second-paragraph'> are both children of <div>

And one more fact is that they are siblings, since they are on the same level sharing the same parent (<div>)

Ali Elkhateeb
  • 3,413
  • 4
  • 21
  • 39
  • So it's really that simple in html? if it's nested inside it's a child and if it's outside its a parent? And a node is just an element? – Marcus Apr 05 '19 at 06:58
  • @Marcus you can refer to [this](https://stackoverflow.com/q/9979172/7021694) for more explanation on node vs element – Ali Elkhateeb Apr 05 '19 at 07:09
2

the html structure is same like tree structure first is root that is html and elements under direct is its child and this goes on nested with elements add under this order the image may give u clear pictureenter image description here