Testing HTML5 & CSS3

This is the new Header Element in the new Intro Division.

I have used the new header element to try out a few of the new exciting codeing possibilities presented by HTML5 and CSS3. You can set the background size of the flower image you see behind this text so it expands and contracts along with the intro element so it always fills the space. You can create nice looking rounded images by using the new border-radius property and you will also be able to use two background Images.
To find out more about how this intro box was made you can click here or "intro" in the navigation section.

Other New Styles

Posted on by Andrew - 3 comments

Basic Setup

Now we've taken care of the content at the top of the page in the last page we can define some basic rules concerning typography, background color of the page, etc. We can do all of this as we would in CSS 2.1 so I won't show everything here. First we reset margin and padding-styles with a simple rule;

{
margin: 0;
padding: 0;
}

Block Declaration

We then tell the browser to render all the new HTML 5 elements as block;

header, footer, aside, nav, article
{display: block;}

The browsers are fine with the new 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, hence the block declaration.

Font Size

Also note how I've chosen to size the fonts for elements such as h1 and h2 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 HTML5 and CSS3 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.

h3{
font-size: 18px;
line-height: 22px;
padding: 11px 0;
}

No additional styles are required for the header, so we'll go straight to the navigation.

Navigation

It is important to note that the width of the body has been defined as 940px and that it has been centered, see the second and third lines below;

body{
margin: 0 auto;
padding: 22px 0;
width: 940px;
font: 13px/22px Helvetica, Arial, sans-serif;
background: #f0f0f0;
}

Our navigation bar needs to span the whole width of the window, so we'll have to apply some additional styles;


nav {
position: absolute;
left: 0;
width: 100%;
background-color:#333333;
}

We position the nav-element absolutely, align it to the left of the window and make it span the whole width of the page.

We'll center align the nested list items by setting the second margin declaration to auto to center it within the boundaries of the layout:

nav ul {
margin: 0 auto;}

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;

nav ul {
margin: 0 auto;
width: 940px;
list-style: none;}

Styling Zebra Stripes for the Comments Section

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. CSS3 introduces the pseudo-class "nth-child", which makes it ridiculously simple to do this without javascript. For this example we'll use it to zebra stripe the articles within the section called comments.

section#comments article:nth-child(2n+1) {
padding: 21px;
background: #E3E3E3;
border: 1px solid #d7d7d7;}

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:

section#comments article:nth-child(odd)

to create the same effect but highlight the even number comments you can use either of the selectors below;

section#comments article:nth-child(2n+0)

section#comments article:nth-child(even)

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.

Comments

Iggy Pop on

Dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut.

Benny Hill on

Click here to learn how you can now apply a Zebra effect to the comments without using JavaScript.

Postman Pat on

This is an ideal section to explain how you zebra stripe the comments using the new css3 styles.

Post a comment