Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8244215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:46:33+00:00 2026-06-07T21:46:33+00:00

How can I simulate the functionality of background-size:cover on an html element like <video>

  • 0

How can I simulate the functionality of background-size:cover on an html element like <video> or <img>?

I’d like it to work like

background-size: cover;
background-position: center center;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:46:35+00:00Added an answer on June 7, 2026 at 9:46 pm

    Here’s how I did this. A working example is in this jsFiddle.

    var min_w = 300; // minimum video width allowed
    var vid_w_orig;  // original video dimensions
    var vid_h_orig;
    
    jQuery(function() { // runs after DOM has loaded
    
      vid_w_orig = parseInt(jQuery('video').attr('width'));
      vid_h_orig = parseInt(jQuery('video').attr('height'));
      $('#debug').append("<p>DOM loaded</p>");
    
      jQuery(window).resize(function () { resizeToCover(); });
      jQuery(window).trigger('resize');
    });
    
    function resizeToCover() {
      // set the video viewport to the window size
      jQuery('#video-viewport').width(jQuery(window).width());
      jQuery('#video-viewport').height(jQuery(window).height());
    
      // use largest scale factor of horizontal/vertical
      var scale_h = jQuery(window).width() / vid_w_orig;
      var scale_v = jQuery(window).height() / vid_h_orig;
      var scale = scale_h > scale_v ? scale_h : scale_v;
    
      // don't allow scaled width < minimum video width
      if (scale * vid_w_orig < min_w) {scale = min_w / vid_w_orig;};
    
      // now scale the video
      jQuery('video').width(scale * vid_w_orig);
      jQuery('video').height(scale * vid_h_orig);
      // and center it by scrolling the video viewport
      jQuery('#video-viewport').scrollLeft((jQuery('video').width() - jQuery(window).width()) / 2);
      jQuery('#video-viewport').scrollTop((jQuery('video').height() - jQuery(window).height()) / 2);
    
      // debug output
      jQuery('#debug').html("<p>win_w: " + jQuery(window).width() + "</p>");
      jQuery('#debug').append("<p>win_h: " + jQuery(window).height() + "</p>");
      jQuery('#debug').append("<p>viewport_w: " + jQuery('#video-viewport').width() + "</p>");
      jQuery('#debug').append("<p>viewport_h: " + jQuery('#video-viewport').height() + "</p>");
      jQuery('#debug').append("<p>video_w: " + jQuery('video').width() + "</p>");
      jQuery('#debug').append("<p>video_h: " + jQuery('video').height() + "</p>");
      jQuery('#debug').append("<p>vid_w_orig: " + vid_w_orig + "</p>");
      jQuery('#debug').append("<p>vid_h_orig: " + vid_h_orig + "</p>");
      jQuery('#debug').append("<p>scale: " + scale + "</p>");
    };
    #video-viewport {
      position: absolute;
      top: 0;
      overflow: hidden;
      z-index: -1; /* for accessing the video by click */
    }
    
    #debug {
      position: absolute;
      top: 0;
      z-index: 100;
      color: #fff;
      font-size: 12pt;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="video-viewport">
      <video autoplay controls preload width="640" height="360">
        <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"type="video/mp4" />
        <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"type="video/webm" />
        <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"type="video/webm" />
      </video>
    </div>
    
    <div id="debug"></div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we simulate calls and SMS in android programmtically? Like in Windows mobile we
We would like to simulate Imagemagick functionality, but only in Javascript. Do any libraries
Is there a way by which we can simulate thread level constants in C++?
Does anyone know of a class/library/etc. that can simulate 32 bit unsigned integers on
Can I simulate in C#/C++ code Control + Alt + Delete sequence in Vista?
How can I simulate the behavior of a stack or a queue in VHDL?
How can I simulate an incoming call from a private number -- in an
Lets say I want to simulate a particle state, which can be normal (0)
Can this be done? It seems like this should be possible. In general, I
I know i can simulate a memory warning on the simulator by selecting 'Simulate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.