< Less-than sign ¢ Cent sign ‘ Left single quote < ¢ ‘ < ¢ ‘ > Greater-than sign £ Pound sign ' Right single quote > £ ’ & £ ’ & Ampersand ¥ Yen sign “ Left double quotes & ¥ “ & ¥ “ \" Quotation mark ¤ Euro sign ” Right double quotes " € ” " € ” © Copyright symbol × Multiplication sign © × © × ® Registered trademark ÷ Division sign ® ÷ ® ÷ ™ Trademark ™ ™ EXTRA MARKUP 194
This example starts by using a content. Several elements use Parts of the page have been DOCTYPE to indicate that this the id and class attributes grouped using <div> elements, is an HTML 4 web page. In the to identify their purpose. The and comments have been added head, you can also see a <meta> copyright symbol has been to indicate what the </div> tag describing the page's added using an escape code. elements are closing. 195 EXTRA MARKUP
Example EXTRA MARKUP <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <meta name=\"description\" content=\"Telephone, email and directions for The Art Bookshop, London, UK\" /> <title>Contact The Art Bookshop, London UK</title> </head> <body> <div id=\"header\"> <h1>The Art Book Shop</h1> <ul> <li><a href=\"index.html\">home</a></li> <li><a href=\"index.html\">new publications</a> </li> <li class=\"current-page\"> <a href=\"index.html\">contact</a></li> </ul> </div><!-- end header --> <div id=\"content\"> <p>Charing Cross Road, London, WC2, UK</p> <p><span class=\"contact\">Telephone</span> 0207 946 0946</p> <p><span class=\"contact\">Email</span> <a href=\"mailto:[email protected]\"> [email protected]</a></p> <iframe width=\"425\" height=\"275\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"http://maps.google.co.uk/maps?f=q& source=s_q&hl=en&geocode=& q=charing+cross+road+london&output=embed\"> </iframe> </div><!-- end content --> <p>© The Art Bookshop</p> </body> </html> EXTRA MARKUP 196
Summary EXTRA MARKUP XX DOCTYPES tell browsers which version of HTML you are using. XX You can add comments to your code between the <!-- and --> markers. XX The id and class attributes allow you to identify particular elements. XX The <div> and <span> elements allow you to group block-level and inline elements together. XX <iframes> cut windows into your web pages through which other pages can be displayed. XX The <meta> tag allows you to supply all kinds of information about your web page. XX Escape characters are used to include special characters in your pages such as <, >, and ©.
9 Flash, Video & Audio XX How to add Flash movies into your site XX How to add video and audio to your site XX HTML5 <video> and <audio> elements
Flash is a very popular technology used to add animations, video, and audio to websites. This chapter begins by looking at how to use it in your web pages. We then focus on how to add video and audio to your site, using either the new HTML5 <video> and <audio> elements or a hosted service (such as YouTube or SoundCloud). In this chapter you will learn: ●● How to use Flash in your web pages ●● How to use HTML5 <video> and <audio> elements ●● When to host your own video and audio and when to use a service such as YouTube 201 FLASH, VIDEO & AUDIO
FLASH, VIDEO & AUDIO 202
How Flash Works Since the late 1990s, Flash has been a very popular tool for creating animations, and later for playing audio and video in websites. Whether you are creating an When you create a Flash file in To view Flash, browsers need animation or a media player in the Flash authoring environment, to use a plugin (an extra piece Flash, the files you put on your it is saved with the .fla file of software that runs in the website are referred to as Flash extension. In order to use this file browser) called the Flash Player. movies. on a web page it has to be saved Statistics commonly indicate in a different format known that 98% of browsers on If you want to create your as SWF. (It has the .swf file desktop computers have the own Flash movie, you need to extension.) Flash plugin installed. (The purchase the Flash authoring percentage of mobiles and environment from Adobe. When you export the movie tablets with it is much less.) into SWF format, Flash creates There are, however, several code that you can use to embed There is not space in this book companies that offer Flash the Flash movie in your page. to teach you how to create Flash animations and slideshows, Traditionally, this code used the movies (there are many books as well as video and audio HTML <object> and <embed> devoted to that one topic), but players that you can use without tags. However, now it is more this chapter will show you how purchasing this tool. common to use JavaScript. to add Flash movies to your site. The Flash authoring environment The .fla file is exported to .swf The .swf file is included in your is used to create Flash Movies. format to use in a web page. web page using JavaScript. 203 FLASH, VIDEO & AUDIO
Use of Flash Since 2005, a number of factors have meant that fewer websites are written in Flash or even use elements of Flash in their pages. When Flash was first released, There are several reasons why There have been laws introduced it was developed to create fewer websites are using Flash to ensure that websites are animations. The technology these days, including: usable by those with visual or quickly evolved, however, and physical impairments — and people started to use it to build In 2005-6, a set of JavaScript Flash has been criticized because media players and even entire libraries were launched Flash content does not always websites. (including Prototype, meet accessibility requirements. script.aculo.us, and JQuery) Although Flash is still very which made it easier for people In 2008, browsers started to popular, in recent years people to create animated effects using support HTML5 <video> and have been more selective about JavaScript. <audio> tags. At the time of when they use it (and now rarely writing, Flash is still a popular consider building an entire When Apple launched the way of playing video and audio website in Flash). iPhone in 2007 and later the on the web but more and more the iPad in 2010, they took the people are switching to HTML5. Despite this, Flash does have a decision not to support Flash. future on the web because there (You will see how to use these are some things it does very well, elements later in the chapter.) such as creating animations. FLASH, VIDEO & AUDIO 204
Timeline: Flash, VidEo & Audio Web technologies change quickly. Here you can see some of the changes in how animation, video, and audio are created on the web. 1995 1996 1997 1998 1999 2000 2001 2002 2003 FutureSplash, forerunner to Flash Animation RealAudio streaming audio Flash 4 streaming audio Audio RealVideo streaming video RealVideo 8 streaming video Flash MX Flash video Video On this page you can see the On the facing page, you can see first major players to provide some of the technologies and web animation, audio, and video. events replacing them. 205 FLASH, VIDEO & AUDIO
Video sharing sites offer alternatives to self-hosting Vimeo launched YouTube launched YouTube releases iPhone app Vimeo releases HTML5 player YouTube releases HTML5 player Vimeo releases iPhone app 2004 2005 2006 2007 2008 2009 2010 2011 Internet Explorer 9 Opera 9 .62 Firefox 3.5 Chrome 3 Safari 3.1 Browsers introduce HTML5 <video> and <audio> tags iPhone iPad Apple releases devices that don't support Flash jQuery script.aculo.us Prototype JavaScript libraries are written to create animated effects FLASH, VIDEO & AUDIO 206
Adding a Flash Movie to Your Web Page The most popular way of chapter-09/adding-a-flash-movie.html HTML adding Flash into a web page is using JavaScript. There are <!DOCTYPE html> several scripts that allow you <html> to do this without an in-depth <head> understanding of the JavaScript <title>Adding a Flash Movie</title> language. <script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/ The script we will be looking at swfobject/2.2/swfobject.js\"></script> here is called SWFObject. You <script type=\"text/javascript\"> can obtain a copy of it for free swfobject.embedSWF(\"flash/bird.swf\", from Google, and you can see \"bird\", \"400\", \"300\", \"8.0.0\");</script> how we use it on the next page. </head> <body> One advantage to using this <div id=\"bird\"><p>An animation of a bird taking technique is that it allows a shower</p></div> browsers to show alternative </body> content for users whose </html> browsers are not capable of showing Flash. The SWFObject script will check If you use a text description as to see if the user's browser can alternative content, then you can This technique uses a <div> play the Flash movie. If it can, the achieve two further benefits: element to create a space where script will replace the content of the Flash movie should sit. the <div> with the .swf file. 1. The text can be accessed by The <div> element has an id those with visual and/or physical attribute whose value is used For users who cannot see the impairments who are not able to by the SWFObject script. In this Flash movie, you could show a interact with the Flash file. example, the value of the id still from the movie instead. You attribute is bird. might also like to consider using 2. The text can be indexed by a text description of the Flash search engines (which are not as Inside the <div> element you file. effective at indexing SWF files), can place the alternative content increasing the chance that your for users who are not able to content will be found. play Flash. 207 FLASH, VIDEO & AUDIO
R e s u lt Article In this example, the SWFObject script is hosted on Google's servers. We include the script in this web page using the first of the two <script> elements. The type attribute is used on the <script> element to indicate that the script inside is written in JavaScript. The src attribute tells the browser where to find the script. The second <script> element is used to tell the browser about the Flash movie, as well as which element it should replace. This element is actually telling the SWFObject script five pieces of information, which are in the brackets: 1. The location of the .swf file: flash/bird.swf 2. The element that the Flash movie should replace, specified by the value of the id attribute on the <div> element: bird 3. The width of the Flash movie: 400 px 4. The height of the Flash movie: 300 px 5. The minimum version of the Flash player needed to view the movie: Flash Player 8 FLASH, VIDEO & AUDIO 208
Understanding Video Formats and Players To add video to your site, there are two key issues to understand: file formats and video players/plugins. Formats Players / pluginS Approach Movies are available in many Browsers were initially designed The easiest way to add video formats (BluRay, DVD, VHS, to show text and images only. to your site is to use a hosted to name a few). Online, there For this reason, browsers built service such as YouTube or are even more video formats prior to 2010 generally required Vimeo. (including AVI, Flash Video, another program called a player H264, MPEG, Ogg Theora, or plugin to to be installed in However, there are some cases QuickTime, WebM, and order to play video content. where using these services is Windows Media). not appropriate (as you will see These players and plugins only on the next page) and you will Just as your DVD player won't supported certain video formats. want to host the video on your play a VHS cassette, browsers own site. differ in what video formats they Recently browsers have evolved do and don't support. to support the HTML5 <video> At the time of writing, to ensure tag (which renders players and most people can play your In order for users to view your plugins obsolete). video content, it is considered video online, you may need to best practice to use the HTML5 convert it to another format. Unfortunately, however, you <video> element for browsers cannot rely on every visitor to that support it, and also Flash The process of converting your website having a recent video for those that do not.This a video into another format browser that supports this means you would need to upload is sometimes referred to as new HTML5 element and the any videos you want to show in \"encoding\" the video. browsers that do recognize the at least two different formats: <video> element require the WebM and MP4. There are several apps available video to be encoded in different on the web that enable you to formats. encode videos (such as www. mirovideoconverter.com). 209 FLASH, VIDEO & AUDIO
Using Hosted Video Services The easiest way to add a video to your site is to upload the video to a site like YouTube or Vimeo and use the features provided on their site to embed the video in your page. Advantages Disadvantages The Alternative Hosted video sites (such as Your video will be available on If you want to host video on your YouTube) provide players that the site of the hosted service, own site - rather than a hosted work with the majority of web so if you want the content to be service - a lot more work is browsers. exclusively available on your site involved in setting up your site to (and not visible on other sites), play the video. You do not need to worry about you need to host the video on encoding your video since your own server and add your We will be looking at two these sites allow you to upload own player into the page. different ways that you can host your content in a range of your own videos: using both formats. Once uploaded, they Some services will limit what Flash Video and the HTML5 automatically convert your video your video is allowed to include. <video> element. into the various formats required For example, most prohibit by different browsers. the use of advertising within In order to ensure that the the video you upload (which maximum number of visitors to Web hosting companies often prevents you from monetizing your site can see the video, you charge extra if you use a lot of that content). will need to use a combination of bandwidth, and video files can both of these techniques. be quite large. Therefore, it can Some hosted services will play cost you extra to host the videos their own adverts before your on your own site. If your video video will begin, or even overlay is hosted on a site like YouTube them over the screen as your or Vimeo, however, you do not video is playing. The quality of need to pay for the bandwidth. video on some hosted services can also be limited. FLASH, VIDEO & AUDIO 210
Preparing a Flash Video for Your Site There are three steps you need to follow to add 3 a Flash Video to your web page: 12 Convert Your Video FinD an FLV Player to Include the Player & into FLV Format Play the Video Video in Your Page To play a Flash Video, you need You'll need a player written in You can include the player in to convert your video into FLV Flash to play the FLV file. Its your page using a JavaScript format. Since Flash 6, the Flash purpose is to hold the FLV movie technique such as SWFObject, authoring environment has come and add controls such as which was mentioned earlier in with a Flash Video Encoder to play/pause. Here are two sites this chapter. convert videos into FLV format. that offer FLV players: You will also need to tell the Some Flash video players www.osflv.com player where it can find the video also support a format called www.longtailvideo.com file that you want it to play. H264 (and some video editing (Some players have advanced programs export video in this You do not need the Flash features such as the ability format). authoring environment to use to create playlists of multiple either of these on your website. videos, or add a still picture Googling \"FLV or H264 before the video plays.) converters\" will allow you to find alternative encoding software. I have provided a sample FLV In the following example, we will In the following example, we will file that you can use with the use the OS FLV player, which is also be using the SWFObject download code on the website a free, open-source Flash Video JavaScript technique mentioned (It is in a separate folder because player. This is included in the on pages 207-208. the video files are large.) download code. It only supports the FLV format (not H264). 211 FLASH, VIDEO & AUDIO
Adding a FlasAhrVtiidceloe to Your Pages HTML chapter-09/adding-a-flash-video.html This example uses the OS FLV player to display a video called <!DOCTYPE html> puppy.flv, which has already <html> been convered into FLV format. <head> <title>Adding a Flash Video</title> You have already seen how to <script type=\"text/javascript\" use SWFObject to embed a src=\"http://ajax.googleapis.com/ajax/libs/ basic animation in a page, but swfobject/2.2/swfobject.js\"></script> sometimes Flash movies need <script type=\"text/javascript\"> information in order for them var flashvars = {}; to work. In this example, the var params = {movie:\"../video/puppy.flv\"}; video player needs to know the swfobject.embedSWF(\"flash/splayer.swf\", path to the video it has to play, \"snow\", \"400\", \"320\", \"8.0.0\", so SWFObject uses JavaScript flashvars, params);</script> variables to pass this information </head> to the Flash movie. These are <body> provided in the two lines of code <div id=\"snow\"><p>A video of a puppy playing in that start with var. the snow</p></div> </body> This particular player is not </html> expecting any information in the flashvars variable, so that is R e s u lt left empty. The path to the movie is supplied in the variable called params. var params = {movie: \"../videos/puppy.flv\"}; The line after the variables is the one that tells the script to replace the HTML element with the video player. It is very similar to the one you saw in the earlier example that introduced SWFObject. Different video players usually require information such as the path to the video in slightly different formats, but they usually come with examples and documentation to help you understand how to use them. FLASH, VIDEO & AUDIO 212
HTML5: Preparing Video for Your Pages Despite the HTML5 <video> element being a very recent addition, it is enjoying widespread use. Here are some of the key issues to be aware of: Support Formats Controls The new HTML5 <video> Not all browsers support the The browser supplies its own element is only supported by same video formats. Therefore, controls for the player, and recent browsers, so you cannot you need to supply your video in these can vary from browser just use this one technique if you more than one format. to browser. You can control the want everyone to be able to see appearance of these controls your video (you need to combine To reach as many browsers as using JavaScript (but that is this HTML5 with Flash Video). possible, you should provide the beyond the scope of this book). video in the following formats: Digital Rights In the Browser H264: IE and Safari At the time of writing, the WebM: Android, Chrome, One of the problems with players <video> element does not Firefox, Opera such as the Flash Player is that support any type of Digital they can behave inconsistently Rights Management (DRM — Chrome, Firefox, and Opera have when elements such as menus sometimes referred to as copy indicated that they will support drop over them, or the window is protection). But a dedicated a format called WebM. (Some scaled up or down. The HTML5 pirate will usually find a way Flash players also support H264, option solves these issues. around DRM. and WebM - which will save on the number of conversions). On page 222 you will see how I have provided a sample video in If you look at this example in to combine this HTML5 video H264 and WebM format for you Firefox and Opera you will see technique with Flash Video to to try with the code downloads. different controls when you achieve wider reach. hover over the video. 213 FLASH, VIDEO & AUDIO
HTML5: AddinAgrVtiidceloe to Your Pages HTML chapter-09/adding-html5-video.html <video> <!DOCTYPE html> The <video> element has a <html> number of attributes which allow <head> you to control video playback: <title>Adding HTML5 Video</title> </head> src <body> <video src=\"video/puppy.mp4\" This attribute specifies the path poster=\"images/puppy.jpg\" to the video. (The example video width=\"400\" height=\"300\" is in H264 format so it will only preload work in IE and Safari.) controls loop> poster <p>A video of a puppy playing in the snow</p> </video> This attribute allows you to </body> specify an image to show while </html> the video is downloading or until the user tells the video to play. In HTML5 you do not need to preload supply values for all attributes, width, height such as the controls, autoplay, This attribute tells the browser and loop attributes used what to do when the page These attributes specify the size with the <video> element. loads. It can have one of three of the player in pixels. These attributes are like on/ values: off switches. If the attribute is controls present, it turns that option on. none If the attribute is omitted, the When used, this attribute option is turned off. The browser should not load indicates that the browser the video until the user presses should supply its own controls If the browser does not play. for playback. support the <video> element or the format of video used, auto autoplay it will display whatever is between the opening <video> The browser should download When used, this attribute and closing </video> tags. the video when the page loads. specifies that the file should play automatically. metadata loop The browser should just collect information such as the size, When used, this attribute first frame, track list, and indicates that the video should duration. start playing again once it has ended. FLASH, VIDEO & AUDIO 214
HTML5: Multiple Video Sources <source> chapter-09/multiple-video-sources.html HTML To specify the location of the file <!DOCTYPE html> to be played, you can use the <html> <source> element inside the <head> <video> element. (This should <title>Multiple Video Sources</title> replace the src attribute on the </head> opening <video> tag.) <body> <video poster=\"images/puppy.jpg\" width=\"400\" You can also use multiple height=\"320\" preload controls loop> <source> elements to specify <source src=\"video/puppy.mp4\" type='video/ that the video is available in mp4;codecs=\"avc1.42E01E, mp4a.40.2\"' /> different formats. <source src=\"video/puppy.webm\" type='video/ webm;codecs=\"vp8, vorbis\"' /> (Due to a bug on the iPad, you <p>A video of a puppy playing in the snow</p> should provide the MP4 video </video> as the first format. Otherwise, it </body> might not play.) </html> src R e s u lt This attribute specifies the path If the browser does not support Online Extra to the video. the <video> element or the We have provided links to tools format of video used, it will that help you encode videos and type display whatever is between the audio into the correct formats in opening <video> and closing the Tools section of the website. You should use this attribute </video> tags. to tell the browser what format the video is. Otherwise, it will download some of the video to see if it can play the file (which will take time and bandwidth). codecs The codec that was used to encode the video is supplied within the type attribute. Note the use of single quotes, as well as double quotes in the type attribute, when it is supplied. 215 FLASH, VIDEO & AUDIO
HTML5: Combining Flash & HTML5 Video By offering your videos in both HTML5 and Flash Video formats, you will ensure that it can be viewed by the majority of users on your site. You may choose to offer HTML5 Because some of the video If you start to work with HTML5 as the first option, and Flash players built in Flash support video in depth, you can also: video as a fallback for people H264 encoding, if you use a whose browser does not support player that supports this format ●● Create your own playback HTML5 video. Or you may work you would only need to provide controls the other way around. the video in H264 and WebM formats. (You would not need ●● Provide different versions it in FLV format as well.) You of the video for browsers will see this demonstrated in that have different sized the example at the end of the screens (so you can provide chapter. lower resolution content for handheld devices) ●● Tell different parts of a page to change when the video reaches a certain point FLASH, VIDEO & AUDIO 216
Adding Audio to Web Pages By far the most popular format for putting audio on web pages is MP3. As with video, there are three routes commonly taken: 1 23 Use a Hosted Service Use Flash Use HTML5 There are several sites that allow There are several Flash movies HTML5 has introduced a new you to upload your audio, and that allow you to play MP3 files; <audio> element. Browsers that provide a player which you can from simple buttons that play support this element provide embed in your page, such as one track to complex players their own controls — much as SoundCloud.com and that allow you to create playlists they do for the video files we just MySpace.com. and juke boxes. looked at. Some people ask how to get This is actually quite difficult This is why some sites offer music to play consistently even to achieve and would rely on audio players in new windows, when visitors move from one techniques like using AJAX to so that listeners are not page to another on a website. load page content or developing interrupted when they move the entire site in Flash. between pages. 217 FLASH, VIDEO & AUDIO
Adding aArFtLiacslhe MP3 Player HTML chapter-09/adding-a-flash-mp3-player.html There are many MP3 players that have already been written in <!DOCTYPE html> Flash, such as: <html> <head> flash-mp3-player.net <title>Adding a Flash MP3 Player</title> musicplayer.sourceforge.net <script type=\"text/javascript\" w w w.wim pyplayer.co m src=\"http://ajax.googleapis.com/ajax/libs/ swfobject/2.2/swfobject.js\"></script> Each of these players has <script type=\"text/javascript\"> different functionality, so check var flashvars = {}; their features before choosing var params = {mp3: \"audio/test-audio.mp3\"}; one for your site. swfobject.embedSWF( \"flash/player_mp3_1.0.0.swf\", This example uses a free player \"music-player\", \"200\", \"20\", \"8.0.0\", from flash-mp3-player.net flashvars, params);</script> which is embedded in the page </head> using the SWFObject technique <body> we met on pages 208-208. The <div id=\"music-player\"> player is told the path to the MP3 <p>You cannot hear this track because this file using a parameter called mp3. browser does not support our Flash music player.</p> After the second <script> tag, </div> you can see that we have created </body> two JavaScript variables; the first </html> called flashvars, the second called params. Even though we R e s u lt are not using the flashvars variable, the SWFObject script expects it before the params variable so we need it there. var flashvars = {}; var params = { mp3: \"music/noise.mp3\"}; These variables are then added at the end of the line that embeds the MP3 player in the page (just before the second closing <script> tag). FLASH, VIDEO & AUDIO 218
HTML5: Adding HTML5 Audio to Your Pages <audio> chapter-09/adding-html5-audio.html HTML HTML5 introduced the <audio> <!DOCTYPE html> element to include audio files <html> in your pages. As with HTML5 <head> video, browsers expect different <title>Adding HTML5 Audio</title> formats for the audio. </head> <body> The <audio> element has a <audio src=\"audio/test-audio.ogg\" number of attributes which allow controls autoplay> you to control audio playback: <p>This browser does not support our audio format.</p> src </audio> </body> This attribute specifies the path </html> to the audio file. R e s u lt controls preload This example only works in This attribute indicates whether browsers that support the Ogg the player should display This attribute indicates what the Vorbis audio format (Firefox, controls. If you do not use this browser should do if the player is Chrome, and Opera). For it to attribute, no controls will be not set to autoplay. It can have work in Safari 5 and IE 9, the shown by default. You can also the same values we saw on page audio would need to be in MP3 specify your own controls using 214 for the <video> element. format (or use the <source> JavaScript. element covered on the next loop page to offer different formats). autoplay This attribute specifies that the The presence of this attribute audio track should play again indicates that the audio should once it has finished. start playing automatically. (It is considered better practice to let visitors choose to play audio.) 219 FLASH, VIDEO & AUDIO
HTML5: MultiplAerAtuicdlioe Sources HTML chapter-09/multiple-audio-sources.html <source> <!DOCTYPE html> It is possible to specify more <html> than one audio file using the <head> <source> element between the <title>Multiple Audio Sources</title> opening <audio> and closing </head> </audio> tags (instead of the <body> src attribute on the opening <audio controls autoplay> <audio> tag). <source src=\"audio/test-audio.ogg\" /> <source src=\"audio/test-audio.mp3\" /> This is important because <p>This browser does not support our audio different browsers support format.</p> different formats for audio files. </audio> </body> MP3: Safari 5+, Chrome 6+, IE9 </html> Ogg Vorbis: Firefox 3.6, Chome R e s u lt 6, Opera 1.5, IE9 src type So you would need to supply two audio formats to get coverage The <source> element uses the At the time of writing, the type across all recent browsers that src attribute to indicate where attribute was not commonly support the <audio> element. the audio file is located. being used on the <source> You could also provide a Flash element in the same way it was alternative for older browsers for the <video> element. that do not support the <audio> element. The HTML5 <audio> tag has not gained as widespread adoption as the <video> tag, and there have been some issues with audio quality in the first browsers to implement it. FLASH, VIDEO & AUDIO 220
221 FLASH, VIDEO & AUDIO
Example FLASH, VIDEO & AUDIO This example uses HTML5 to show a video. The video has been encoded in H264 and WebM formats to reach as many browsers as possible. A Flash player has been added to the page for browsers that do not support HTML5 video. The Flash player is embedded using SWFObject. If the browser does not support HTML5 video or Flash, then a plain text message will be shown to the user. <!DOCTYPE html> <html> <head> <title>Flash, Video and Audio</title> <script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/ swfobject/2.2/swfobject.js\"></script> <script type=\"text/javascript\"> var flashvars = {}; var params = {movie: \"../video/puppy.flv\"}; swfobject.embedSWF(\"flash/osplayer.swf\", \"snow\", \"400\", \"320\", \"8.0.0\", flashvars, params);</script> </head> <body> <video poster=\"images/puppy.jpg\" width=\"400\" height=\"320\" controls=\"controls\"> <source src=\"video/puppy.mp4\" type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"' /> <source src=\"video/puppy.webm\" type='video/webm; codecs=\"vp8, vorbis\"' /> <div id=\"snow\"> <p>You cannot see this video of a puppy playing in the snow because this browser does not support our video formats.</p> </div> </video> </body> </html> FLASH, VIDEO & AUDIO 222
Summary FLASH, VIDEO & AUDIO XX Flash allows you to add animations, video and audio to the web. XX Flash is not supported on iPhone or iPad. XX HTML5 introduces new <video> and <audio> elements for adding video and audio to web pages, but these are only supported in the latest browsers. XX Browsers that support the HTML5 elements do not all support the same video and audio formats, so you need to supply your files in different formats to ensure that everyone can see/hear them.
10 Introducing CSS XX What CSS does XX How CSS works XX Rules, properties, and values
In this section, we will look at how to make your web pages more attractive, controlling the design of them using CSS. CSS allows you to create rules that specify how the content of an element should appear. For example, you can specify that the background of the page is cream, all paragraphs should appear in gray using the Arial typeface, or that all level one headings should be in a blue, italic, Times typeface. Once you have learned how to write a CSS rule, learning CSS mostly involves learning the different properties you can use. So this chapter will: ●● Introduce you to how CSS works ●● Teach you how to write CSS rules ●● Show you how CSS rules apply to HTML pages The remaining chapters in this section will look at all of the various CSS properties you can use. 227 INTRODUCING CSS
INTRODUCING CSS 228
Understanding CSS: Thinking Inside the Box The key to understanding how CSS works is to imagine that there is an invisible box around every HTML element. On this page, you can see a basic HTML page. On the right hand page, you can see the same HTML page, but I have added outlines to each of the elements so that you can see how CSS will treat each element as if it lives inside its own box. BLOCK & INLINE ELEMENTS You may remember from Block level elements look Inline elements flow within the pages 185-186 that in there is a like they start on a new line. text and do not start on a new difference between block level Examples include the <h1>- line. Examples include <b>, <i>, and inline elements and how <h6>, <p> and <div> elements. <img>, <em> and <span>. how browsers display them. 229 INTRODUCING CSS
CSS allows you to create rules that control the way that each individual box (and the contents of that box) is presented. In this example, block level elements are shown with red borders, and inline elements have green borders. The <body> element creates the first box, then the <h1>, <h2>, <p>, <i>, and <a> elements each create their own boxes within it. Using CSS, you could add a border around any of the boxes, specify its width and height, or add a background color. You could also control text inside a box — for example, its color, size, and the typeface used. Example Styles Text Specific There are also specific ways Boxes Typeface in which you can style certain Size elements such as lists, tables, Width and height Color and forms. Borders (color, width, and style) Italics, bold, uppercase, Background color and images lowercase, small-caps INTRODUCING CSS 230 Position in the browser window.
CSS Associates Style rules with HTML elements CSS works by associating rules with HTML elements. These rules govern how the content of specified elements should be displayed. A CSS rule contains two parts: a selector and a declaration. Selector p{ font-family: Arial;} Declaration This rule indicates that all <p> Selectors indicate which Declarations indicate how elements should be shown in the element the rule applies to. the elements referred to in The same rule can apply to the selector should be styled. Arial typeface. more than one element if you Declarations are split into two separate the element names parts (a property and a value), with commas. and are separated by a colon. 231 INTRODUCING CSS
CSS Properties Affect How Elements Are Displayed CSS declarations sit inside curly brackets and each is made up of two parts: a property and a value, separated by a colon. You can specify several properties in one declaration, each separated by a semi-colon. h1, h2, h3 { font-family: Arial; color: yellow;} Property Value This rule indicates that all <h1>, Properties indicate the aspects Values specify the settings <h2> and <h3> elements of the element you want to you want to use for the chosen should be shown in the Arial change. For example, color, font, properties. For example, if you width, height and border. want to specify a color property typeface, in a yellow color. then the value is the color you want the text in these elements to be. INTRODUCING CSS 232
233 INTRODUCING CSS
Example INTRODUCING CSS Here you can see a simple web page that is styled using CSS. This example uses two documents: the HTML file (example.html) and a separate CSS file (example.css). The fifth line of HTML uses the <link> element to indicate where the CSS file is located. On the next page, you will see how CSS rules can also be placed in your HTML pages and we will discuss when you might want to do this. <!DOCTYPE html> <html> <head> <title>Introducing CSS</title> <link href=\"css/example.css\" type=\"text/css\" rel=\"stylesheet\" /> </head> <body> <h1>From Garden to Plate</h1> <p>A <i>potager</i> is a French term for an ornamental vegetable or kitchen garden ... </p> <h2>What to Plant</h2> <p>Plants are chosen as much for their functionality as for their color and form ... </p> </body> </html> body { font-family: Arial, Verdana, sans-serif;} h1, h2 { color: #ee3e80;} p{ color: #665544;} INTRODUCING CSS 234
Using External CSS <link> chapter-10/using-external-css.html HTML The <link> element can be used <!DOCTYPE html> in an HTML document to tell the <html> browser where to find the CSS <head> file used to style the page. It is an <title>Using External CSS</title> empty element (meaning it does <link href=\"css/styles.css\" type=\"text/css\" not need a closing tag), and it rel=\"stylesheet\" /> lives inside the <head> element. </head> It should use three attributes: <body> <h1>Potatoes</h1> href <p>There are dozens of different potato varieties. They are usually described as This specifies the path to the early, second early and maincrop.</p> CSS file (which is often placed in </body> a folder called css or styles). </html> type chapter-10/styles.css C SS This attribute specifies the type body { of document being linked to. The font-family: arial; value should be text/css. background-color: rgb(185,179,175);} h1 { rel color: rgb(255,255,255);} This specifies the relationship R e s u lt between the HTML page and the file it is linked to. The value should be stylesheet when linking to a CSS file. An HTML page can use more than one CSS style sheet. To do this it could have a <link> element for every CSS file it uses. For example, some authors use one CSS file to control the presentation (such as fonts and colors) and a second to control the layout. 235 INTRODUCING CSS
Using InternAarltiCclSeS H T M L + C SS chapter-10/using-internal-css.html <style> <!DOCTYPE html> You can also include CSS rules <html> within an HTML page by placing <head> them inside a <style> element, <title>Using Internal CSS</title> which usually sits inside the <style type=\"text/css\"> <head> element of the page. body { font-family: arial; The <style> element should use background-color: rgb(185,179,175);} the type attribute to indicate h1 { that the styles are specified in color: rgb(255,255,255);} CSS. The value should be text/ </style> css. </head> <body> When building a site with more <h1>Potatoes</h1> than one page, you should use <p>There are dozens of different potato an external CSS style sheet. This: varieties. They are usually described as early, second early and maincrop.</p> ●● Allows all pages to use the </body> same style rules (rather than </html> repeating them in each page). R e s u lt ●● Keeps the content separate from how the page looks. ●● Means you can change the styles used across all pages by altering just one file (rather than each individual page). In HTML 4 and Transitional that appeared within the value because you may see it used in XHTML, you could also use of the attribute would only apply older code. Here is an example a style attribute on most of to that one element. You should that changes the color of the text the elements that appear in the avoid using this attribute in any in a single paragraph red: body of a page. The CSS rules new site but I mention it here <p style=\"color:red;\"> INTRODUCING CSS 236
CSS Selectors There are many different types chapter-10/css-selectors.html HTML of CSS selector that allow you to target rules to specific elements <!DOCTYPE html> in an HTML document. <html> <head> The table on the opposite page <title>CSS Selectors</title> introduces the most commonly </head> used CSS selectors. <body> <h1 id=\"top\">Kitchen Garden Calendar</h1> On this page, there is an HTML <p id=\"introduction\">Here you can read our file to demonstrate which handy guide about what to do when.</p> elements these CSS selectors <h2>Spring</h2> would apply to. <ul> <li><a href=\"mulch.html\"> CSS selectors are case sensitive, Spring mulch vegetable beds</a></li> so they must match element <li><a href=\"potato.html\"> names and attribute values Plant out early potatoes</a></li> exactly. <li><a href=\"tomato.html\"> Sow tomato seeds</a></li> There are some more advanced <li><a href=\"beet.html\"> selectors which allow you Sow beet seeds</a></li> to select elements based on <li><a href=\"zucchini.html\"> attributes and their values, Sow zucchini seeds</a></li> which you will see on page 292. <li><a href=\"rhubarb.html\"> Deadhead rhubarb flowers</a></li> IE 7 was the first version of IE to </ul> support the last two selectors in <p class=\"note\"> the table (the sibling selectors), This page was written by so their use is less common than <a href=\"mailto:[email protected]\"> the other selectors shown here. [email protected]</a> for <a href=\"http://www.example.org\">Example</a>. </p> <p> <a href=\"#top\">Top of page</a> </p> </body> </html> 237 INTRODUCING CSS
Selector Meaning Example Universal Selector Applies to all elements in the Type Selector document * {} Class Selector Matches element names Targets all elements on the page ID Selector Matches an element whose h1, h2, h3 {} class attribute has a value that Targets the <h1>, <h2> and <h3> Child Selector matches the one specified after elements the period (or full stop) symbol Descendant Selector .note {} Matches an element whose Targets any element whose class Adjacent Sibling id attribute has a value that attribute has a value of note Selector matches the one specified after p.note {} General Sibling the pound or hash symbol Targets only <p> elements Selector Matches an element that is a whose class attribute has a direct child of another value of note Matches an element that is a #introduction {} descendent of another specified Targets the element whose element (not just a direct child of id attribute has a value of that element) introduction Matches an element that is the li>a {} next sibling of another Targets any <a> elements that are children of an <li> element Matches an element that is a (but not other <a> elements in sibling of another, although it the page) does not have to be the directly preceding element p a {} Targets any <a> elements that sit inside a <p> element, even if there are other elements nested between them h1+p {} Targets the first <p> element after any <h1> element (but not other <p> elements) h1~p {} If you had two <p> elements that are siblings of an <h1> element, this rule would apply to both INTRODUCING CSS 238
How Css Rules Cascade If there are two or more rules chapter-10/cascade.html HTML that apply to the same element, it is important to understand <h1>Potatoes</h1> which will take precedence. <p id=\"intro\">There are <i>dozens</i> of different <b>potato</b> varieties.</p> LAST RULE <p>They are usually described as early, second early If the two selectors are identical, and maincrop potatoes.</p> the latter of the two will take precedence. Here you can see C SS the second i selector takes precedence over the first. *{ font-family: Arial, Verdana, sans-serif;} SPECIFICITY h1 { If one selector is more specific font-family: \"Courier New\", monospace;} than the others, the more i{ specific rule will take precedence color: green;} over more general ones. In this i{ example: color: red;} b{ h1 is more specific than * color: pink;} p b is more specific than p pb{ p#intro is more specific than p color: blue !important;} pb{ IMPORTANT color: violet;} You can add !important after p#intro { any property value to indicate font-size: 100%;} that it should be considered p{ more important than other rules font-size: 75%;} that apply to the same element. R e s u lt Understanding how CSS rules cascade means you can write simpler style sheets because you can create generic rules that apply to most elements and then override the properties on individual elements that need to appear differently. 239 INTRODUCING CSS
InherAirtatinccle HTML chapter-10/inheritance.html If you specify the font-family or color properties on the <div class=\"page\"> <body> element, they will apply <h1>Potatoes</h1> to most child elements. This is <p>There are dozens of different potato because the value of the varieties.</p> font-family property is <p>They are usually described as early, second inherited by child elements. It early and maincrop potatoes.</p> saves you from having to apply </div> these properties to as many elements (and results in simpler C SS style sheets). body { You can compare this with font-family: Arial, Verdana, sans-serif; the background-color or color: #665544; border properties; they are not padding: 10px;} inherited by child elements. If .page { these were inherited by all child border: 1px solid #665544; elements then the page could background-color: #efefef; look quite messy. padding: inherit;} You can force a lot of properties R e s u lt to inherit values from their parent elements by using inherit for the value of the properties. In this example, the <div> element with a class called page inherits the padding size from the CSS rule that applies to the <body> element. INTRODUCING CSS 240
Why use External Style Sheets? When building a website there are several advantages to placing your CSS rules in a separate style sheet. All of your web pages can share Therefore, once the user has the one CSS style sheet, rather the same style sheet. This is downloaded the CSS stylesheet, than changing the CSS rules on achieved by using the <link> the rest of the site will load every page. The HTML code element on each HTML page of faster. If you want to make a will be easier to read and edit your site to link to the same CSS change to how your site appears, because it does not have lots of document. This means that the you only need to edit the one CSS rules in the same document. same code does not need to be CSS file and all of your pages It is generally considered good repeated in every page (which will be updated. For example, practice to have the content of results in less code and smaller you can change the style of the site separated from the rules HTML pages). every <h1> element by altering that determine how it appears. Sometimes you might consider placing CSS rules in the same page as your HTML code. If you are just creating a single If you have one page which Most of the examples in this page, you might decide to put requires a few extra rules (that book place the CSS rules in the the rules in the same file to are not used by the rest of the <head> of the document (using keep everything in one place. site), you might consider using the <style> element) rather (However, many authors would CSS in the same page. (Again, than a separate document. This consider it better practice to most authors consider it better is simply to save you opening keep the CSS in a separate file.) practice to keep all CSS rules in a two files to see how the CSS separate file.) examples work. 241 INTRODUCING CSS
Different versions of CSS & Browser Quirks CSS1 was released in 1996 and CSS2 followed two years later. Work on CSS3 has been ongoing but the major browsers have already started to implement it. In the same way that there have Browsers did not implement all This is mentioned when it is been several versions of HTML, CSS features at once, so some likely to affect you, along with there have also been different older browsers do not support notes where CSS properties versions of CSS. every property. might not behave as expected. Any seasoned user of CSS will tell you that some browsers display a few of the CSS properties in an unexpected way. But finding and squashing those bugs is easy when you know how... Before launching any new site, Using these tools, it is a good Some common browser bugs are it is important to test it in more idea to check the site on discussed in this book, but there than one browser, because there different operating systems (PC, are many smaller bugs that only can be slight differences in how Mac, and Linux) and in older occur in rare situations, or on old browsers display the pages. versions of the major browsers, browsers that few people use. as well as recent versions. You do not need lots of If you come across a CSS bug, computers to test your site, as When you look at your site in you can use your favorite search there are online tools to show more than one browser, you engine to try and find a solution. you what a page looks like in might find that some elements Or you can check these sites: multiple browsers: on your page do not look as you expect them to. PositionIsEverything.net BrowserCam.com QuirksMode.org BrowserLab.Adobe.com When a CSS property does BrowserShots.org not display as expected, it INTRODUCING CSS 242 CrossBrowserTesting.com is generally referred to as a browser quirk or CSS bug.
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 1 - 50
- 51 - 100
- 101 - 150
- 151 - 200
- 201 - 250
- 251 - 300
- 301 - 350
- 351 - 400
- 401 - 450
- 451 - 500
- 501 - 514
Pages: