Code a Backwards Compatible, One Page Portfolio with HTML5 and CSS3

HTML5 is the future of web development but believe it or not you can start using it today. HTML5 is much more considerate to semantics and accessibility as we don’t have to throw meaningless div’s everywhere. It introduces meaningful tags for common elements such as navigations and footers which makes much more sense and are more natural.

This is a run through of the basics of HTML5 and CSS3 while still paying attention to older browsers. Before we start, make note of the answer to this question. Do websites need to look exactly the same in every browser?

View Demo | Download Files (.zip)

The HTML

<!DOCTYPE html>
<html lang="en">

<!-- This is a demonstration of HTML5 goodness with healthy does of CSS3 mixed in -->
<head>

    <title>One Page Portfolio</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

    <!--[if IE]>
    	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!--[if IE 7]>
    	<link rel="stylesheet" href="ie7.css" type="text/css" media="screen" />
    <![endif]-->

    <link rel="stylesheet" href="style.css" type="text/css" media="screen" />

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js" type="text/javascript"></script>
    <script src="js/jquery.anchor.js" type="text/javascript"></script>
    <script src="js/jquery.fancybox-1.2.6.pack.js" type="text/javascript"></script>

</head>

<body>

    <header> <!-- HTML5 header tag -->

    	<div id="headercontainer">

    		<h1><a class="introlink anchorLink" href="#intro">Web Design Portfolio</a></h1>

    		<nav> <!-- HTML5 navigation tag -->
    			<ul>
    				<li><a class="introlink anchorLink" href="#intro">Intro</a></li>
    				<li><a class="portfoliolink anchorLink" href="#portfolio">Portfolio</a></li>
    				<li><a class="aboutlink anchorLink" href="#about">About</a></li>
    				<li><a class="contactlink anchorLink" href="#contact">Contact</a></li>
    			</ul>
    		</nav>

    	</div>

    </header>

    <section id="contentcontainer"> <!-- HTML5 section tag for the content 'section' -->

    	<section id="intro">

    		<h2 class="intro">Hand-coded <strong>HTML</strong> and <strong>CSS</strong> is what I do. <span class="sub">It's what I'm good at so why not?</span></h2>

    		<a class="featured" href="http://inspectelement.com"><img src="images/featured.gif" alt="Inspect Element large preview" /></a>

    		<p>Featured Project: <a href="#">Inspect Element</a></p>

    	</section>

    	<section id="portfolio"> <!-- HTML5 section tag for the portfolio 'section' -->

    		<h2 class="work">My Portfolio</h2>

    		<ul class="work">
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    			<li>
    				<a href="http://inspectelement.com"><img src="images/inspectelementSmall.jpg" alt="Inspect Element preview" /></a>
    			</li>
    		</ul>

    	</section>

    	<section id="about"> <!-- HTML5 section tag for the about 'section' -->

    		<h2 class="about">About Me</h2>

    		<p>Now this is a story all about how my life got twisted upside down and I'd like to take a minute just sit right there I'll tell you how I became the prince of a town called Bel-Air. In West Philadelphia born and raised on the playground my momma said most of my days chilling out, maxing and relaxing all cool and all shooting some b-ball outside of school when a couple of guys they were up to no good started making trouble in our neighbourhood I got in one little fight and my mom got scared, she said your moving in with your auntie and uncle in Bel-Air</p>

    	</section>

    	<section id="contact"> <!-- HTML5 section tag for the contact 'section' -->

    		<h2 class="contact">Contact Me</h2>

    		<p>I whistled for a cab and when it came near the license plate said fresh and had dice in the mirror, if anything I could say that this cab was rare but I thought nah, <a href="">forget it</a>, yo home to Bel-Air! I pulled up to the house about seven or eight I yelled to the cabbie yo home, smell you later, looked at my kingdom I was finally there to sit on my throne as the prince of Bel-Air</p>

    		<form id="contactform"> 

    			<p><label for="name">Name</label></p>
    			<input type="text" id=name name=name placeholder="First and last name" required tabindex="1" /> 

    			<p><label for="email">Email</label></p>
    			<input type="text" id=email name=email placeholder="example@domain.com" required tabindex="2" /> 

    			<p><label for="comment">Your Message</label></p>
    			<textarea name="comment" id="comment" tabindex="4"></textarea> 

    			<input name="submit" type="submit" id="submit" tabindex="5" value="Send Message" /> 

    		</form> 

    	</section>

    	<footer> <!-- HTML5 footer tag -->

    		<ul>
    			<li><img src="images/twitter.png" alt="" /><a href="http://twitter.com/tkenny">Follow me on Twitter</a></li>
    			<li><a href="http://inspectelement.com/articles/code-a-backwards-compatible-one-page-portfolio-with-html5-and-css3">Back to the Tutorial on Inspect Element</a></li>
    		</ul>

    	</footer>	

    </section>

</body>

</html>

Download the HTML here (Right-click and save as).

First thing first, let’s make sure the code validates with W3C’s experimental HTML5 validator.

Good news, it does! This is a simple example but good to know we’re on the right lines.

The HTML5 Goodness

As you can see from the code above there are new tags that you may not be familiar with. To make it even simpler here is the code stripped down to only the HTML5 tags.

<header> <!-- HTML5 header tag -->

        <nav> <!-- HTML5 navigation tag -->
	</nav>

</header> 

<section id="contentcontainer"> <!-- HTML5 section tag for the content 'section' -->

	<section id="intro"> <!-- HTML5 section tag for the introduction 'section' -->
	</section>

	<section id="portfolio"> <!-- HTML5 section tag for the portfolio 'section' -->
	</section>

	<section id="about"> <!-- HTML5 section tag for the about 'section' -->
	</section>

	<section id="contact"> <!-- HTML5 section tag for the contact 'section' -->
	</section>

	<footer> <!-- HTML5 footer tag -->
	</footer>	

</section>

Header

The first one you’ll notice is <header> and it does exactly what it implies. You can use this for the header of your page, typically containing the logo and the navigation.

The header element represents a group of introductory or navigational aids.

A header element is intended to usually contain the section’s heading (an h1–h6 element or an hgroup element), but this is not required. The header element can also be used to wrap a section’s table of contents, a search form, or any relevant logos.

The header element as described in the HTML5 specs.

Nav

The <nav> tag now gives us the ability to highlight the navigation of a site through the HTML. Usually contained within the header of a page but can also be applied to left or right sided navigation in sidebars.

The nav element represents a section of a page that links to other pages or to parts within the page: a section with navigation links.

Not all groups of links on a page need to be in a nav element — only sections that consist of major navigation blocks are appropriate for the nav element. In particular, it is common for footers to have a short list of links to various pages of a site, such as the terms of service, the home page, and a copyright page. The footer element alone is sufficient for such cases, without a nav element.

User agents (such as screen readers) that are targeted at users who can benefit from navigation information being omitted in the initial rendering, or who can benefit from navigation information being immediately available, can use this element as a way to determine what content on the page to initially skip and/or provide on request.

The nav element as described in the HTML5 specs

Section

In this example the <section> tag is being used to separate the different parts of the page. The introduction, my portfolio, about me and contact me areas are all sections that make up the page, all contained within a section tag.

The section element represents a generic document or application section. A section, in this context, is a thematic grouping of content, typically with a heading.

Examples of sections would be chapters, the various tabbed pages in a tabbed dialog box, or the numbered sections of a thesis. A Web site’s home page could be split into sections for an introduction, news items, contact information.

The section element as described in the HTML5 specs.

Footer

You’ll find the <footer> tag at the end of the contact section of the portfolio example. It’s included there and not at the end of the document in this case because it contains content relevant to the contact section in the form of the Twitter link (ignoring the back to tutorial link). For more information read the following included in the HTML 5 spec overview:

The footer element represents a footer for its nearest ancestor sectioning content or sectioning root element. A footer typically contains information about its section such as who wrote it, links to related documents, copyright data, and the like.

Contact information for the author or editor of a section belongs in an address element, possibly itself inside a footer.

Footers don’t necessarily have to appear at the end of a section, though they usually do.

When the footer element contains entire sections, they represent appendices, indexes, long colophons, verbose license agreements, and other such content.

The footer element is not sectioning content; it doesn’t introduce a new section.

When the nearest ancestor sectioning content or sectioning root element is the body element, then it applies to the whole page.

The footer element as described in the HTML5 specs.

The Form

While the form itself doesn’t seem to be radically different to previous methods, HTML5 does introduce a nice addition in the placeholder attribute. Basically this fills in a text field with sample text much like we’re used to doing with Javascript now.

<input placeholder="example@domain.com" />

For more on HTML5 in forms make sure you check out Have a Field Day with HTML5 Forms on last year’s 24 ways.

Backwards Compatibility

All of this is great. We’re using the latest and greatest technology in web development but currently only a few browsers support HTML5 in any capacity. We now need to think about all versions of Internet Explorer which don’t include any support for HTML5 whatsoever. Fortunately for us, Remy Sharp has created a Javascript file that reverses IE’s inability to style elements it doesn’t recognise.

Just included the following code:

<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

Now that all major browsers will recognise the styling let’s move onto the CSS.

The CSS3 Goodness

Most of the CSS won’t be new to you but there are a few CSS3 properties that we’ll have a look at including gradients, embeddable fonts and text shadow.

View the CSS here

Let’s begin by looking at how this page behaves visually in a browser that supports CSS3. View the demo in the latest versions of Safari, Chrome or Firefox to see the full effect. Or see the screenshots below:

@font-face

While this example uses @font-face on every instance of text, I wouldn’t recommend doing so on large websites. Also, using some fonts as body text may case text to become hard to read. Don’t get carried away. This theme uses Yanone Kaffeesatz at a large enough size to make it very readable and differ enough from the regular web-safe fonts.

@font-face { font-family: Keffeesatz; src: url(YanoneKaffeesatz-Light.otf) format("opentype") }
@font-face { font-family: KeffeesatzBold; src: url(YanoneKaffeesatz-Bold.otf) format("opentype") }

RGBa

With RGBa you can declare a colour and an opacity as a single property. Not only that but it can be applied to anything that uses colour. To demonstrate this, the image link and input borders have RGBa applied. You can see the subtle background texture show through, especially noticeable on the input elements in the form.

input[type="text"] { border: 5px solid rgba(122, 192, 0, 0.15); }

The first three values (122, 192, 0) are the red, green and blue values of the colour with the fourth (0.15) being the alpha value, or opacity.

Where it can be seen in the example:

  • Logo (subtle border top and bottom)
  • Image link borders
  • Input borders

Gradients

WebKit started the support of CSS gradients and Mozilla have followed suit and implement a slightly different way of generating them but support is there. Make sure to set a standard background colour for browsers that don’t support RGBa. The example below shows a linear gradient going from bottom-to-top.

h1 a {
background: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(0.23, #c34000),
    color-stop(0.62, #ff5400)
);
background: -moz-linear-gradient(
    center bottom,
    #c34000 23%,
    #ff5400 62%
);
}

Where it can be seen in the example:

  • Logo
  • Button

Text-Shadow

A great addition to CSS3 used in the one page portfolio to add a touch of depth to the text on the page lifting it slightly from the patterned background.

body { text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2); }

The first value is the horizontal distance, the second the vertical and the third how much blur is applied. The fourth value here is the previously mentioned RGBa. Both WebKit and firefox recognise the single declaration above which is nice.

Where it can be seen in the example:

  • Everywhere (expect input text and)

Box-Shadow

Be aware that the box-shadow CSS3 property has been dropped from the CSS3 specs for now but it still works fine in WebKit and the latest versions of Firefox.

header {
-webkit-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.4);
}

Where it can be seen in the example:

  • Logo
  • Header
  • Hovering over image links
  • Hovering over input fields
  • Form button

Border-Radius

Probably the most used CSS3 property in the wild right now and very simply implemented as the code shows below:

h1 a {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}

Where it can be seen in the example:

  • Logo
  • Button
  • Borders that use RGBa in WebKit

Selection Colour

Briefly covered on Inspect Element before, CSS3 gives us the ability to change the colour or background colour, or both, of selected text with the following code:

::selection { background-color: rgba(122, 192, 0, 0.2); }
::-moz-selection { background-color: rgba(122, 192, 0, 0.2); }

So then, Internet Explorer…

Let’s have a look at how this looks in our good old friend, Internet Explorer:

As you can see it doesn’t look quite as good as intended but it still looks really good. Don’t forget that the average visitor looking at sites in Internet Explorer typically don’t open it up in different browsers and compare the two so they’re none the wiser. It’s still perfectly readable and usable.

The possible exception is the logo, where you may want to save it as an image so it displays 100% as intended in all browsers.

jQuery

This extremely simple method of smooth scrolling using jQuery is used and makes navigation much more interesting.

Can I have the .psd File please?

You may be wondering where the Photoshop or Fireworks file is. Well, there isn’t one. This is a result of designing in the browser and shows what can be achieved when skipping the Photoshop design phase, saving a lot of time in the process.

View Demo | Download Files (.zip)

Author Tom Kenny

I'm the creater of Inspect Element and currently work as a web designer for TUI Travel. You can view my portfolio and follow me on Twitter.

Discussion

  1. Mil says:

    Very thorough, and well-written.

    Keep it up.

  2. Mihai O. says:

    Great article, but the download link doesn’t work ;)

  3. Hi Tom, well done and very informative. As to your first question (do website need to look the same in every browser), my personal opinion (and answer) is “no, they don’t”, in fact I enjoy seeing how different websites can look in different browsers and how new techniques can change the little things that make the difference in the end. But I’m a web designer! I have fun discovering what’s new and experimenting a little bit with CSS and XHTML, jQuery and Ajax. In my experience, clients see it just the opposite…to make it very simple -moz-border-radius will generate the question “why my site has squared borders in IE?”

  4. Matthew says:

    Saying that sites don’t need to look the same in every browser is not strictly true, it depends on the site.

    Personally…
    - Sites do not need to be pixel perfect in every browser
    - Some extra features of new browsers are god and degrade nicely.

    However…
    If you do an AB test to see if having a box shadow round something improves your sales, then the box shadow must look the same in all browsers, otherwise the test is pointless.

    • Rio says:

      If you point it to IE, the best solution is to use conditional statement for that crappy browser :] that way you can still make it look the same in this browser, while you use the modern approach.

      Don’t let IE and other old browsers that don’t support web standards make you stuck and let you hate HTML5 and CSS3. There are so much fun in them :]

  5. Oliv G. says:

    Yeah great article. if I had had this article for my site earlier, it would have been very helpful:)

  6. Elena says:

    Thank you for this. It’s such a thorough tutorial. This is a great intro to these new languages.

  7. Rett says:

    Thanks for the great post Tom. I was nodding in agreement up until the last part about not having a psd. I won’t argue about the end product because it does look great, but I think no matter the size of a project, it’s imperative to have a “design phase.”

    Before even getting on the computer and firing up Photoshop or Illustrator I’m a big fan of pencil on paper as that’s the quickest way to churn out a bunch of ideas and force yourself to explore new ways of doing things. And then once on the computer I really think taking the time in Photoshop is the best way to make sure that you don’t take any shortcuts. My experience with jumping straight to the browser is that you tend to “settle” and think, eh, it’s good enough. Whereas if you have a psd you will make sure to match it pixel for pixel.

    Anyway, just wanted to throw that out there for any of the younger designers out there who are tempted to skip the design phase, and say that my opinion is DON’T EVER :)

  8. Erik Ford says:

    I think it is important for all of us to begin to familiarize ourselves with HTML5 tags to get a “leg up”. But, I question whether or not this should be implemented for a client globally on a website before the specs have been approved and incorporated into the future releases of modern browsers?

    Otherwise, the promise of HTML5 has me extremely excited for the bright future ahead of us.

  9. Amanda Kay says:

    “You may be wondering where the Photoshop or Fireworks file is. Well, there isn’t one.” – Great point! I’m always glad to know of others who code in this way. Also the use of CSS-based effects ensures the site is not weighed down by large image files and calls to the server.

    Well written and thought-provoking – thanks for publishing this!

  10. Faruk Ate? says:

    Hi Tom,

    Have you seen Modernizr? Might be useful for you :-)

    http://www.modernizr.com/

    -Faruk

  11. That’s a well written introduction to HTML5… really appreciate it! Thanks.

    Would you mind using your files as a template for my personal page (giving credit of course)? It’s well structured and would serve perfectly my needs.

  12. Jann says:

    I’m retired now, but when I was designing for our company [large IT corporation] it was REQUIRED that our designs looked the same in the specified, popular browsers. In other words the users experience HAD to be the same no matter which browser they were using.

    This was a good article…a nice way to introduce HTML5.

  13. Michael says:

    Huh… interesting… looks very much like my site. I also posted a blog about HTML5 &CSS3 Backwards Compatibility have you seen it?

  14. Fabio says:

    Very informative and useful tutorial, really a great way to plunge into html5.
    I was wondering, however, whether it’s possible to highlight the active section on the navbar, I can see only 2 states – link and hover, but there’s no way to have a different color for the link of the active section. Maybe with jquery can we append dynamically a class and style it differently?
    Thanks if you can answer this question, but thanks even if you can’t, anyway a great tutorial.
    cheers!

    • michael says:

      I don’t know why you would want to do such a thing. When would you be selecting text and clicking on a menu button simultaneously? I don’t think that’s possible. These are the current pseudo-classes available to use. The [ ::selection ] pseudo-classes is intended only to change the color of selected text. Try selecting the text on this page and you’ll see what I mean.

      a:link /* unvisited link */
      a:visited /* visited link */
      a:hover /* mouse over link */
      a:active /* selected link */

      • Fabio says:

        maybe I wasn’t able to explain what I mean: say you click on “portfolio”, you go to the portfolio section, but no item on the navbar is highlighted or styled in a different way.
        In a everyday use of a site, when you go to a new page you’d like to have the part of the navbar that “brought” you on that page styled in a different way.
        Usually you get this effect with a css sprite with 3 states: one for the link status, one for the hover and the third for the active page you are on (I don’t mean the a:active pseudo class, but a class you give to the body element or to a single item on your navbar, call it active or current, or whatever). I wanted to get this effect but I can’t, since there is only one page here and I can’t target an element on the page to make it the current section.
        So my question was if there’s a way to get a new class in a dynamic way, that’s all..and by the way, thank you, but I know a little bit of CSS and I was aware of the existence of pseudo classes..;)

        • Tom Kenny says:

          I see what you mean and it’s possible you could do something with JavaScript on the example portfolio here but I fail to see why would want to. The problem is that you’re only on a section, not a page. As soon as you scroll away from the section you’re on, you would need to work out where the visitor is.

          One way around this is to redesign with the navigation repeated at the top of each section rather than at the top of the page itself. This way you can highlight the section that the navigation relates to.

  15. Shawn K. says:

    Thanks for separating the HTML5, that’s what actually caught my attention to read the article.

    I guess I’ve been avoiding HTML5 due to everyone trying to use it as an alternative to Flash…when many browsers still wont display HTML5 to it’s full extent.
    One of the many reasons to hate Internet Explorer, it tends to hold back web standards from evolving.

  16. Sandy says:

    I really think there’s a place for browser-based design. When everyone seems to be throwing images around the place, a good layout and some smart coding can easily produce a more fresh looking website. I haven’t yet used HTML5 or CSS3, but it’s great to have a breakdown such as this.

  17. fosron says:

    Cool tutorial, and a really awesome result, but there’s one problem with your layout, and to be exact, with use of tag. You can’t use it like this , this one should be a div. I’m saying should because the HTML5 draft says that section is for CONTENT not for STYLING, and to follow the draft or not, is your decision :) I really love the CSS3 goodies, and damn, i hate IE even more now ^^

  18. fosron says:

    i was talking about {section} tag , and {section id=”contentcontainer”} , forgot that WP strips tags out ^^

  19. mike says:

    I came here to say what fosron said.

    SECTION is a semantic tag for defining sections of text and content, yet you are using a SECTION tag (section id=”contentcontainer” to be exact) in the same way we used to use DIV tags. This is incorrect. The correct method would be to replace that one section tag with a DIV, or replace it with an ARTICLE tag… however, again the article tag is really only for grouping several articles on one page (think a blog for example)… my advice, replace the SECTION tag with a DIV to stick to the HTML5 spec.

    Otherwise a nice writeup.

  20. erikdana says:

    Hey,
    by some reason the intro image was pushed to the right, it’s in the same line as the subtitle “It’s what I’m good at so why not?” … Looks like there should be a line break there. Weird.

    I’m using firefox 3.6 on linux btw

  21. Dan says:

    Nice introduction to HTML5 and CSS3. I am of the opinion that a site should look identical in all browsers, but maybe that’s just because I’ve done my time making sure everything works in IE6, and it’s so much easier to ensure they do look the same these days.

    It’s interesting to note that some of the CSS3 effects like drop-shadows etc were present in IE way back. They weren’t great, but MS were actually ont he right track including those sorts of ideas. Hopefully in the next 3-5 years we’ll actually be able to use them via CSS3.

    Nice job @fosron and @mike for clearing up a few issues.

Become Part of the Discussion

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.