Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Setting the video width and height

From: HTML5: Video and Audio in Depth

Video: Setting the video width and height

A couple of important attributes to include in your video tag are the width and height attributes. If you look at this demo page I have here, the first line of examples is what happens when you don't include a width and height. With the very first one, we're not preloading the video and we haven't provided a poster image, so the browser has no information to base the video size on and it picks an arbitrary size. I've added a gray border to all the videos. So you can see the actual space provided for it. In the video to the right, we're preloading the video, so the browser sets the video area to the exact size of the video. In the last video on that line, we've provided a poster image and the browser is sizing the video area to the exact size of the poster image.

Setting the video width and height

A couple of important attributes to include in your video tag are the width and height attributes. If you look at this demo page I have here, the first line of examples is what happens when you don't include a width and height. With the very first one, we're not preloading the video and we haven't provided a poster image, so the browser has no information to base the video size on and it picks an arbitrary size. I've added a gray border to all the videos. So you can see the actual space provided for it. In the video to the right, we're preloading the video, so the browser sets the video area to the exact size of the video. In the last video on that line, we've provided a poster image and the browser is sizing the video area to the exact size of the poster image.

In the next line, we've set a width and height, but it's not the same dimensions of the video. It's too tall for the aspect ratio. In the second video, the browser is adding space above and below the video. Also notice there is a letterbox so that the video can play in the space without being stretched. In the third video we provided a poster image and Firefox is stretching the poster to fill the space. If we look at another browser, and I'll just go over to Safari, we can see that it doesn't stretch the poster like Firefox does.

So that's a specific issue for Firefox. In the third line, we have the same thing, except the video area is too wide for the video. So the browser is adding space to the size of it, also known as a pillarbox. Finally, in our last line, we have the correct width and height and everything looks as expected. Finally, in the last line we have the correct width and height and everything looks as expected. So the goal is to get the video area the same width and height as the video itself. In addition to avoiding extra space around the video, it keeps the browser from having to do extra processing to scale the video up and down.

But sometimes you can't know the video size ahead of time, so in that case it might be best putting a black background on the video tag, like I've done in this last example, so that when you play the video it's not floating in space. (Music playing) So really quick let's go to our code and add the width and height to our video tag. So if you're following along we're now in the 2-07 folder of the exercise files and I'm going to add our width and height just right after the video tag name. I know my video has a width of 480 pixels and a height of 300 pixels. And I can save that.

If we go back to Firefox and go to our podcast page, reload it, and that's how you do it and is pretty easy. It doesn't change much in the case of the podcast since the browser was already sizing the frame based on the preloaded video, but we're reinforcing the width and height in case we ever don't preload it or don't have a poster. All right, now audio on the other hand has no width and height attributes. So if you add them, they won't do anything. However, you can still resize the audio player using CSS and I actually want our audio player to be of the same width as our video player.

So I'm going to go and do that. Go back to our code. Normally it would be best to do this in a separate style sheet. For the sake of this example though, I'm just going to do an in-line style and I'm going to set our width to 480 pixels, the same size as our video in our video player. Okay, so now if we save that, we'll go back to our browser and reload it, we should see that now our audio player has the same width as our video. Okay, so that's width and height for video and audio.

For video you can use the width and height attributes of the video tag; for audio you have to use styles if you want to change anything. For video I'd recommend using width and height every time so that the browser can know how big the video frame should be ahead of time.

Show transcript

This video is part of

Image for HTML5: Video and Audio in Depth
HTML5: Video and Audio in Depth

37 video lessons · 14657 viewers

Steve Heffernan
Author

 
Expand all | Collapse all
  1. 1m 24s
    1. Welcome
      42s
    2. Using the exercise files
      42s
  2. 15m 18s
    1. Understanding HTML5 video and audio
      4m 46s
    2. The state of HTML5 video and audio
      4m 29s
    3. Understanding the format war
      3m 53s
    4. Configuring your server to deliver HTML5 media formats
      2m 10s
  3. 32m 36s
    1. Using the video and audio tags
      5m 34s
    2. Using multiple sources for browser compatibility
      5m 53s
    3. Preloading
      4m 16s
    4. Autoplaying
      2m 2s
    5. Looping
      5m 11s
    6. Setting a poster frame
      3m 43s
    7. Setting the video width and height
      3m 40s
    8. Displaying subtitles and captions
      2m 17s
  4. 12m 40s
    1. Falling back to Flash
      4m 4s
    2. Falling back to download links
      6m 21s
    3. Using an embed code builder
      2m 15s
  5. 22m 17s
    1. Understanding HTML5 video formats
      5m 10s
    2. Understanding HTML5 audio formats
      2m 57s
    3. Encoding MP4/H.264 video with HandBrake
      3m 13s
    4. Encoding Ogg, Theora, and WebM with Firefogg
      3m 18s
    5. Encoding audio formats with VLC
      5m 54s
    6. Automating video and audio encoding
      1m 45s
  6. 34m 59s
    1. Setting up the JavaScript
      4m 39s
    2. Creating Play and Pause buttons
      7m 52s
    3. Creating a Play Progress bar
      5m 49s
    4. Creating a Load Progress bar
      9m 29s
    5. Creating a Current Time and Duration display
      7m 10s
  7. 4m 37s
    1. Overview of known issues
      35s
    2. Autobuffer or preload?
      56s
    3. Load progress in Firefox 3.6
      41s
    4. Android 2.0 issues
      1m 5s
    5. iOS 3 issues
      1m 20s
  8. 2m 8s
    1. Flight of the Navigator
      50s
    2. ZEN Audio Player by simurai
      34s
    3. The Wilderness Downtown by Google and Arcade Fire
      44s
  9. 1m 40s
    1. Further exploration
      1m 40s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5: Video and Audio in Depth.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.