This lesson will walk you through the fundamentals of how web pages are built. They are made up of three different components (HTML, CSS, and JavaScript), which you might think of loosely as the components of a house.
| component | ... provides the | ... and consists of |
|---|---|---|
| HTML | structure of the house (e.g., foundation, walls, rooms) | HTML elements |
| CSS | look of the house (e.g., paint and decor) | CSS rules |
| JavaScript | interaction with the house (e.g., make switches turn on lights) | JavaScript variables |
Take a moment and play with the interactive house drawing below then answer the following three questions.
Which component is responsible for putting the roof above the interior, the interior above the foundation, and the rooms inside the interior?
Which component is most likely responsible for the color and fonts used for the different house elements?
Which component is most likely responsible for the effect of hovering and clicking on the rooms?
All HTML code consists of elements. The diagram below shows the structure of an HTML element.
Example 1 is of a single HTML p element, which is normally (as in this example) used to contain a paragraph of text.
HTML elements can be nested within each other, creating child and parent relationships. In example 2, we have a p element nested within a div element. You could also refer to the child element as the content of the parent element. You might do this to group text content with other content. Note that elements are allowed to be empty (i.e., for the closing tag to come immediately after the opening tag).
Both example 1 and example 2 show how we set element attributes. HTML attributes change the behavior of that element and different elements support different combinations of attributes (class and style are examples of global attributes supported by all element types). For example, the video element has an optional autoplay attribute that indicates whether the video should immediately play. Some of those elements have special attributes that change the way they behave. But for this lesson you will only need:
1. Which type of HTML element is shown in example 1?
2. Add a div element to the HTML code window on the right. Give it a class attribute with the value "example".
3. Open the official reference on HTML elements and review a few of the sections. What questions come to mind? How might you go about answering them?
For this next step, you will go ahead and build out the main structure for the house. We will refer to each element according to its class attribute (since they will all be div elements and so that won't be a useful way to refer to them).
The roof, interior, and foundation elements should be immediate children of the house element. And there should be three room elements that are each direct children of the interior element. They are also described below. To finish this step, create all your elements in the HTML code area to the right.
| class | parent | content |
|---|---|---|
| house | ||
| roof | house | Roof |
| interior | house | |
| foundation | house | Foundation |
| room | interior | Room |
Before we move on from HTML over to CSS, let's take a look at how we can style our HTML elements. At the start, we said that styling is done with CSS. But the lines are actually a little blurry. We can use the style attribute to add inline styles to our elements.
For this step, please add a style attribute to each of the room elements in the HTML. The value should be the same for all of them:
width: 100px; height: 50px; background-color: yellow;"
You might find yourself with overly long lines of code. Note that, in HTML you can insert a line break anywhere a space would normally go.
You might think that styling elements in HTML looks inelegant, especially with how we have to keep repeating the same style attributes. And that's why we use CSS. CSS allows you to declare a rule once and then apply it to a large number of elements. A CSS rule looks like the following.
CSS provides flexibility in how a developer can select elements for styling. You can select by class by prefixing it with a "." and you can select by tag name by using the tag name alone. You can also combine them together to create rules spanning tag name and class. Here are some examples.
| Selector | will select ... |
|---|---|
| p | Any p element, regardless of class. |
| div.car | Any div element with class car. |
| div.car.green | Any div element that has class car and green (i.e., an element can have more than one class by separating them with a space, such as with class="car green"). |
| div.car .seat | Any div element that has class seat and is inside a div element having class car. |
| .car .seat | Any element that has class seat and is inside an element having class car (does not matter if the containing element is a div or not). |
There are also many CSS properties. The example shows two (background-color and width).
For this next step, you should replace the styling from the last step with a CSS rule (which means you should no longer have style attributes set in your HTML). Add a CSS rule that selects elements with class room (i.e., .room) and sets the width, height, and background-color properties to the same values as last time. Don't forget to refer to the required syntax above (especially the curly braces and semicolons).
We now need to finish styling the remaining elements. Please create CSS rules to apply the styling as laid out in this table. Note that you should probably group some of these to avoid writing them several times. For example, you may want to define the width of house, foundation, and roof using a single rule.
| class | selector | Property | Value |
|---|---|---|---|
| house | .house | width | 350px |
| foundation | .foundation | width | 350px |
| roof | .roof | width | 350px |
| foundation | .foundation | height | 25px |
| roof | .roof | height | 25px |
| house | .house | text-align | center |
| roof | .roof | background-color | lightgray |
| interior | .interior | display | flex |
| interior | .interior | justify-content | space-between |
| room | .room | width | 100px |
| room | .room | height | 50px |
| room | .room | background-color | yellow |
| room | .room:hover | background-color | lightyellow |
| foundation | .foundation | background-color | gray |
You probably have picked up on what the width, height, and background-color properties do and how the selectors were defined (the . prefix chooses by class). But some of these probably deserve some explanation.
| Selector | Property and Value | Description |
|---|---|---|
| .room:hover | background-color: lightyellow | Sets a background color for the room class, but only when the user is hovering over it. |
| .interior | display: flex | Makes the interior lay internal elements horizontally. |
| .interior | justify-content: space-betwen | Puts space between the horizontal elements in the interior element. |
The :hover pseudo-class is a common tool for communicating interactivity to a user. And the flex layout is a common way to define the layout of elements along an axis. There are many ways to lay out HTML elements and we will explore them more in the future.
We now move on to JavaScript. As we said at the start, JavaScript consists of variables and operations on those variables. Variables store information and functionality that we can access later.
We'll start by declaring two kinds of variables: strings and numbers. Strings allow us to store textual information. Numbers allow us to count and perform calculations. Here is the syntax for a string.
And here is the syntax for a number. Note that they are very similar! Note that we use the keyword var to declare the variable. However, if you wish to change a variable later on, you simply re-assign it using the equal sign and you do not use var again. You can think of the var keyword as meaning "I am declaring a new thing into existence" and the equal sign as saying "and this thing's value is ___". Naturally, once it exists you are free to change its value without declaring it again.
Okay, your turn. Please do the following:
Another type of variable is a function. Functions represent sets of instructions. Of course, most things in JavaScript are instruction (e.g., declare this variable, modify that variable). But functions allow us to package instrucions up so that they can be used multiple times without having to write the same code repeatedly. Here is the syntax for a function.
If we ever want to execute the instructions in this function, we would call it by referring to the variable name and then giving it arguments using parentheses. For example, executing myFunction(3, 4) should return the value 12. Note that the return statement at the end of the function is what determines what the function hands back to you (we would refer to you as the caller because you called the function). Functions don't have to return anything. When they don't, they are sometimes called void functions.
Okay, now it's your turn, we'll declare a function that increases the value of roomWidth by 1.
Another type of variable is an object. In the same way that functions group instructions together, objects group information together. For example, you might want a single variable that indicates the first name, last name, and age of a user. It would be unwieldy to define three variables for each user and then keep track of them all. Objects helps us avoid this.
After defining an object, we can refer to its properties using dot notation with the keys. For the above example, we would expect myVariable.myOtherProperty to return 42.
Okay, now it's your turn, please do the following.
So far, we have worked with HTML, CSS, and JavaScript separately. Let's implement the last part of this tutorial, where a user can click on a room and have "the lights go out." We can do this using event handlers. Event handlers are JavaScript functions that are called when an event. Examples of events include (but are not limited to):
There are several ways to attach an event handler to an element. But one of the easiest is using on<event> attributes.
Please do the following to create your first event handler.