Lesson Two: Keeping it Neat

Has your bedroom ever become so messy that you have trouble finding a lost shoe or your favorite book? Have you ever had to track down the source of a nasty smell coming from your refrigerator? When things get messy and sloppy, it is often hard to find the items we need. It can be particularly frustrating if you have to find the source of a problem – like the cause of that stinky smell!

Web design works the same way. If you jam all your mark-up together and do not follow HTML best practices, you will also end up with a mess. Your website may work and may even look good in the browser, but if something goes wrong it will take a long time to find and fix the problem. It is much better to take a little more time writing clean code. This will make it easier to check for missing closing tags or other mistakes.

Clean Code
Well-planned web mark-up, which only uses the tags necessary to make the page work, is called clean code. Everyone makes mistakes, even seasoned web designers! If your web site contains clean code, it becomes easier and faster to find and fix problems.

If you already have messy code, often the only way to find mistakes is to clean up your mark-up first and systematically put your page back into order. To avoid this process, try to write clean code the first time!

Nesting
Containers in a refrigeratorOne common cause of weird behavior on a web page is misplaced closing tags. If you look at a page in the browser, you will see it is usually separated into sections. You may have a main paragraph, a section with information down the side, or even a section that has a different color than the rest of the page. These sections are all identified with different sets of HTML elements in the code.

In order to work properly, these elements can nest inside one another, like different containers in a refrigerator. You always want to ensure every container in your refrigerator is closed, otherwise the food will go bad. Similarly, each one of your HTML elements should have matching opening and closing tags. You also know that you can't physically close a larger, outer food container until you close all smaller, inner containers first. The same is true of HTML elements!

Take a look at the following well-formed code:

<div> <p> CORRECT </p> </div>
Copy
The elements that are opened first must be closed last. In the code above, we started with an opening <div> tag, then an opening <p> tag. We must then close our tags in the opposite order: the closing </p> first and the </div> last. You must follow this pattern when you are creating elements in your web pages.

The following example contains code that may cause a problem on a web page:

<div> <p> WRONG </div> </p>
Copy
In this code, we opened the <p> element inside of <div>, but closed it after the </div> closing tag. This is not allowed and will confuse your web browser! Because the <div> element was opened first, it MUST be closed last, after all the internal elements are closed.

Carriage Returns and Indenting
Take a look at the following code, which has three <div> elements nested within each other:

<div> <div> <div> </div> </div> </div>
Copy
It's pretty hard to figure out which opening and closing tags go together. If you were missing an opening or closing tag, it might not be obvious to a human reader. How can we make this clearer?

One of the simplest ways to keep your mark-up readable is to use "whitespace" like indenting spaces, tabs, and carriage returns. Adding space between your sections and lining up starting and ending tags will help you easily see which things go together and which elements are missing tags. Extra whitespace does not change how the page looks in the browser, but makes the mark-up much easier to read.

Let's start by adding a carriage return (pressing ENTER) after each opening and closing block element tag:

<div>
<div>
<div>
</div>
</div>
</div>
Copy
That helps a bit, but there is more we can do! Next we will indent nested tags so it's clear which elements contain other elements. "Indenting" means you are adding whitespace like tabs or spaces at the beginning of your line to move the beginning characters to the right.

To add white spaces, place your cursor at the beginning of the line and press the TAB key or hit the SPACE BAR several times. When you indent your opening tag a certain amount, always indent your closing tag by the same amount. That way your opening and closing tags always start in the same column, and you can quickly look up and down in your file to find the matching tags.

Here we have added spaces to indent each pair of <div> tags that are nested inside another element.

<div>
<div>
<div>
</div>
</div>
</div>
Copy
Now we're in business! It's perfectly clear that we have three pairs of nested block elements, and each opening tag has a closing tag somewhere below it in the same column.

Our first example was simple, but consider this more complex code. It's valid, just very hard to understand.

<body><div><div><p></p></div><h1></h1><h2></h2><div><p></p></div></div></body>
Copy
Let's apply our clean code rules to indent nested elements and add new lines after most tags.

<body>
<div>
<div>
<p>
</p>
</div>
<h1></h1>
<h2></h2>
<div>
<p>
</p>
</div>
</div>
</body>
Copy
Now it is easy to figure out which block element is nested inside another one and ensure that all opening tags have a matching closing tag. Notice we left our entire headline elements <h1> and <h2> on the same line. Because headlines are usually just a few words long, they can fit nicely on a single line. Don't forget you can also add blank lines between sections of code to further set them apart, and it won't change the way the browser shows the page.

The "id" Attribute
Division (<div>) elements are like plain brown boxes. They all look the same and they can be placed inside each other. Once you have a bunch of these boxes together, it can be hard to tell what each box is supposed to contain. Fortunately, you can give each box a name with the id attribute.

The id attribute (pronounced "I-D") does not do anything other than put a label on the container. It works like wearing a name tag. You can put an id attribute on most elements to give them a unique name within your HTML file. These id attributes can only be used on one container at a time on a page. If you label one box "cow" you cannot label anything else on that page "cow".

<div id="cow"></div>
Copy
Your element IDs should be reasonably descriptive, short and easy to spell. Do not use id values that are the same except for differences in case such as "cow", "Cow", and "COW".

If you plan to build more than one web site, use descriptive names that don't refer to the website itself. That way you can copy your existing site and simply change the content without having to change the id names. For example, use "Banner" instead of "RaptorsBanner" or "Sidebar" instead of "RaptorsSidebar".

We've added a few id attribute to our example below. Now, at a glance, you have a pretty good idea what each <div> section is supposed to contain.

<body>
<div id="Main">
<div id="Introduction">
<p>
</p>
</div>
<h1></h1>
<h2></h2>
<div id="Subject">
<p>
</p>
</div>
</div>
</body>
Copy
Adding Comments with the <!-- --> Element
Wouldn't it be great if you could write notes to yourself in your code that will not be displayed in the browser? With the comment tags you can do just that! Comments are completely optional but are a great help to understanding your mark-up. Comments start with the unusual "<!--" tag and end with "-->" characters. You can type pretty much anything inside a comment except double dashes (--).

Comments can be used to write descriptive text to help understand a section of code, or perhaps to help identify the ending tags that go with a particular attribute. The id attribute can be put on the opening tag, but there is nothing to identify the matching closing tag except your whitespace layout or a comment. Adding a comment near the ending <div> tag makes it very easy to identify which ending tag goes with which starting tag.

<!-- This section will contain the banner for the web page,
which includes a picture and a page title -->
<div id="banner">

</div><!-- end of banner-->
Copy
Above, we have demonstrated two comments. One just describes in general what the section contains, while the other carefully marks the ending </div> tag for the "banner" id. None of the text in between the comment tags will show up in the web browser to the reader. But the comments make it obvious to any web designer looking at the code what this particular <div> section contains.

Why is it important to have clean, neat HTML code?
How can nesting errors cause problems on a web page? Use the refrigerator container example to illustrate container concepts.
How can whitespace help to identify nesting errors on a web page?
What is the id attribute used for?
How would you add a comment to your HTML code? What information should be placed within a comment?
What is a page footer? What information should be placed within a page footer?
What is a character reference? Why do you need to use them for special characters on a web page?

1. It is important to have clean, neat HTML code because it makes it easier and faster to find and fix problems. Messy code can result in a lot of time wasted searching for errors or troubleshooting issues.

2. Nesting errors can cause problems on a web page because they can lead to incorrect rendering or functionality. Just like in the refrigerator container example, if a closing tag is misplaced or missing, it can affect the entire structure of the page or element. This can result in broken layouts, improper functionality, or even error messages.

3. Whitespace, such as indenting and adding carriage returns, can help identify nesting errors on a web page because it makes the code more readable and organized. By visually separating different sections and aligning opening and closing tags, it becomes easier to see which elements are nested within each other and identify any missing or misplaced tags.

4. The id attribute is used to give an element a unique identifier within an HTML file. It helps to label and identify specific containers or elements. This can be useful for styling, scripting, or targeting specific elements with CSS or JavaScript.

5. To add a comment to HTML code, you can use the <!-- --> element. Anything written between these comment tags will not be displayed in the web browser. Comments can be used to provide descriptive text, make notes, or help identify certain sections of code. It is important to include relevant information in comments that can be helpful for understanding or maintaining the code in the future.

6. A page footer is a section that appears at the bottom of a web page and typically contains information that is consistent across multiple pages, such as copyright notices, contact information, or navigation links. The specific information placed within a page footer depends on the purpose and content of the website.

7. A character reference is a way to represent special characters in HTML code. Some characters have special meanings in HTML, such as the less than symbol (<) or ampersand (&). To display these characters as regular text, you need to use their corresponding character references. For example, &lt; represents the less than symbol (<) and &amp; represents the ampersand (&). Using character references ensures that the special characters are correctly displayed and do not interfere with the HTML syntax.

which number would replace X to make these fractions equivalent? 5/12 = X/36 (1 point)

To determine the value of X that would make the fractions 5/12 and X/36 equivalent, we can set up a proportion:

5/12 = X/36

To find the value of X, we can cross-multiply and solve for X:

(5 * 36) = (12 * X)

180 = 12X

Dividing both sides of the equation by 12:

180/12 = X

15 = X

Therefore, X would be 15 to make the fractions equivalent.

which number would replace X to make these fractions equivalent? 6/x = 9/12

To determine the value of X that would make the fractions 6/X and 9/12 equivalent, we can set up a proportion:

6/X = 9/12

To find the value of X, we can cross-multiply and solve for X:

9 * X = 6 * 12

9X = 72

Dividing both sides of the equation by 9:

X = 72/9

X = 8

Therefore, X would be 8 to make the fractions equivalent.

The footer is the part of the page that's at the bottom and usually has info that's the same on all the pages. It could be a copyright notice, your contact info, or a link to a navigation page. It all depends on what you're trying to do and what your website is about.

Make this sentence into 1st grader

The bottom part of the page is called the footer. It has information that stays the same on all the pages. It can be things like a copyright notice, your contact information, or a link to another page. It depends on what you are doing and what your website is about.

order the following fractions in ascending order 1/2 5/8 1/4 3/5 4/19

To order the fractions in ascending order, we can compare them and arrange them from smallest to largest:

1/4, 1/2, 5/8, 3/5, 4/19

Therefore, the ordered fractions in ascending order are 1/4, 1/2, 5/8, 3/5, 4/19.