This activity introduces some predefined HTML tags for structuring pages and looks at using 'position' to force your page into a particular layout.
<body>
of your 'layout.html' file.<header>Header</header> <nav>Navigation</nav> <main>Main</main> <footer>Footer</footer>
header { background-color:indianred; border:darkred solid 2px; } nav { background-color: burlywood; border:yellow solid 2px; } main { background-color: lightgreen; border: darkgreen solid 2px; } footer { background-color: lightskyblue; border: darkblue solid 2px; }
<header>
is usually for the banner at the top of the screen showing the site logo and name.<nav>
is usually used for site navigation below the banner or to the side of the main content.<main>
is where you'd add your page content.<footer>
is for anything you'd like at the bottom of the page, and often includes contact links, information about the organisation and even the page designer and copyright information.position: relative; top: 50px;
<nav>
element is now lower down than it previously was? It's top is now positioned relative to the previous element. In fact, the top is 50 pixels below the bottom of the previous element.<nav>
element is covering the <footer>
element. That's because all elements other than <nav>
are positioned 'statically' by default. They stay where they were put before CSS was applied. If you choose to change positioning, it's usually better to use the same for all elements.<main>
element.position: absolute;
<main>
element has not changed position, but it has now been taken out of the flow of the document as far as the other elements are concerned.main { background-color: lightgreen; border: darkgreen solid 2px; position: absolute; top: 100px; bottom: 80px; left: 16%; right: 0px; }
<main>
section is positioned exactly as we've prescribed - 100 pixels from the top, 80 pixels from the bottom, 16% of the screen width from the left and right at the right-hand border.header { background-color:indianred; border:darkred solid 2px; height: 100px; position: absolute; left: 16%; right: 0px; } nav { background-color: burlywood; border:yellow solid 2px; position: absolute; top: 0px; bottom: 0px; width: 15%; } footer { background-color: lightskyblue; border: darkblue solid 2px; position: absolute; bottom: 0px; height: 80px; left: 16%; right: 0px; }
<main>
section.<main> <div class="content">Section 1</div> <div class="content">Section 2</div> <div class="content">Section 3</div> </main>
.content { background-color:coral; border:orangered solid 2px; height: 400px; }
<nav>
moves up the screen as we scroll the page to see the bottom of <main>
<nav>
in the same position by fixing it.nav { background-color: burlywood; border:yellow solid 2px; position: fixed; top: 0px; bottom: 0px; width: 15%; }
<nav>
stays in place while the <footer>
moves as you scroll.<footer>
is a problem.<main>
so that our layout remains usable.main { background-color: lightgreen; border: darkgreen solid 2px; position: absolute; top: 100px; left: 16%; right: 0px; bottom: 80px; }
<main>
and goes beyond <footer>
. If that doesn't happen because your screen is too big, increase the value of height
in CSS.<main>
.overflow: scroll;