How to: CSS Large Background



Since I posted the huge collection of Large Background Websites, I received several email requests on how to make a large background site with CSS. So, I thought it would be a good idea to share my techniques on designing large background websites. In this tutorial, I will provide various CSS examples on how you can create a large background site using either a single or double images.


Common Mistake: Background Gets Cropped (see demo)


Take a look at the demo file, it looks fine under 1280px. But if you have a high resolution display, you will see the background gets cut off on the sides.


background cuts off


Example #1: Single Image (see demo)


A quick solution to fix the problem mentioned earlier: make the edge of the image the same color as the BODY background color. Here, I’m going to use Web Designer Wall as an example. Take a look at the image below and notice the edge is a solid color?


WDW background image


CSS Part


The CSS part is very simple. Specify the background image (position it center, top) for the BODY element.


CSS overview


Here is the CSS code:


body {
padding: 0;
margin: 0;
background: #f8f7e5 url(wdw-bg.jpg) no-repeat center top;

width: 100%;
display: table;
}


Notice there are two extra lines in the BODY selector. That is to prevent the background image from shifting when you resize the browser smaller than the content width (it happens in Firefox).


Firefox bug


Example #2: Double Images (see demo)


For this example, I’m going to use the job board design, Design Jobs on the Wall. I have a cork board pattern repeating in the BODY tag and a wrapper background in the center.


cork board


The trick here is to export the GIF matte with a brown color that is similar to the cork board pattern.


cork board overlay background


Example #3: Sky Background (see demo)


In this example, I have a 1px gradient that is repeated horizontally for the BODY tag. Then I attached a cloud background in the center of the #wrapper tag.


sky background


Update: Sky Background Using HTML Selector (see demo)


Thanks to the comments from the readers. Below is an example of the sky background using HTML selector to display the gradient background, so the #wrapper DIV tag is not required. It is a much more cleaner approach.


sky background 2


Download the demo zip now and don’t forget to check out the Large Background Websites.


Credits: thanks to Alex from markup4u.com for the {width: 100%; display: table} CSS trick.




Building Custom WordPress Theme



This is the Chapter II of the Complete WordPress Theme Guide series. This chapter will show you how to build a custom WordPress theme. Although the Codex site provides very good documentations on how to create a theme, but I find it too complicated for a beginner. In this tutorial, I will explain the basics of how WordPress theme works and show you how to convert a static HTML template into a theme. No PHP skill is required, but you need Photoshop and CSS skills to create your own design.


1. The Blog Frontend


Before you start, let’s take a look at the WordPress default theme and see how it is structured. Take note of the elements (header, post title, search form, navigation, footer, etc.).


default homepage Default Frontpage (index.php)


default homepage Default Single (single.php)


2. Photoshop Mockups


Based on the information gathered from the default theme, design a Photoshop mockup of your blog. Here I’m using GlossyBlue, one of my free WordPress themes, as an example. Download the demo.zip to see the Photoshop file.


default homepage


3. HTML + CSS


After the PSD design is done, create a static HTML+CSS template of each page. You can use my GlossyBlue HTML files in the demo.zip to follow this tutorial. Extract the zip and take a look at the index.html, single.html, and page.html. Later in the tutorial, I will use these HTML files and convert them into a theme.


default homepage


Why Create a Static HTML File First?


Mainly because it will make the development process a lot easier. I usually create a HTML file for every template that I need, test it across all browsers, validate both HTML and CSS markups, then all I have to do is cut & paste the WordPress code. By doing so, I don’t have to worry about HTML or CSS bugs during my theme making process.


4. How WordPress Theme Works


If you go the default theme folder (wp-content/themes/default), you should see many PHP files (called template file) and one style.css file. When you are viewing the front page, WordPress actually uses several template files to generate the page (index.php << header.php, sidebar.php, and footer.php).


how theme works


For more details, check out Site Architecture and Template Hierarchy at Codex.


5. Duplicate The Template Files


Copy the GlossyBlue HTML folder into the wp-content/themes folder. Then, go to the default theme folder, copy the comments.php and searchform.php file to the glossyblue folder.


copy files


6. Style.css


Go to the WordPress default theme folder, open the style.css file. Copy the commented code at the top and paste it to the GlossyBlue style.css file. Change the theme name and the author information as you desire.


theme name and author's information


7. Splitting The Files


Now you need to understand where to split the file into several files: header.php, sidebar.php, and footer.php. The image below shows a simplified version of my index file and how the markups should split.


splitting files


8. Header.php


Open the index.html file. Cut from the top to where the <!--/header --> ends, paste it in a new PHP file, and save the file as header.php.


header code


Go to the default theme folder, open the header.php. Copy and replace the tags where it requires PHP code (Template Tag): <title>, <link> stylesheet, <h1>, and <div class=description>.


replace code


Navigation Menu (wp_list_pages)


Replace the <li> tags in the <ul id=nav> with <?php wp_list_pages('sort_column=menu_order&depth=1&title_li=');?>


replace code


Reference: wp_list_pages.


9. Sidebar.php


Back to the index.html file, cut from where the <form id=searchform> start to the closing tag of <div id=sidebar> and paste it in a new PHP file, save it as sidebar.php.



  • Replace the <form id=searchform> wrap with <?php include (TEMPLATEPATH . '/searchform.php'); ?>.

  • Replace the category <li> tags with <?php wp_list_categories('show_count=1&title_li='); ?>

  • Replace the archive <li> tags with <?php wp_get_archives('type=monthly'); ?>


sidebar


References: wp_list_categories and wp_get_archives.


10. Footer.php


Back to the index.html file, cut from the <div id=footer> tag to the end of </html> and paste it in a new PHP file, save it as footer.php.


footer


Recent Posts


Here I used the query_post to display the 5 latest posts.


recent posts


Recent Comments


Recent comments are generated by a plugin (included in the theme folder).


recent comments


11. Index.php


Now in your index.html file, you should only have the <div id=content> wrap. Save the file as index.php. Insert the line:get_header, get_sidebar, and get_footer in the same order as your layout structure.


index


12. Understanding The Loop


The image below illustrates how The Loop works. The Loop is used to display blog posts and it also lets you control what to display. Basically, The Loop checks if there are posts in your blog, while there are posts, display it, if no post found, say "Not Found".


the loop


13. Copy The Loop


Go to the default theme folder, open the index.php file. Copy The Loop from the default index.php and paste it in between the <div id=content>..</div>. Then, replace the static text with the WordPress Template Tags: post date, title, category, comments, next and previous link.


the Loop


14. Preview The Theme


Congrats! You’ve done the front page (the main part of the theme). Now, login to your admin panel, go to the Design tab, you should see the GlossyBlue theme, activate it and go to the front page to preview the theme.


15. Single.php


Now, it is time to do the single.php template. If you want, you can go through the same process — cut & paste from the default theme. But, I find it easier to use the index.php that you just created and save it as single.php. Open the default theme single.php file and copy the Template Tags over. Then include the comments_template. The image below highlights what I’ve changed:


single.php


16. Page.php


With the single.php template you just created, save it as page.php. Remove the post date, comment form, next/previous link… and that’s it.. there goes your page.php template.


17. Delete The HTML Files


Delete all the HTML files in the glossyblue folder (we don’t need them anymore). Technically, that is enough for a basic WordPress theme. You may notice there are more PHP files in the default theme. Well, you don’t really need those files if you just want a basic theme. For example, if the search.php or 404.php is not present in the theme folder, WordPress will automatically use the index.php to render the page. Read the Template Hierarchy for more details.


18. WordPress Page Template


Ok, final example. I will show you how to use Page Template to create an archive page that will list all posts on your blog (good for sitemap). Copy the archives.php file from the default theme folder. Delete the unwanted code and you should have something like this:


Archives template


Here I’m using the query_post (showposts=-1 means display all posts) to display a list of all posts.


Archives query posts


Now, login to your admin panel, write a new page, title it Archives. On the Page Template dropdown, select Archives.


Archives page


More Reading:


Check out a list of WordPress Plugins that you may find useful. For more advance theme coding, read my WordPress Theme Hacks.




HTML 5 and CSS 3: The Techniques You’ll Soon Be Using



What We Are Going to Build


This is what our page is going to look like when finished:



Diagram of basic structure

Pretty much your every day blog design. Header with title, horizontal navigation, content area with comments and form, sidebar and a footer. Nothing too special. Let's get building.





HTML 5


HTML 5 is the next major version of HTML. It introduces a bunch of new elements that will make our pages more semantic. This will make it a lot easier for search engines and screenreaders to navigate our pages, and improve the web experience for everyone. In addition, HTML 5 will also include fancy APIs for drawing graphics on screen, storing data offline, dragging and dropping, and a lot more. Let's get started marking up the blog page.





Basic Structure


Before we begin marking up the page we should get the overall structure straight:



Diagram of basic structure

In HTML 5 there are specific tags meant for marking up the header, navigation, sidebar and footer. First, take a look at the markup and I'll explain afterwards:


  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4.     <title>Page title</title>  
  5. </head>  
  6. <body>  
  7.     <header>  
  8.         <h1>Page title</h1>  
  9.     </header>  
  10.     <nav>  
  11.         <!-- Navigation -->  
  12.     </nav>  
  13.     <section id="intro">  
  14.         <!-- Introduction -->  
  15.     </section>  
  16.     <section>  
  17.         <!-- Main content area -->  
  18.     </section>  
  19.     <aside>  
  20.         <!-- Sidebar -->  
  21.     </aside>  
  22.     <footer>  
  23.         <!-- Footer -->  
  24.     </footer>  
  25.   
  26. </body>  
  27. </html>  

It still looks like HTML markup, but there are a few things to note:




  • In HTML 5, there is only one doctype. It is declared in the beginning of the page by <!doctype html>. It simply tells the browser that it's dealing with an HTML-document.


  • The new tag header is wrapped around introductory elements, such as the page title or a logo. It could also contain a table of contents or a search form. Every header typically contains a heading tag from <h1> to <h6>. In this case the header is used to introduce the whole page, but we'll use it to introduce a section of the page a little later.


  • The nav-tag is used to contain navigational elements, such as the main navigation on a site or more specialized navigation like next/previous-links.


  • The section-tag is used to denote a section in the document. It can contain all kinds of markup and multiple sections can be nested inside each other.


  • aside is used to wrap around content related to the main content of the page that could still stand on it's own and make sense. In this case we're using it for the sidebar.

  • The footer-tag should contain additional information about the main content, such as info about who wrote it, copyright information, links to related documents and so on.


Instead of using divs to contain different sections of the page we are now using appropriate, semantic tags. They will make it a lot easier for search engines and screen readers to figure out what's what in a page.





Marking Up the Navigation


The navigation is marked up exactly like we would do it in HTML 4 or XHTML, using an unordered list. The key is that this list is placed inside the nav-tags.


  1. <nav>  
  2.     <ul>  
  3.         <li><a href="#">Blog</a></li>  
  4.         <li><a href="#">About</a></li>  
  5.         <li><a href="#">Archives</a></li>  
  6.         <li><a href="#">Contact</a></li>  
  7.         <li class="subscribe"><a href="#">Subscribe via. RSS</a></li>  
  8.     </ul>  
  9. </nav>  




Marking Up the Introduction


We have already defined a new section in the document using the section tag. Now we just need some content.


  1. <section id="intro">  
  2.     <header>  
  3.         <h2>Do you love flowers as much as we do?</h2>  
  4.     </header>  
  5.     <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut.</p>  
  6. </section>  

We add an id to the section tag so we can identify it later when styling. We use the header tag to wrap around the introductory h2 element. In addition to describing a whole document, the header-tag should also be used to describe individual sections.





Marking Up the Main Content Area


Our main content area consists of three sections: the blog post, the comments and the comment form. Using our knowledge about the new structural tags in HTML 5, it should be easy to mark it up.



Marking up the Blog Post


Go through the markup and I'll explain the new elements afterwards.


  1. <section>  
  2.     <article class="blogPost">  
  3.         <header>  
  4.             <h2>This is the title of a blog post</h2>  
  5.             <p>Posted on <time datetime="2009-06-29T23:31:45+01:00">June 29th 2009</time> by <a href="#">Mads Kjaer</a> - <a href="#comments">3 comments</a></p>  
  6.         </header>  
  7.         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod tellus eu orci imperdiet nec rutrum lacus blandit. Cras enim nibh, sodales ultricies elementum vel, fermentum id tellus. Proin metus odio, ultricies eu pharetra dictum, laoreet id odio...</p>  
  8.     </article>  
  9. </section>  

We start a new section and wrap the whole blog post in an article-tag. The article tag is used to denote an independent entry in a blog, discussion, encyclopedia, etc. and is ideal to use here. Since we are viewing the details of a single post we only have one article, but on the front page of the blog we would wrap each post in an article-tag.


The header element is used to present the header and metadata about the blog post. We tell the user when the post was written, who wrote it and how many comments it has. Note that the timestamp is wrapped in a



Diagram describing use of the datetime HTML attribute


  1. The year followed by a figure dash (a minus sign to you non-typography nerds)

  2. The month followed by a figure dash

  3. The date

  4. A capital T to denote that we are going to specify the local time

  5. The local time in the format hh:mm:ss

  6. The time zone relative to GMT. I'm in Denmark which is 1 hour after GMT, so I write +01. If you were in Colorado you would be 7 hours behind GMT, and you would write -07.



Marking up the Comments


Marking up the comments is pretty straight-forward. No new tags or attributes are used.


  1. <section id="comments">  
  2.     <header>  
  3.         <h3>Comments</h3>  
  4.     </header>  
  5.     <article>  
  6.         <header>  
  7.             <a href="#">George Washington</a> on <time datetime="2009-06-29T23:35:20+01:00">June 29th 2009 at 23:35</time>  
  8.         </header>  
  9.         <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut.</p>  
  10.     </article>  
  11.     <article>  
  12.         <header>  
  13.             <a href="#">Benjamin Franklin</a> on <time datetime="2009-06-29T23:40:09+01:00">June 29th 2009 at 23:40</time>  
  14.         </header>  
  15.         <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut.</p>  
  16.     </article>  
  17. </section>  


Marking up the Comment Form


Several enhancements to forms have been introduced in HTML 5. You longer have to do client-side validation of required fields, emails, etc. The browser takes care of this for you.


  1. <form action="#" method="post">  
  2.     <h3>Post a comment</h3>  
  3.     <p>  
  4.         <label for="name">Name</label>  
  5.         <input name="name" id="name" type="text" required />  
  6.     </p>  
  7.     <p>  
  8.         <label for="email">E-mail</label>  
  9.         <input name="email" id="email" type="email" required />  
  10.     </p>  
  11.     <p>  
  12.         <label for="website">Website</label>  
  13.         <input name="website" id="website" type="url" />  
  14.     </p>  
  15.     <p>  
  16.         <label for="comment">Comment</label>  
  17.         <textarea name="comment" id="comment" required></textarea>  
  18.     </p>  
  19.     <p><input type="submit" value="Post comment" /></p>  
  20. </form>  

There are new two new types of inputs, email and url. Email specifies that the user should enter a valid E-mail, and url that the user should enter a valid website address. If you write required as an attribute, the user cannot submit an empty field. "Required" is a boolean attribute, new to HTML 5. It just means that the attribute is to be declared without a value.





Marking up the Sidebar and Footer


The markup of the sidebar and footer is extremely simple. A few sections with some content inside the appropriate aside- and footer-tags.


You can view the final, unstyled markup here. Now for the styling.





Styling with CSS 3


CSS 3 builds upon the principles about styles, selectors and the cascade that we know so well from earlier versions of CSS. It adds loads of new features, including new selectors, pseudo-classes and properties. Using these new features it becomes a lot easier to set up your layout. Let's dive in.



Basic Setup


To start off with we are going to define some basic rules concerning typography, background color of the page, etc. You'll recognize all of this from CSS 2.1


  1. /* Makeshift CSS Reset */  
  2. {  
  3.     margin: 0;  
  4.     padding: 0;  
  5. }  
  6.   
  7. /* Tell the browser to render HTML 5 elements as block */  
  8. header, footer, aside, nav, article {  
  9.     displayblock;  
  10. }  
  11.   
  12. body {  
  13.     margin: 0 auto;  
  14.     width940px;  
  15.     font13px/22px HelveticaArialsans-serif;  
  16.     background#f0f0f0;  
  17. }  
  18.   
  19. h2 {  
  20.     font-size28px;  
  21.     line-height44px;  
  22.     padding22px 0;  
  23. }  
  24.   
  25. h3 {  
  26.     font-size18px;  
  27.     line-height22px;  
  28.     padding11px 0;  
  29. }  
  30.   
  31. p {  
  32.     padding-bottom22px;  
  33. }  

First we reset margin- and padding-styles with a simple rule. In a production environment I would use a more complete CSS Reset such as Eric Meyer's (for CSS 2.1) but for the scope of the tutorial this will do.


We then tell the browser to render all the new HTML 5 elements as block. The browsers are fine with elements they don't recognize (this is why HTML 5 is somewhat backwards compatible), but they don't know how those elements should be rendered by default. We have to tell them this until the standard is implemented across the board.


Also note how I've chosen to size the fonts in pixels instead of ems or %. This is to maintain the progressive nature of the tutorial. When the major browsers one day are completely finished implementing HTML 5 and CSS 3 we will all have access to page zooming instead of just text resizing. This eliminates the need to define sizes in relative units, as the browser will scale the page anyway.


See what the page looks like with the basic styling applied. Now we can move on to styling the rest of the page. No additional styles are required for the header, so we'll go straight to the navigation.





Styling the Navigation


It is important to note that the width of the body has been defined as 940px and that it has been centered. Our navigation bar needs to span the whole width of the window, so we'll have to apply some additional styles:


  1. nav {  
  2.     positionabsolute;  
  3.     left: 0;  
  4.     width: 100%;  
  5.     backgroundurl("nav_background");  
  6. }  

We position the nav-element absolutely, align it to the left of the window and make it span the whole width. We'll center the nested list to display it within the boundaries of the layout:


  1. nav ul {  
  2.     margin: 0 auto;  
  3.     width940px;  
  4.     list-stylenone;  
  5. }  

Now we'll define some additional styles to make the navigation items look prettier and align them to the grid the layout is based on. I've also included a style for highlighting the page the user is on, and some custom styling for the subscription-link.


  1. nav ul li {  
  2.     floatleft;  
  3. }  
  4.   
  5.     nav ul li a {  
  6.         displayblock;  
  7.         margin-right20px;  
  8.         width140px;  
  9.         font-size14px;  
  10.         line-height44px;  
  11.         text-aligncenter;  
  12.         text-decorationnone;  
  13.         color#777;  
  14.     }  
  15.   
  16.         nav ul li a:hover {  
  17.             color#fff;  
  18.         }  
  19.   
  20.         nav ul li.selected a {  
  21.             color#fff;  
  22.         }  
  23.   
  24.         nav ul li.subscribe a {  
  25.             margin-left22px;  
  26.             padding-left33px;  
  27.             text-alignleft;  
  28.             backgroundurl("rss.png"left center no-repeat;  
  29.         }  




Styling the Introduction


The markup for the introduction is pretty simple: A section with a heading and a paragraph of text. However, we'll use some new CSS 3 tricks to make it look more appealing.


  1. #intro {  
  2.     margin-top66px;  
  3.     padding44px;  
  4.     background#467612 url("intro_background.png"repeat-x;  
  5.     background-size: 100%;  
  6.     border-radius: 22px;  
  7. }  

We are using two new properties. The first one is background-size, which allows you to scale the background-image. In our case, we scale it to 100% on both axes. If the box expands as we add more content to it, the gradient background will scale as well. This is something that was not possible in CSS 2.1 without non-semantic markup and miscellaneous browser issues.





The second new property is border-radius, which applies rounded corners to the element. The radius of our rounded corners are 22px in every corner. You could specify different values for each corner or choose to only round individual corners.




Unfortunately, neither of the properties are fully implemented into the major browsers. However, we can get some support by using vendor-specific attributes. Background-size is supported by newer versions of Safari, Opera and Konqueror. Border-radius is supported by newer versions of Safari and Firefox.


  1. #intro {  
  2.     ...  
  3.     /* Background-size not implemented yet */  
  4.     -webkit-background-size: 100%;  
  5.     -o-background-size: 100%;  
  6.     -khtml-background-size: 100%;  
  7.   
  8.     /* Border-radius not implemented yet */  
  9.     -moz-border-radius: 22px;  
  10.     -webkit-border-radius: 22px;  
  11. }  

Since we have a background-color defined, there will be no major problems in browsers that don't support background-size, such as Firefox. Now we just need to style the heading and the text.


  1. #intro h2, #intro p¬†{  
  2.     width336px;  
  3. }  
  4.   
  5. #intro h2 {  
  6.     padding: 0 0 22px 0;  
  7.     font-weightnormal  
  8.     color#fff;  
  9. }  
  10.   
  11. #intro p {  
  12.     padding: 0;  
  13.     color#d9f499;  
  14. }  

The flower image can be added easily by giving #intro a second background image, something that CSS 3 supports.


  1. #intro {  
  2.     ...  
  3.     background#467612 url("intro_background.png"top left (287px 100%) repeat-x,  
  4.             url("intro_flower.png"top rightright (653px 100%) no-repeat;  
  5.     ...  
  6. }  

We give the two background images explicit dimensions to ensure that they don't overlap, and we're set. Note the shorthand notation of background-size.




Unfortunately, no browser reliably supports this yet, so we'll have to do it the old-fashioned way: by including an inline image and positioning it using CSS. See the final example to see how it was done.





Styling the Content Area and Sidebar


The content area and sidebar are going to be aligned beside each other. Traditionally you would do this by using floats, but in CSS 3 we are going to use tables!


"What?! Tables?" you might ask and look confused. You probably learned years ago that using tables for web layout is a big no-no, and it still is. You should never use the table-element to mark up a layout. However, in CSS 3 we can make elements behave like tables without it ever showing in the markup! To start off with, we're going to need some divs to group the sections in a little more logical manner.


  1. <div id="content">  
  2.     <div id="mainContent">  
  3.         <section>  
  4.             <!-- Blog post -->  
  5.         </section>  
  6.         <section id="comments">  
  7.             <!-- Comments -->  
  8.         </section>  
  9.         <form>  
  10.             <!-- Comment form -->  
  11.         </form>  
  12.     </div>  
  13.     <aside>  
  14.         <!-- Sidebar -->  
  15.     </aside>  
  16. </div>  

Everything still makes sense semantically, but now we can style it. We want the #content div to behave like a table, with #mainContent and aside as table-cells. With CSS 3, this is very easy:




  1. #content {  
  2.     display: table;  
  3. }  
  4.   
  5.     #mainContent {  
  6.         displaytable-cell;  
  7.         width620px;  
  8.         padding-right22px;  
  9.     }  
  10.   
  11.     aside {  
  12.         displaytable-cell;  
  13.         width300px;  
  14.     }  

That's all! No more floating, faux column background images, clearing or collapsing margins. We've made the elements behave like a table, and this makes it much easier for us to do layout.





Styling the Blog Post


The styling of the post header is rather trivial so I'll skip to the fun part: the multi-column layout.



Multiple columns


Multiple columns of text was previously impossible without manually splitting the text, but with CSS 3 it's a piece of cake, although we have to add a div around the multiple paragraphs for this to work with current browsers.


  1. <div>  
  2.     <p>Lorem ipsum dolor sit amet...</p>  
  3.     <p>Pellentesque ut sapien arcu...</p>  
  4.     <p>Vivamus vitae nulla dolor...</p>  
  5.     ...  
  6. </div>  

Now we can add two simple properties and call it a day.


  1. .blogPost div {  
  2.     column-count: 2;  
  3.     column-gap: 22px;  
  4. }  

We want 2 columns and a gap of 22px between the columns. The additional div is needed because there is currently no supported way of making an element span more than one column. In the future, however, you'll be able to specify the column-span property, and we could just write:


  1. .blogPost {  
  2.     column-count: 2;  
  3.     column-gap: 22px;  
  4. }  
  5.   
  6.     .blogPost header {  
  7.         column-span: all;  
  8.     }  

Of course the column-count and column-gap properties are only supported by some browsers, Safari and Firefox. We have to use the vendor-specific properties for now.


  1. .blogPost div {  
  2.     /* Column-count not implemented yet */  
  3.     -moz-column-count: 2;  
  4.     -webkit-column-count: 2;  
  5.   
  6.     /* Column-gap not implemented yet */  
  7.     -moz-column-gap: 22px;  
  8.     -webkit-column-gap: 22px;  
  9. }  


Box shadow


If you look closely at the image in the blog post you'll see a drop-shadow. We are able to generate this using CSS 3 and the box-shadow property.


  1. .blogPost img {  
  2.     margin22px 0;  
  3.     box-shadow: 3px 3px 7px #777;  
  4. }  


Illustration describing how the browsers render the box-shadow CSS property

The first "3px" tells the browser where we want the shadow to stop horizontally. The second "3px" tells it where we want the shadow to stop vertically. The last "7px" is how blurred the border should be. If you set it to 0 it will be completely solid. Last but not least we define the base color of the shadow. This color is of course faded, depending on how much you blur the shadow.


It probably comes as no surprise that this property is not implemented in all browsers yet. In fact, it only works in Safari, and you have to use the vendor-specific property.


  1. .blogPost img {  
  2.     margin22px 0;  
  3.     -webkit-box-shadow: 3px 3px 7px #777;  
  4. }  




Zebra-striping the Comments


Zebra-striping, or highlighting every second element in a series, has traditionally involved selecting all the elements via javascript, then looping through them and highlighting all the odd elements. CSS 3 introduces the pseudo-class "nth-child", which makes it ridiculously simple to do this without javascript. We'll use it to zebra-stripe the comments.


  1. section#comments article:nth-child(2n+1) {  
  2.     padding21px;  
  3.     background#E3E3E3;  
  4.     border1px solid #d7d7d7;  
  5.   
  6.     /* Border-radius not implemented yet */  
  7.     -moz-border-radius: 11px;  
  8.     -webkit-border-radius: 11px;  
  9. }  

The weird value "2n+1" is actually pretty simple if you understand what it stands for:



  • 2n selects every second item. If you wrote 3n it would select every third item, 4n every fourth item, and so on.

  • The +1 tells the browser to start at element 1. If you are familiar with programming you probably know that all arrays start at 0, and this is also true here. This means that element 1 is actually the second element in the series.


Alternatively, you could simply write:


  1. section#comments article:nth-child(odd) { ... }  

Since the standard includes the two most used values as shorthand, odd and even. The rest of the comment styling should be simple to understand with your new knowledge.





Styling the Comment Form, Footer and Sidebar


A couple of CSS 3 techniques are reused in the styling of the comment form, footer and sidebar. In the comment form and the footer I've used the same type of table layout technique used in the main layout. In the sidebar border-radius is used to add rounded corners to the different sections.





The Final Design


See the final design with all styling applied.





Compatibility


The page renders correctly in Safari 4 and newer webkit-based browsers, as it is the only rendering engine that supports all of the CSS 3 techniques we have used. Firefox 3 has some problems applying rounded corners to our flower image and it doesn't support background-size, but besides that the layout works. I've chosen to ignore Internet Explorer as it requires a bit of hacking to get HTML 5 to work. You could also define some more rules and get everything working across major browsers, but all of this is outside the scope of the tutorial.





Conclusion


When HTML 5 and CSS 3 are one day implemented in all browsers it will be a lot easier to build websites. We'll finally be able to stop using floats for layout (which they were never meant to be used for), and we will spend considerably less time writing javascript to scale our background images or zebra-stripe our tables. Hopefully we will use all this extra time to study some long-neglected areas of web design, like front end optimization and proper information architecture.