Setting HTML5 Video to Parent Size

I have a <video> element inside a <div> that automatically changes when other elements on the page are dynamically changed / added / deleted.

I would like the video element to also automatically resize so that it always remains within its background div; this type of work if I set the height and width of the CSS video element to 100%, so it will always be the same size as its container. However, if the contained div dimensions are below the video image inherent to videoWidth or videoHeight, then it starts to behave as if the CSS height and width properties refer to percentages of video images whose dimension does not match the size of the container! For example, if the CSS height is 100%, it scales normally, except that it has a minimum video height size; if the CSS height is 50%, it scales normally, but with a minimum size of 50% of the highest video height.

I can fix this, for example, using JavaScript, so that periodically reset the height of the video element in pixels was the calculated height of the container, but it is very slow and intermittent. Is there a way to fix this in CSS so that the video element is correctly defined?

+4
source share
2 answers

I am well aware that this is an older question, but I was struggling with the layout with CSS, where the video automatically adjusts to some field, usually inside the parent element.

Just using width and height with static positioning only works in a specific configuration of parent-child topologies, and also depends heavily on the style of the topology. Even if you get some element for the correct calculation of its boundaries, as soon as you put a reproducing video element in it, it will expand the field allowed by the parents, although this is the least reasonable behavior that you expect.

Throw a few fieldset elements and you're in the rabbit hole of CSS and browser features.

What I found out is that it is easiest to remove a video element from a positioning context using position: absolute . This does not mean that it will not behave visually well - using width: 100% and height: 100% effectively makes it properly limit itself, as otherwise (but will not). Then you need to add position: relative to the corresponding element of the ancestor of the video element, otherwise the video will be absolutely positioned relative to the document root, which, most likely, will not be what you want.

Omitting left and right works because absolute positioning does not reset the position, it simply switches the calculation method. You can alternatively set both properties to zero, then you will get your video aligned to the upper left corner of the offset. max-width and max-height not needed - I just saw how they are thrown in many cases when people struggle with the restriction of their video elements - do not worry.

You can specify the background color for the video element or its offset parent. Thus, you get the effect of boxing letters - say, black bars on the sides of the video.

+2
source

Since your video is inside a div , this can be solved by setting the width and height of the video to 100% . This makes the video occupy 100% of the div element.

Layout Example:

 <div id="video_container"> <video></video> </div> 

Styles:

 #video_container video { width: 100%; height: 100%; } 
+1
source

Source: https://habr.com/ru/post/1380878/


All Articles