Skip to main content

How to Calculate HTML5 Video Height and Width dynamically to fit web page.



While developing the web pages with inline HTML5 videos we often need to adjust the video width or height according the available space on the web page. 

Here is the solution to find that required height and width keeping the same aspect ratio of the video. 

You may go ahead and set video CSS property to width: 100% and height: auto but this will not give you any idea about how much height the video is going to take on the web page. Then the better approach would be setting the video height or width using Javascript.

So, sometimes to adjust the width according to available height on the web page, it require to calculate the NEW WIDTH which you can set to your video as per available height. The same goes with Height as well. So here are the solutions to solve this problem.

Here I am assuming the video is having 16:9 ratio, which would be a HD video with 1920px width and 1080px height.

Now suppose I want my video to fit in the width of 1280px, then how much height it will this video required to fit with same aspect ratio?

You can calculate it using below formula:

New Video Height = (Current Height / Current Width ) * My Required Width

So Here is the calculation.

New Video Height  = (1080 / 1920) * 1280

And this shows that if you want to set 1280px width to the video, it will take 720px of height on your web page.

Now, how will you calculate the required width if you want your video to fit with  height of 657px.

You can calculate the required new width using below formula:

New Width = (Current Width / Current Height) * My Required Height

So Here is the calculation.

New Width = (1920 / 1080) * 657

So, if you want to set 657px height to the video, it will take 1168px of width on your web page. 
Refer the below table to understand about this calculations in short.





Tags: how to set height and width of video in htmlhtml5 video scale to fit divdynamically adjust video height and width using javascript

Comments

Popular posts from this blog

Most importment JavaScript frameworks & libraries for frontend development

Here is a list of top Most importment JavaScript frameworks & libraries for frontend development . 1. Jquery You can directly use jQuery library if your requirement is to deal with browser's DOM. jQuery has been a reliable, easy to use and handy library for frontend developers. You can trust on it's cross browser & legacy support. Along with simple jQuery.js, adding the jQuery UI provides you more features for adding different kind of interactivity on your webpage. 2. Angular Since 2016, Angular is becoming a most popular framework for clien-side development. Angular.js is the initial base of Angular (released in 2010) which was completely written in JavaScript. In 2016, Google released the next version of Angular.js as Angular 2.0 (not .js). Angular 2.0 and above versions uses Typescript as a language to write code. Angular added a new way to develop client-side SPA applications. If you want to build a Single Page Application, if you want a most secured application...

Search using Javascript: Part - 1 Searching the text from html table

Hi all, Here is my first programming stuff. This demo is about implementing search operation on your webpage. Here is the source code: HTML and JavaScript: <!DOCTYPE html> <html> <head> <title>Searching the text html table </title> <style>  table{border: 1px solid #000 }  td{border: 1px solid #000; font-size: 20px; padding: 10px; }  tr:nth-of-type(even){background-color: yellow; } </style> </head> <body> <h3> Table</h3><p></p> <table class="tabl" name="tabName1" id="tabId1"> <tr>  <th class="tdClassname"> Name </th> <th class="tdClassname"> Acc No </th> <th class="tdClassname"> Branch No </th> <th class="tdClassname"> Tel </th> </tr> <tr>  <td class="tdClassname"> Name 11 </td> <td ...