Web Development
Lesson 2: Layouts
1. HTML5 - What's New
HTML, the language that drives the web, has evolved over the years. The current version of HTML is called HTML5, which gives us some exciting new elements and features.
New Semantic Elements:
HTML5 introduced new elements that make document structure easier to understand:
<header>
<main>
<article>
<section>
<footer>
New Attributes For <form>
Elements:
number
date
time
range
New Graphic Elements:
<canvas>
<svg>
New Multimedia Elements:
<audio>
<video>
HTML5 also offers:
HTML Geolocation to produce maps.
HTML Drag and Drop to drag and drop elements.
HTML Local Storage to store small amounts of data on the user's browser.
To learn more about the elements and features HTML5 has to offer, check out the HTML5 CheatSheet
2. Add A Header And Top Navigation
Now that we know about some of the new elements included in HTML5, let's add a
<header>
tag to our website (not to be confused with the <head>
tag) with a <nav>
element inside of it. Below is our updated
index.html
file.
(Be sure to update your <base>
tag on line 12 to the name of your root folder).
ACTION STEP 1: Update your index.html
file with the code below:
In the file above, we added several elements. Let's style these elements by adding to our main.css
file. Here is the updated main.css
file:
ACTION STEP 2: Update your main.css
file with the code below:
Now, our website has a space for a logo in the upper-left, and a navigation list in the upper-right:
Use Chrome
DevTools to examine the code structure and the new CSS. Click Here to see our
new index.html
page in a browser.
3. Layout With CSS
In the code above, we laid out our page structure with CSS. Many of the HTML elements used to
structure web pages, like <div>
, <header>
,
<footer>
, <section>
, <main>
, <article>
,
and <nav>
have a default CSS display:
value of block
. CSS displays block-level elements one on top of
another and stretches them out as much as it can depending upon the size of their parent element, like
this:
DISPLAY: BLOCK;
<div>
div {
display:block;
}
</div>
<div>
div {
display:block;
}
</div>
In our new layout, we used the display:
value of
inline-block
to align elements next to each other,
instead of on top of each other.
DISPLAY: INLINE-BLOCK;
<div>
div {
display:inline-block;
}
</div>
<div>
div {
display:inline-block;
}
</div>
We also used the float:
property to push our
navigation to the right of its parent. The float:
property, however, removes elements from the flow of the page relative to block elements. This is why
we see the parent element of the floated elements collapse below:
FLOAT: LEFT; & FLOAT: RIGHT;
<div>
div {
display:inline-block;
float:left;
}
</div>
<div>
div {
display:inline-block;
float:right;
}
</div>
The float:
property was
originally designed to allow the wrapping of text around a block element (like an image). Notice what
happens below when we include a block level <p>
element inside the parent element
and float our <div>
to the right. The parent element expands to fit the <p>
content.
<div>
div {
float:right;
}
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet,
nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor.
Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut
aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a
lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim
ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida
venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras
ac leo purus. Mauris quis diam velit.</p>
But, if our parent element only contains floated elements (like our header) we
need to set the overflow:
property on the parent
element to hidden
(or auto
).
OVERFLOW: HIDDEN;
<div>
div {
display:inline-block;
float:left;
}
</div>
.parent-element {
overflow:hidden;
}
<div>
div {
display:inline-block;
float:right;
}
</div>
Now the parent element will expand to contain our floated elements.
The image below illustrates the layout of our current website, and shows how we used CSS to position the various elements.
4. Add Sidebar and Sections
Using what we now know about page layout, let's add a sidebar to our page and break our content up into articles and sections (new lines are highlighted below).
ACTION STEP 3: Update your index.html
file with the code below:
Our CSS file will also change to style the new elements (new declarations also highlighted):
ACTION STEP 4: Update your main.css
file with the code below:
The image below shows what our new website looks like. You can Click Here to see the site in action.
5. Add Logo, Links, And Data
The basic structure of our page is set up, but the page doesn't really serve any purpose yet. Let's change that! We'll be using our page to create web flashcards to help us with our studies. We'll also add links to resources in our sidebar so we have everything we need on one page. Best of all, once the page is complete we'll be able to copy and reuse our code to help us study any subject. We'll also improve the look of our site by adding some color and a logo in the upper-left-hand corner.
First, below is the logo for the updated page. Right Click on it and select Save Image As...
ACTION STEP 5: Save this image in your MyWebsite/img
directory OR create
your own.
If you create your own image, make sure that it measures 300 pixels wide by 75 pixels high.
And, here is the updated index.html
file:
ACTION STEP 6: Update your index.html
file with the code below:
ACTION STEP 7: Update your main.css
file with the code below:
Our page now looks like this:
And you can Click
Here to see this new index.html
page in a browser.
We've added our flashcards, and if we hover over them, we can see the answers. We've also added our logo image and links to resources we may need in the sidebar.
We've also added a lot of new CSS declarations as well. We'll take a deeper look at the CSS beginning with the next lesson.