How to make image or video fill entire container when dealing with sub-pixel rendering?How do I auto-resize an image to fit a 'div' container?Make the cursor a hand when a user hovers over a list itemHow to fill video element with poster image even if the poster image is a different aspect ratio than its video element?Video JS - poster image cover/containStretch CSS Background ImageCSS / Jquery How to auto-size containers and imagesMake a Video Match the size of its Parent Container (which uses vh and vw units)Showing the poster image when video is paused. How to make it smoother?Full width (fixed height) responsive youtube embed bannerMake video responsive when browser resizing

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

What is the unit of time_lock_delta in LND?

What to do with someone that cheated their way through university and a PhD program?

Contradiction proof for inequality of P and NP?

Cayley's Matrix Notation

Would the change in enthalpy (ΔH) for the dissolution of urea in water be positive or negative?

Who's the random kid standing in the gathering at the end?

How can I get rid of an unhelpful parallel branch when unpivoting a single row?

Do I need to watch Ant-Man and the Wasp and Captain Marvel before watching Avengers: Endgame?

Retract an already submitted recommendation letter (written for an undergrad student)

Multiple fireplaces in an apartment building?

Von Neumann Extractor - Which bit is retained?

How bug prioritization works in agile projects vs non agile

Can a Bard use the Spell Glyph option of the Glyph of Warding spell and cast a known spell into the glyph?

Island of Knights, Knaves and Spies

How to have a sharp product image?

Is Electric Central Heating worth it if using Solar Panels?

How much of a wave function must reside inside event horizon for it to be consumed by the black hole?

Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?

Is it acceptable to use working hours to read general interest books?

Check if a string is entirely made of the same substring

Creating a chemical industry from a medieval tech level without petroleum

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

Negative Resistance



How to make image or video fill entire container when dealing with sub-pixel rendering?


How do I auto-resize an image to fit a 'div' container?Make the cursor a hand when a user hovers over a list itemHow to fill video element with poster image even if the poster image is a different aspect ratio than its video element?Video JS - poster image cover/containStretch CSS Background ImageCSS / Jquery How to auto-size containers and imagesMake a Video Match the size of its Parent Container (which uses vh and vw units)Showing the poster image when video is paused. How to make it smoother?Full width (fixed height) responsive youtube embed bannerMake video responsive when browser resizing






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















It appears that some browsers, when rendering image or video media that is positioned and sized on the page using percentages, occasionally round the size of the media content (image or video) differently than they round the actual DOM element that contains them. We're not sure how to fix this. There are two variants: HTML5 video element, and div with a background image (set to "contain").



My testing is on Chrome 64.0 on macOS Sierra, but we've had the same issue reported on Chrome on Windows. I've not yet tested other browsers.



Video Element



Here's an example of a very minimal test case that shows the issue. Resize the width of your browser until you see a red line beside the video, or the video overflowing its element (as shown in the screenshots below).






video 
background-color: red;
width: 32%;
margin: 0 auto;
display: block;

<video controls src="http://vjs.zencdn.net/v/oceans.mp4">
</video>





Or, as a codepen: https://codepen.io/jthomerson/pen/mXrpZr



Here are two variations of the error:



video renders slightly smaller than video elementvideo renders slightly larger than video element



Is there a way to fix this? JW Player appears to be working around this with some complex JS that's watching for all resize events and then adding dynamic transforms and positioning directly to the video element. Is there a simpler way? Or if that's the only way, is there a library out there where this has been standardized? (We are using VideoJS, but this isn't a problem specific to VideoJS).



Background Image



The same thing can happen with a div that is sized to be the exact same aspect ratio as your background image, and the background image uses no-repeat and contain.






.container 
width: 60%;
border: 1px solid blue;
margin: 0 auto;
padding: 1em;

.someBox
background-color: red;
background-image: url('https://placebear.com/640/360');
background-repeat: no-repeat;
background-size: contain;
margin: 0 auto;
display: block;
padding-top: 56.25%;

<div class="container">
<div class="someBox">
</div>
</div>





Or, as a codepen: https://codepen.io/jthomerson/pen/xYEYZK



Here is an example of the error:



image renders slightly larger than the element it is background for



Of course, in this situation you might say "just change contain to cover, that's what it's made for". That's true, but in our actual usecase we are using a div with a background image as the poster image for a video when the video is not playing. However, we don't know the actual aspect ratio of the image. The video player itself is always sized to be 16:9, but in some cases our clients upload a poster image that is 1:1 (example: music video album art), or 4:3 or 2:1 (for legacy content where they don't have 16:9 content). So, we couldn't change contain to cover unless we were programmatically loading the image, detecting its size, and then if it's actually 16:9, use cover, otherwise use contain.










share|improve this question
























  • another interesting thing is that if you add background-position: center ; this pixel is moved to the left !

    – Temani Afif
    Feb 6 '18 at 22:26











  • and using background-size: 100% 100%; is not a solution ? unlinke cover, it will stretch the image to hide this pixel and not hide a pixel of the image

    – Temani Afif
    Feb 6 '18 at 22:35












  • @TemaniAfif no, that won't work - see the part about why we can't use cover; because sometimes the client has a non-16:9 image in there. (Well, we could do it, but we'd have to detect actual image dimensions before using it as the background, and that's ugly).

    – Jeremy Thomerson
    Feb 7 '18 at 22:11

















3















It appears that some browsers, when rendering image or video media that is positioned and sized on the page using percentages, occasionally round the size of the media content (image or video) differently than they round the actual DOM element that contains them. We're not sure how to fix this. There are two variants: HTML5 video element, and div with a background image (set to "contain").



My testing is on Chrome 64.0 on macOS Sierra, but we've had the same issue reported on Chrome on Windows. I've not yet tested other browsers.



Video Element



Here's an example of a very minimal test case that shows the issue. Resize the width of your browser until you see a red line beside the video, or the video overflowing its element (as shown in the screenshots below).






video 
background-color: red;
width: 32%;
margin: 0 auto;
display: block;

<video controls src="http://vjs.zencdn.net/v/oceans.mp4">
</video>





Or, as a codepen: https://codepen.io/jthomerson/pen/mXrpZr



Here are two variations of the error:



video renders slightly smaller than video elementvideo renders slightly larger than video element



Is there a way to fix this? JW Player appears to be working around this with some complex JS that's watching for all resize events and then adding dynamic transforms and positioning directly to the video element. Is there a simpler way? Or if that's the only way, is there a library out there where this has been standardized? (We are using VideoJS, but this isn't a problem specific to VideoJS).



Background Image



The same thing can happen with a div that is sized to be the exact same aspect ratio as your background image, and the background image uses no-repeat and contain.






.container 
width: 60%;
border: 1px solid blue;
margin: 0 auto;
padding: 1em;

.someBox
background-color: red;
background-image: url('https://placebear.com/640/360');
background-repeat: no-repeat;
background-size: contain;
margin: 0 auto;
display: block;
padding-top: 56.25%;

<div class="container">
<div class="someBox">
</div>
</div>





Or, as a codepen: https://codepen.io/jthomerson/pen/xYEYZK



Here is an example of the error:



image renders slightly larger than the element it is background for



Of course, in this situation you might say "just change contain to cover, that's what it's made for". That's true, but in our actual usecase we are using a div with a background image as the poster image for a video when the video is not playing. However, we don't know the actual aspect ratio of the image. The video player itself is always sized to be 16:9, but in some cases our clients upload a poster image that is 1:1 (example: music video album art), or 4:3 or 2:1 (for legacy content where they don't have 16:9 content). So, we couldn't change contain to cover unless we were programmatically loading the image, detecting its size, and then if it's actually 16:9, use cover, otherwise use contain.










share|improve this question
























  • another interesting thing is that if you add background-position: center ; this pixel is moved to the left !

    – Temani Afif
    Feb 6 '18 at 22:26











  • and using background-size: 100% 100%; is not a solution ? unlinke cover, it will stretch the image to hide this pixel and not hide a pixel of the image

    – Temani Afif
    Feb 6 '18 at 22:35












  • @TemaniAfif no, that won't work - see the part about why we can't use cover; because sometimes the client has a non-16:9 image in there. (Well, we could do it, but we'd have to detect actual image dimensions before using it as the background, and that's ugly).

    – Jeremy Thomerson
    Feb 7 '18 at 22:11













3












3








3








It appears that some browsers, when rendering image or video media that is positioned and sized on the page using percentages, occasionally round the size of the media content (image or video) differently than they round the actual DOM element that contains them. We're not sure how to fix this. There are two variants: HTML5 video element, and div with a background image (set to "contain").



My testing is on Chrome 64.0 on macOS Sierra, but we've had the same issue reported on Chrome on Windows. I've not yet tested other browsers.



Video Element



Here's an example of a very minimal test case that shows the issue. Resize the width of your browser until you see a red line beside the video, or the video overflowing its element (as shown in the screenshots below).






video 
background-color: red;
width: 32%;
margin: 0 auto;
display: block;

<video controls src="http://vjs.zencdn.net/v/oceans.mp4">
</video>





Or, as a codepen: https://codepen.io/jthomerson/pen/mXrpZr



Here are two variations of the error:



video renders slightly smaller than video elementvideo renders slightly larger than video element



Is there a way to fix this? JW Player appears to be working around this with some complex JS that's watching for all resize events and then adding dynamic transforms and positioning directly to the video element. Is there a simpler way? Or if that's the only way, is there a library out there where this has been standardized? (We are using VideoJS, but this isn't a problem specific to VideoJS).



Background Image



The same thing can happen with a div that is sized to be the exact same aspect ratio as your background image, and the background image uses no-repeat and contain.






.container 
width: 60%;
border: 1px solid blue;
margin: 0 auto;
padding: 1em;

.someBox
background-color: red;
background-image: url('https://placebear.com/640/360');
background-repeat: no-repeat;
background-size: contain;
margin: 0 auto;
display: block;
padding-top: 56.25%;

<div class="container">
<div class="someBox">
</div>
</div>





Or, as a codepen: https://codepen.io/jthomerson/pen/xYEYZK



Here is an example of the error:



image renders slightly larger than the element it is background for



Of course, in this situation you might say "just change contain to cover, that's what it's made for". That's true, but in our actual usecase we are using a div with a background image as the poster image for a video when the video is not playing. However, we don't know the actual aspect ratio of the image. The video player itself is always sized to be 16:9, but in some cases our clients upload a poster image that is 1:1 (example: music video album art), or 4:3 or 2:1 (for legacy content where they don't have 16:9 content). So, we couldn't change contain to cover unless we were programmatically loading the image, detecting its size, and then if it's actually 16:9, use cover, otherwise use contain.










share|improve this question
















It appears that some browsers, when rendering image or video media that is positioned and sized on the page using percentages, occasionally round the size of the media content (image or video) differently than they round the actual DOM element that contains them. We're not sure how to fix this. There are two variants: HTML5 video element, and div with a background image (set to "contain").



My testing is on Chrome 64.0 on macOS Sierra, but we've had the same issue reported on Chrome on Windows. I've not yet tested other browsers.



Video Element



Here's an example of a very minimal test case that shows the issue. Resize the width of your browser until you see a red line beside the video, or the video overflowing its element (as shown in the screenshots below).






video 
background-color: red;
width: 32%;
margin: 0 auto;
display: block;

<video controls src="http://vjs.zencdn.net/v/oceans.mp4">
</video>





Or, as a codepen: https://codepen.io/jthomerson/pen/mXrpZr



Here are two variations of the error:



video renders slightly smaller than video elementvideo renders slightly larger than video element



Is there a way to fix this? JW Player appears to be working around this with some complex JS that's watching for all resize events and then adding dynamic transforms and positioning directly to the video element. Is there a simpler way? Or if that's the only way, is there a library out there where this has been standardized? (We are using VideoJS, but this isn't a problem specific to VideoJS).



Background Image



The same thing can happen with a div that is sized to be the exact same aspect ratio as your background image, and the background image uses no-repeat and contain.






.container 
width: 60%;
border: 1px solid blue;
margin: 0 auto;
padding: 1em;

.someBox
background-color: red;
background-image: url('https://placebear.com/640/360');
background-repeat: no-repeat;
background-size: contain;
margin: 0 auto;
display: block;
padding-top: 56.25%;

<div class="container">
<div class="someBox">
</div>
</div>





Or, as a codepen: https://codepen.io/jthomerson/pen/xYEYZK



Here is an example of the error:



image renders slightly larger than the element it is background for



Of course, in this situation you might say "just change contain to cover, that's what it's made for". That's true, but in our actual usecase we are using a div with a background image as the poster image for a video when the video is not playing. However, we don't know the actual aspect ratio of the image. The video player itself is always sized to be 16:9, but in some cases our clients upload a poster image that is 1:1 (example: music video album art), or 4:3 or 2:1 (for legacy content where they don't have 16:9 content). So, we couldn't change contain to cover unless we were programmatically loading the image, detecting its size, and then if it's actually 16:9, use cover, otherwise use contain.






video 
background-color: red;
width: 32%;
margin: 0 auto;
display: block;

<video controls src="http://vjs.zencdn.net/v/oceans.mp4">
</video>





video 
background-color: red;
width: 32%;
margin: 0 auto;
display: block;

<video controls src="http://vjs.zencdn.net/v/oceans.mp4">
</video>





.container 
width: 60%;
border: 1px solid blue;
margin: 0 auto;
padding: 1em;

.someBox
background-color: red;
background-image: url('https://placebear.com/640/360');
background-repeat: no-repeat;
background-size: contain;
margin: 0 auto;
display: block;
padding-top: 56.25%;

<div class="container">
<div class="someBox">
</div>
</div>





.container 
width: 60%;
border: 1px solid blue;
margin: 0 auto;
padding: 1em;

.someBox
background-color: red;
background-image: url('https://placebear.com/640/360');
background-repeat: no-repeat;
background-size: contain;
margin: 0 auto;
display: block;
padding-top: 56.25%;

<div class="container">
<div class="someBox">
</div>
</div>






javascript css video html5-video subpixel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 6 '18 at 21:45







Jeremy Thomerson

















asked Feb 6 '18 at 21:38









Jeremy ThomersonJeremy Thomerson

19710




19710












  • another interesting thing is that if you add background-position: center ; this pixel is moved to the left !

    – Temani Afif
    Feb 6 '18 at 22:26











  • and using background-size: 100% 100%; is not a solution ? unlinke cover, it will stretch the image to hide this pixel and not hide a pixel of the image

    – Temani Afif
    Feb 6 '18 at 22:35












  • @TemaniAfif no, that won't work - see the part about why we can't use cover; because sometimes the client has a non-16:9 image in there. (Well, we could do it, but we'd have to detect actual image dimensions before using it as the background, and that's ugly).

    – Jeremy Thomerson
    Feb 7 '18 at 22:11

















  • another interesting thing is that if you add background-position: center ; this pixel is moved to the left !

    – Temani Afif
    Feb 6 '18 at 22:26











  • and using background-size: 100% 100%; is not a solution ? unlinke cover, it will stretch the image to hide this pixel and not hide a pixel of the image

    – Temani Afif
    Feb 6 '18 at 22:35












  • @TemaniAfif no, that won't work - see the part about why we can't use cover; because sometimes the client has a non-16:9 image in there. (Well, we could do it, but we'd have to detect actual image dimensions before using it as the background, and that's ugly).

    – Jeremy Thomerson
    Feb 7 '18 at 22:11
















another interesting thing is that if you add background-position: center ; this pixel is moved to the left !

– Temani Afif
Feb 6 '18 at 22:26





another interesting thing is that if you add background-position: center ; this pixel is moved to the left !

– Temani Afif
Feb 6 '18 at 22:26













and using background-size: 100% 100%; is not a solution ? unlinke cover, it will stretch the image to hide this pixel and not hide a pixel of the image

– Temani Afif
Feb 6 '18 at 22:35






and using background-size: 100% 100%; is not a solution ? unlinke cover, it will stretch the image to hide this pixel and not hide a pixel of the image

– Temani Afif
Feb 6 '18 at 22:35














@TemaniAfif no, that won't work - see the part about why we can't use cover; because sometimes the client has a non-16:9 image in there. (Well, we could do it, but we'd have to detect actual image dimensions before using it as the background, and that's ugly).

– Jeremy Thomerson
Feb 7 '18 at 22:11





@TemaniAfif no, that won't work - see the part about why we can't use cover; because sometimes the client has a non-16:9 image in there. (Well, we could do it, but we'd have to detect actual image dimensions before using it as the background, and that's ugly).

– Jeremy Thomerson
Feb 7 '18 at 22:11












1 Answer
1






active

oldest

votes


















0














A quick "hacky" fix that worked for me in some situations was to add a simple 1px transparent border around the parent element of the cover image/video.






border: 1px solid transparent;








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48652383%2fhow-to-make-image-or-video-fill-entire-container-when-dealing-with-sub-pixel-ren%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    A quick "hacky" fix that worked for me in some situations was to add a simple 1px transparent border around the parent element of the cover image/video.






    border: 1px solid transparent;








    share|improve this answer



























      0














      A quick "hacky" fix that worked for me in some situations was to add a simple 1px transparent border around the parent element of the cover image/video.






      border: 1px solid transparent;








      share|improve this answer

























        0












        0








        0







        A quick "hacky" fix that worked for me in some situations was to add a simple 1px transparent border around the parent element of the cover image/video.






        border: 1px solid transparent;








        share|improve this answer













        A quick "hacky" fix that worked for me in some situations was to add a simple 1px transparent border around the parent element of the cover image/video.






        border: 1px solid transparent;








        border: 1px solid transparent;





        border: 1px solid transparent;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 16:30









        Adrian PuescuAdrian Puescu

        12611




        12611





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48652383%2fhow-to-make-image-or-video-fill-entire-container-when-dealing-with-sub-pixel-ren%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript