简体   繁体   English

如何将视频右对齐?

[英]How can I align a video to the right?

I have an HTML5 video with a height greater than its width.我有一个高度大于宽度的 HTML5 视频。 I gave the video 100% in height and 100% in width , so it scales automatically depending ob my window size.我给视频设置了100% in height100% in width ,因此它会根据我的 window 大小自动缩放。

But I want the video to be on the right ob its container.但我希望视频位于其容器的右侧。 In the image below, you can see that the video places its content in center..在下图中,您可以看到视频将其内容置于中心位置。

How can I achieve that?我怎样才能做到这一点?

screenshot of video element视频元素的屏幕截图

 .container { width: 100%; display: flex; }.left, .right { height: 100vh; width: 50%; border: 1px dotted blue; display: flex; align-items: center; }.video { width: 100%; height: 100%; }
 <div class="container"> <div class="left"> </div> <div class="right"> <video class="video" playsinline loop autoplay muted> <source src="" type="video/mp4"> </video> </div> </div>

The main issue is, that the video has a width: 100%;主要问题是,视频的width: 100%; . . As such it spans the entire parent's width even when the width of the video is smaller than the parent.因此,即使视频的宽度小于父级,它也会跨越整个父级的宽度。 Remove the .video { width: 100%; }删除.video { width: 100%; } .video { width: 100%; } to fix that. .video { width: 100%; }来解决这个问题。 After that, you can align it to the right of the container by using: margin-left: auto之后,您可以使用以下命令将其对齐到容器的右侧: margin-left: auto

 .container { width: 100%; display: flex; }.left, .right { height: 100vh; width: 50%; border: 1px dotted blue; display: flex; align-items: center; }.video { height: 100%; margin-left: auto; }
 <div class="container"> <div class="left"> </div> <div class="right"> <video class="video" playsinline loop autoplay muted> <source src="https://marceldiedrich.com/so/video-hor.mp4" type="video/mp4"> </video> </div> </div>

There is nothing wrong with your css style, it is the problem about the video .你的css风格没有问题,是video的问题。 It contains lots of space.它包含很多空间。

You could use object-fit and width to change and crop the video您可以使用object-fitwidth来更改和裁剪视频

 .container { width: 100%; display: flex; }.left, .right { height: 100vh; width: 50%; border: 1px dotted blue; align-items: center; overflow:hidden }.video { width: 110%; height: 110%; border: 2px solid red; margin-left:calc(-2vw); margin-top:calc(-2vh); object-fit:cover }
 <div class="container"> <div class="left"> </div> <div class="right"> <video class="video" playsinline loop autoplay muted> <source src="https://marceldiedrich.com/so/video-hor.mp4" type="video/mp4"> </video> </div> </div>

For the video to scale automatically you should use another div to nest your video in. As @tacoshy mentioned, the width 100% is the main problem as it prevents us from using margin-left auto.对于自动缩放的视频,您应该使用另一个 div 来嵌套您的视频。正如@tacoshy 提到的,宽度 100% 是主要问题,因为它阻止我们使用 margin-left auto。 Hence, losing the scalability.因此,失去了可扩展性。

Now you can use width: fit-content;现在你可以使用width: fit-content; to contain the video and set the height to 100% as expected.包含视频并将高度设置为预期的 100%。 Then just add margin-left: auto;然后只需添加margin-left: auto; on the new div instead of the video itself, this way it scales.在新的 div 而不是视频本身上,这样它就可以缩放。

 .container { width: 100%; display: flex; }.left, .right { height: 100vh; flex: 1; border: 1px dotted blue; display: flex; align-items: center; }.video { width: 100%; height: 100%; }.right>div { height: 100%; width: fit-content; margin-left: auto; }
 <div class="container"> <div class="left"> </div> <div class="right"> <div> <video class="video" playsinline loop autoplay muted> <source src="https://marceldiedrich.com/so/video-hor.mp4" type="video/mp4"></video> </div> </div> </div>

Safari Solution (even with shrinking viewport height) ~ Safari 解决方案(即使缩小视口高度)~

 .container { width: 100%; display: flex; }.left, .right { height: 100vh; flex: 1; border: 1px dotted blue; display: flex; align-items: center; }.video { max-width: 100%; height: 100%; }.right>div { height: 100%; width: 100%; display: flex; justify-content: flex-end; }
 <div class="container"> <div class="left"> </div> <div class="right"> <div> <video class="video" playsinline loop autoplay muted> <source src="https://marceldiedrich.com/so/video-hor.mp4" type="video/mp4"></video> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM