Giter VIP home page Giter VIP logo

fewpjs-dom-editing-lab's Introduction

DOM Editing Lab

Learning Goals

  1. Identify that DOM nodes are written as HTML

Introduction

We've started looking at the DOM and its structure. Now it's time to see what we can do with it.

Identify That DOM Nodes Are Written As HTML

Syntax

When viewing the DOM (through DevTools' Elements tab) we see HTML that is a clone of the HTML found in the source HTML file. DOM nodes represent all components that make up a web page.

DOM nodes most often have a starting tag and an ending tag. Because of this, something else could nest inside. This inner node is called a child node. The outer node is called a parent node.

An example of a normal tag is a paragraph:

<p>I am a paragraph.</p>

Another normal tag is a main section:

<main></main>

To nest items in a normal tag, we simply add the child node HTML element content between its parent's starting and ending tags:

<body>
  <main>
    <p>I am a nested paragraph, inside the main element, inside the body!</p>
  </main>
</body>

Some nodes only have a starting tag. Those are called self-closing elements. These elements do not have any content nested inside of them. More technically, they are called void elements. Void elements cannot be parent nodes.

An example of a self-closing tag is an image:

<img
  src="https://media.giphy.com/media/3o6MbkZSYy4mI3gLYc/giphy.gif"
  alt="A policeman"
/>

In self-closing tags, the trailing / is optional. This is valid too:

<img
  src="https://media.giphy.com/media/3o6MbkZSYy4mI3gLYc/giphy.gif"
  alt="A policeman"
/>

Every HTML element has a display value. Since these are known by modern browsers, you don't have to worry about specifying the value unless you want to change it. This value can be many things (including none, which hides the elements), but the default value for most elements is either block or inline. For the images above, the value is inline.

Instructions

Enough chit-chat, let's write some HTML!

First, we need a way to view the results of our HTML in a browser.

If you're working on your own machine, from the terminal in the directory you're working in, run open index.html to open up a copy of the file in your browser. Or, run explorer.exe index.html if you are using a WSL/Ubuntu local environment.

Just to speed things up a bit, paste the following code into index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Introduction to the DOM Lab</title>
  </head>
  <body>
    <!--All our work for this lesson will go here-->
  </body>
</html>

Refresh the page to see the changes.

Open the Google Developer Tools. Click on the "Elements" tab. Here we have the DOM representation of the HTML source the browser loaded. Use the "Elements" window to see that the body node is, temporarily, child-less.

First, let's add a title to our page:

<h1>My HTML adventure</h1>

Refresh the page and view the Elements tab again. You should see that a new child-node has appeared underneath body. You can use the disclosure triangle to see which children are wrapped or nested within the body tag.

disclosure triangle

Next, we'll add a paragraph below the title. We'll also add some highlighted bits of text to the paragraph to make it stand out a little.

<p>
  We're writing HTML markup to display in our <strong>browser</strong>. We're
  basically telling computers what to do. <em>Neat!</em>
</p>

Save the file and check out the page in the 'Elements' tab. What's happening above is that we added some inline elements, <strong> and <em> to our paragraph to style things a little. The <strong> tag makes any text within look important. It's usually bold in browsers by default. The <em> tag allows us to emphasize certain text. This text is typically rendered as italic in browsers.

Let's add a link to MDN to define HTML. We'll use the<a> tag for this. Add this to our existing <p>:

We're writing
<a href="https://developer.mozilla.org/en-US/docs/Web/HTML">HTML</a> markup to
display in our <strong>browser</strong>.

Notice that HTML attributes are shown alongside their opening tag e.g. the href attribute.

Lastly, we'll add a table below the paragraph to recap some of the stuff in this lesson:

<table>
  <thead>
    <tr>
      <th>Element name</th>
      <th>Display value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>h1</td>
      <td>block</td>
    </tr>
    <tr>
      <td>p</td>
      <td>block</td>
    </tr>
    <tr>
      <td>strong</td>
      <td>inline</td>
    </tr>
    <tr>
      <td>em</td>
      <td>inline</td>
    </tr>
  </tbody>
</table>

Woah. That's a lot of markup! If you take a look at the result though, you'll see that it's a fairly complex visual โ€” it's a table! Our table consists of a header and a body. The header allows us to give the columns a name, and the table body contains the rows of content. Both <thead> and <tbody> tags contains rows, which are represented as <tr> (table row). These rows then contain columns (or cells). In the <thead> row, cells are represented as <th>, while cells in <tbody> have their content in <td> tags.

That's a lot of nesting.

Look again at the Elements tab. Expand out all the children of the table. This is the DOM tree!

Conclusion

We reviewed DOM nodes and their HTML sources, and we reviewed how the DOM is structured. We also looked at block and inline elements and their behaviors. Finally, we practiced working directly with the DOM.

Resources

fewpjs-dom-editing-lab's People

Contributors

cernanb avatar dependabot[bot] avatar graciemcguire avatar ihollander avatar maxwellbenton avatar sdcrouse avatar sylwiavargas avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fewpjs-dom-editing-lab's Issues

httpserver

In your terminal, type httpserver to start up a temporary web server.

I had no idea what this was asking me to do, I googled around and found nothing, I tried many things. I asked a mod 5 for assistance, he confirmed that it made no sense.

Can't run learn

I had to manually install the learn-co gem to be able to pass the tests.

It would be helpful to have that mentioned in the README.

Test failure due to timeout error

Received the following error after running the learn command, tests still passed on learn.co and I was able to submit and move on to the next lesson despite receiving this error.

image

DOM Editing Lab

Hello, this lab has a slight confusion in reference to self-closing tags. When you begin the lab, it is a blank page. It then asks to add is header element to the HTML file (

My HTML adventure

). When this element is added, you are prompted to refresh the live page and check Elements tab, that a "new, self-closing child-node has appeared underneath body". However, what would appear is the header tag, which is not self-closing.

Thanks!
Emilia

Screen Shot 2020-01-21 at 6 42 27 AM

Illustrate what's meant by "Disclosure Triangle"

Thanks for raising this issue! Future learners thank you for your diligence. In
order to help the curriculum team address the problem, please use this template
to submit your feedback. We'll work on addressing the issue as soon as we can.

Please fill out as much of the information below as you can (it's ok if you
don't fill out every section). The more context we have, the easier it will be
to fix your issue!

Note: you should only raise issues related to the contents of this lesson.
If you have questions about your code or need help troubleshooting, reach out to
an instructor/your peers.


Link to Canvas

Add a link to the assignment in Canvas here.

What should be changed?

Current:

You can use the disclosure triangle to see which children are wrapped or nested within the body tag.

Change to:

You can use the disclosure triangle to see which children are wrapped or nested within the body tag.
Image of Recommendation

Additional context

Add any other context about the problem here.

Missing instructions for opening index.html file from WSL/Ubuntu

The instructions on this lab say to open the index.html file either with the open index.html command or httpserver. However, neither command works if you're using a WSL/Ubuntu environment. The correct command in that case is explorer.exe index.html.

I will fix this in a pull request.

--- Sdcrouse

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.