30 August 2010
ページ ツール |
初級
This is Part 2 of a three-part tutorial series exploring the HTML5 and CSS3 features in Dreamweaver CS5 version 11.0.3 . Part 1 shows how to build a web page for a fictional restaurant, Citrus Café, using HTML5 structural elements, such as <header>, <footer>, <nav>, <section>, and <article>. In this part, you'll style the page using CSS3 properties—such as text and box shadows, rounded corners, and transitions—that are widely supported in modern browsers and in Dreamweaver CS5 Live view. Because CSS3 is still being developed, you need to use browser-specific properties in combination with the official properties. In this tutorial, you'll learn how to access the browser-specific properties in Dreamweaver CS5's code hints. You'll also learn how to persuade Internet Explorer to apply styles to the new HTML5 structural elements.
After styling the page for display on a desktop computer, you'll learn in Part 3 how to add CSS media queries to optimize the styles for a tablet device and mobile phone using the Multiscreen Preview panel.
This tutorial is divided into the following sections:
In Part 1 of this tutorial series, you built the web page for the Citrus Café site using HTML5 structural markup. If you didn't complete Part 1, you should do so before continuing with this part. Alternatively, you can use the download files for this part, citrus_pt2_start.zip if you haven't already . When unzipped, define a Dreamweaver site called Citrus Café with the citrus folder as the local site folder. Use index.html in the site root and desktop.css in the css folder as your work files. The completed files are in the completed folder.
The Citrus Café web page is currently completely unstyled, but the content follows a logical structure, which makes it accessible and search engine friendly see Figure 1 .
You now need to style the page with CSS, first for display in a desktop computer. These styles will form the basis for two other style sheets that optimize the page display for screens with smaller resolutions, such as tablet devices and mobile phones. The supplementary styles will be applied using CSS media queries to limit their application depending on the detected screen resolution. Not all browsers support CSS media queries, so it's important to have one style sheet that can be understood by all browsers. That's the style sheet you'll create in this part of the tutorial series, in preparation for using the Dreamweaver CS5 Multiscreen Preview panel see Figure 2 in Part 3 to optimize the layout for smaller screen resolutions.
Although index.html uses HTML5 structural elements that hadn't even been thought of when Internet Explorer 6 was released in 2001, the unstyled page displays correctly in that ancient browser. However, once you start adding CSS to the page, not only Internet Explorer 6, but also Internet Explorer 7 and Internet Explorer 8 fail to apply style rules to the new elements. Fortunately, a little bit of JavaScript magic cajoles these miscreants into compliance. The script keeps Internet Explorer 6, Internet Explorer 7, and Internet Explorer 8 happy by creating dummy HTML5 structural elements in the page's Document Object Model DOM , persuading them to render the styles as expected. So, the first task is to add the script to the <head> of the page.
The JavaScript file can be served either from your site or from the Google Code content distribution network CDN . The following instructions describe both methods.
</head> tag.<head> of the page:<script type="text/javascript" src="javascript/html5.js"></script>
This applies the local version of the script.
If you want to use the Google Code CDN, amend the src attribute like this:
src="http://html5shiv.googlecode.com/svn/trunk/html5.js"
Note: Using a local version of the script ensures that the file is always available. Serving the file from a CDN runs the risk admittedly a small one that the remote server might not be available, but it has the advantage that the file might already be in the user's browser cache as a result of visiting another site. Since the script is only 2 KB, downloading the file from either source is unlikely to affect the speed of your page.
<script> tag in the following Internet Explorer conditional comment:<!--[if lte IE 8]>
<script src="javascript/html5.js"></script>
<![endif]-->
This ensures that the script is downloaded only by Internet Explorer 8 or earlier. Other browsers ignore it although it doesn't do any harm if they download it, too .
If anyone visits your site with JavaScript disabled in Internet Explorer 8 or earlier, the HTML5 structural elements won't be styled, but the rest of the page will be. If a significant part of your target audience is likely to be using Internet Explorer with JavaScript turned off, you should replace the HTML5 structural elements with <div> elements or wrap them in <div> elements, and apply the CSS to the <div> elements.
Note: This script must be included in the <head> of the page. It won't work if you add it before the closing </body> tag.
The <header> section contains an <hgroup> element with the main heading and subtitle. This is necessary for search engines and screen readers for the blind. For display in visual browsers, the <hgroup> will be replaced by an attractive logo with the text in an image.
Note: Dreamweaver has several ways of doing most things. You can also open the New CSS Rule by selecting Format > CSS Styles > New, or by selecting <New CSS Rule> from the Targeted Rule pop-up menu of the Property inspector in CSS mode. For brevity, I'll just tell you to open the New CSS Rule dialog box. Choose whichever method suits your workflow best.
All basic rules will be defined in desktop.css, so use this setting all the time until you start creating the alternative rules for different resolutions in Part 3.
In the Type category, set Font-family to Trebuchet MS, Arial, Helvetica, sans-serif, and color to black #000 .
In the Background category, set Background-color to #66B034.
In the Box category, set Margin and Padding to 0, and leave the Same for all check boxes selected.
Click OK to create the style definition. The background turns a lime green, and the font is changed.
container <div> . This uses an ID, so set Selector Type in the New CSS Rule dialog box to ID, and Selector Name to #container. Click OK.Set Margin Top and Bottom to 0, and Right and Left to auto.
Click OK. The page's content is centered in a fixed width.
<header#logo> in the Tag selector at the bottom of the Document window.In the Box category, set Width to 100% and Height to 138px.
When you click OK, the Citrus Café logo is inserted as a background image at the top of the page, but the <hgroup> element sits on top of it.
<hgroup> , and select <hgroup> in the Tag selector.<hgroup> should disappear. If Design view fails to refresh, press F5 or toggle Live view on and off.The text headings are no longer visible in a visual browser, but they will still be detected by search engines and screen readers for the blind.
The top section of index.html should now look like Figure 3.
Moving the <hgroup> out of the way has resulted in the rest of the page content moving up, with the menu sitting on top of the logo. You'll fix that next.
Styling an unordered list for a menu is now widely accepted as one of the most accessible ways of adding site navigation. Thanks to new CSS3 properties being adopted by many browsers, it's possible to achieve effects that previously relied on images and JavaScript. The CSS Rule Definition dialog box doesn't yet support CSS3 properties, so let's begin by creating the basic menu before adding flourishes of CSS3 goodness.Creating the basic menu styles
<nav> element, and Design view seems to share the same opinion, but Figure 4 shows what happens if you do.
This doesn't have anything to do with the use of HTML5. It's the old story of adjacent vertical margins merging in CSS. The <header> element has no top margin and the <hgroup> has been removed from the flow by absolute positioning, so adding a top margin to the <nav> element pushes the <header> from the top of the page, bringing the background images with it.
Backgrounds are displayed through padding, so the solution is to use padding.
In the New CSS Rule dialog box, set Selector Type to Tag, and select nav from the Selector Name pop-up menu all the HTML5 elements are listed . Click OK to open the CSS Rule Definition dialog box.
However, if you turn on Live view, the content below the menu moves up to obscure it. Let's fix that by creating a rule for the maincontent <div> .
The <div> uses an ID, so set Selector Type to ID in the New CSS Rule dialog box. Set Selector Name to #maincontent.
There's still some overlap in Live view, but that will be eliminated when the menu items are floated.
ul {
padding:0;
margin:0;
}
nav ul {
list-style: none;
margin-bottom: 15px;
font-weight: bold;
font-size:20px;
}
nav ul li {
float: left;
}
nav ul a {
display: block;
width:140px;
padding: 10px;
text-align:center;
text-decoration: none;
color: #fff;
border: 1px solid #618A37;
margin: 5px 0px;
}
If possible, continue working with Live view activated. You need to work directly in desktop.css because the CSS3 properties are available only through code hints in Code view.
nav ul a style definition, and press Enter/Return to insert a new line. The code hints for CSS properties should appear.
CSS properties that begin with a hyphen are browser-specific. Because CSS3 is still being developed, you need to use the browser-specific versions followed by the standard property. Properties prefixed with –moz are implemented by Firefox, -o is for Opera, and –webkit is for Safari and Google Chrome.
–moz properties. Continue typing or use the down arrow key until you find border-radius see Figure 7 , and double-click or press Enter/Return to add the property to the style rule.
-moz-border-radius to 6px. Pressing F5 refreshes Live view, but you won't see any change, becSet the value of -moz-border-radius to 6px. Pressing F5 refreshes Live view, but you won't see any change, because Live view is powered by the WebKit browser engine.-o submenu, you'll see it's quite short.You should always finish with the official CSS3 property. Once browsers support the official property, they will ignore the browser-specific one.
With the -moz , -webkit and official versions added, the nav ul a style rule should look like this:
nav ul a {
display: block;
width:140px;
padding: 10px;
text-align:center;
text-decoration: none;
color: #fff;
border: 1px solid #618A37;
margin: 5px 0px;
-moz-border-radius:6px;
-webkit-border-radius:6px;
border-radius:6px;
}
nav ul a style rule:-moz-box-shadow: rgba(0,0,0,0.3) 1px 1px 3px;
-webkit-box-shadow: rgba(0,0,0,0.3) 1px 1px 3px;
box-shadow: rgba(0,0,0,0.3) 1px 1px 3px;
The box-shadow property takes a color and three length values. The first length is the horizontal offset, the second length is the vertical offset, and the third one is the blur radius of the shadow. Positive values for the first two lengths put the shadow to the right and bottom of the element. Negative values put the shadow on the left and top. So, these property definitions add a shadow to the menu buttons offset one pixel to the right and bottom, with a 3 pixel blur.
The colors here are expressed using the rgba syntax, which is new to CSS3. The first three values represent red, blue, and green on a scale from 0 to 255 you can also use percentages . The final value represents opacity on a scale of 0 to 1, ranging from transparent 0 to fully opaque 1 .
Note: Dreamweaver doesn't support the conversion of hexadecimal color values to rgba . You can download a free Convert to RGBA Dreamweaver extension from my website at http://foundationphp.com/tools/.
nav ul a style rule:text-shadow: rgba(0,0,0,0.8) 1px 1px 1px;
You don't need browser-specific versions for this property. Although it's part of CSS3, it was originally part of CSS2, but was dropped from the specification through lack of browser support. Now, all current browsers—except one—support it. No prizes for guessing that the odd one out is Internet Explorer.
nav ul a:link, nav ul a:visited {
background: rgba(255,255,255,0.2);
}
nav ul a:hover, nav ul a:active, nav ul a:focus {
background: rgba(255,255,255,0.4);
}
These two rules add a translucent white background to the menu buttons using the new rgba syntax. In their normal or visited state, the white background has an opacity of 20%. When in the hover, active, or focus states, the opacity increases to 40%, adding a highlight effect to the buttons.
nav ul li:hover {
margin-top:-10px;
}
This moves each menu button 10 pixels up when you hover over it.
nav ul li rule like this:nav ul li {
float: left;
-webkit-transition-duration:.5s;
-webkit-transition-property:margin-top;
-webkit-transition-timing-function:ease-in-out;
-o-transition-duration:.5s;
-o-transition-property:margin-top;
-o-transition-timing-function:ease-in-out;
-moz-transition-duration:.5s;
-moz-transition-property:margin-top;
-moz-transition-timing-function:ease-in-out;
transition-duration:.5s;
transition-property:margin-top;
transition-timing-function:ease-in-out;
}
It looks like a lot of typing, but there are three basic properties: transition-duration , transition-property , and transition-timing-function . At the moment, only WebKit browsers and Opera support these properties, but they're due to be supported in Firefox 4. You need to use the browser-specific properties, but they're all the same apart from the prefix, and they all take the same values.
This sets the duration of the transition at half a second, and applies it to margin-top, easing slowing down the transition at the start and finish.
The remaining parts of the page use standard CSS2.1. The main points to notice concern the background to the restaurant's mission statement, and the smaller images in the pods.
The background image to the mission statement lrg_hero.jpg is 819 pixels wide and 297 pixels high. However, the text needs to be constrained to the left side by padding. Because padding is added outside the content of an element, you need to subtract the value of the padding from the width applied to the <div> element.
The other point is that there are three pods under the mission statement. That leaves only 273 pixels for each pod, including the gaps between them. However, the images in the first two pods are both 302 pixels wide. They have been made that size because med_hero.jpg, the background image that will be used in the design optimized for tablet devices is 669 pixels wide. In the tablet design, there will be just two pods under the mission statement, so the images need to be displayed at their full width. To make them fit the narrower pods in the desktop version, the overflow property of the pod's content needs to be set to hidden.
The mission statement is in an <article> element that has an ID, so you can use an ID selector to style it.
#vision {
font-family:"Palatino Linotype", "Book Antiqua", Palatino, serif;
font-size:32px;
font-weight:bold;
background-image:url(../images/hero.jpg);
background-repeat:no-repeat;
}