可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a video, and I want it to FILL 100% of the width, and 100% of the height. And keep the aspect ratio.
Is it possible that it at least fills 100% for both? And if a bit of the video has to be out of the screen to keep the aspect ratio, that doesn't matter.
HTML:
<video preload="auto" class="videot" id="videot" height="100%" preload>
<source src="BESTANDEN/video/tible.mp4" type="video/mp4" >
<object data="BESTANDEN/video/tible.mp4" height="1080">
<param name="wmode" value="transparent">
<param name="autoplay" value="false" >
<param name="loop" value="false" >
</object>
CSS:
.videof, .videot {
width: 100% !important;
height: 100% !important;
}
回答1:
You can use Javascript to dynamically set the height to 100% of the window and then center it using a negative left margin based on the ratio of video width to window width.
http://jsfiddle.net/RfV5C/
var $video = $('video'),
$window = $(window);
$(window).resize(function(){
var height = $window.height();
$video.css('height', height);
var videoWidth = $video.width(),
windowWidth = $window.width(),
marginLeftAdjust = (windowWidth - videoWidth) / 2;
$video.css({
'height': height,
'marginLeft' : marginLeftAdjust
});
}).resize();
回答2:
By checking other answers, I used object-fit in CSS:
video {
object-fit: fill;
}
From MDN (https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit):
The object-fit CSS property specifies how the contents of a replaced element should be fitted to the box established by its used height and width.
Value: fill
The replaced content is sized to fill the element’s content box: the object’s concrete object size is the element’s used width and height.
回答3:
video {
width: 100% !important;
height: auto !important;
}
Take a look here
http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php
回答4:
This works for me for video in a div container.
.videoContainer
{
position:absolute;
height:100%;
width:100%;
overflow: hidden;
}
.videoContainer video
{
min-width: 100%;
min-height: 100%;
}
Reference: http://www.codesynthesis.co.uk/tutorials/html-5-full-screen-and-responsive-videos
回答5:
If you're looking for the equivalent to background-size: cover
for video
.
video {
object-fit: cover;
}
This will fill the container without distorting the video.
PS: I'm extending on Leo Yu's answer here.
回答6:
Easiest & Responsive.
<body>
<video src="full.mp4" autoplay muted loop></video>
</body>
video {
height: 100vh;
width: 100%;
object-fit: fill;
position: absolute;
}
回答7:
I use JavaScript and CSS to accomplish this. The JS function needs to be called once on init and on window resize. Just tested in Chrome.
HTML:
<video width="1920" height="1080" controls>
<source src="./assets/video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
JavaScript:
function scaleVideo() {
var vid = document.getElementsByTagName('video')[0];
var w = window.innerWidth;
var h = window.innerHeight;
if (w/16 >= h/9) {
vid.setAttribute('width', w);
vid.setAttribute('height', 'auto');
} else {
vid.setAttribute('width', 'auto');
vid.setAttribute('height', h);
}
}
CSS:
video {
position:absolute;
left:50%;
top:50%;
-webkit-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
}
回答8:
This is a great way to make the video fit a banner, you might need to tweak this a little for full screen but should be ok. 100% CSS.
position: absolute;
top: 50%;
left: 50%;
z-index: 1;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
transform: translate(-50%, -50%);
回答9:
I am new into all of this. Maybe you can just add/change this HTML code. Without need for CSS. It worked for me :)
width="100%" height="height"
回答10:
We tried with the below code & it works on Samsung TV, Chrome, IE11, Safari...
<!DOCTYPE html>
<html>
<head>
<title>Video</title>
<meta charset="utf-8" />
<style type="text/css" >
html,body {
height: 100%;
text-align: center;
margin: 0;
padding:0;
}
video {
width: 100vw; /*100% of horizontal viewport*/
height:100vh; /*100% of vertical viewport*/
}
</style>
</head>
<body>
<video preload="auto" class="videot" id="videot" preload>
<source src="BESTANDEN/video/tible.mp4" type="video/mp4" >
<object data="BESTANDEN/video/tible.mp4" height="1080">
<param name="wmode" value="transparent">
<param name="autoplay" value="false" >
<param name="loop" value="false" >
</object>
</video>
</body>
</html>