如何居中 HTML5 视频?
我只是在摆弄一些 HTML5,我试图将视频放在页面的中心。由于某种原因,我无法将其置于中心位置。我尝试向视频标签本身添加一个类,并将视频包装在单独的 div
中。但是,视频仍位于左侧。
<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<script type="text/css">
.center {
margin: 0 auto;
}
</script>
</head>
<body>
<div class="center">
<video controls="controls">
<source src="/media/MVI_2563.ogg" type="video/ogg" />
Your browser does not support the video tag.
</video>
</div>
</body>
</html>
我知道这一定是我在凌晨时忽略的事情,但任何帮助将不胜感激。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
HTML 代码:
CSS 代码:
HTML CODE:
CSS CODE:
中心类必须具有宽度才能使自动边距起作用:
然后我将中心类应用于视频本身,而不是容器:
The center class must have a width in order to make auto margin work:
Then I would apply the center class to the video itself, not a container:
我遇到了同样的问题,直到我意识到
元素是内联元素,而不是块元素。您只需将容器元素设置为
text-align: center;
即可将视频在页面上水平居中。I was having the same problem, until I realized that
<video>
elements are inline elements, not block elements. You need only set the container element to havetext-align: center;
in order to center the video horizontally on the page.这样做:
效果完美! :D
Do this:
Works perfect! :D
我不喜欢像 @user142019 所说的那样仅使用视频标签居中。我更喜欢这样做:
这将使您的视频具有响应能力,同时控件面板将具有与视频矩形相同的大小(我尝试了@user142019所说的并且视频居中,但在Google Chrome中看起来很难看)。
I will not prefer to center just using video tag as @user142019 says. I will prefer doing it like this:
This will make your video responsive at the same time the panel for controls will have the same size as the video rectangle (I tried what @user142019 says and the video was centered, but it looked ugly in Google Chrome).
我也遇到了同样的问题。这对我有用:
I was having the same problem. This worked for me:
试试这个:
Try this:
@snowBlind 在您给出的第一个示例中,您的样式规则应该放在
另外,我尝试了此答案中提到的更改(查看结果 http://jsfiddle.net/8cXqQ/7/),但它们似乎仍然不起作用。
您可以用 div 包围视频,并对 div 应用宽度和自动边距以使视频居中(以及指定视频的宽度属性,请参阅 http://jsfiddle.net/8cXqQ/9/)。
但这似乎不是最简单的解决方案...我们是否应该能够将视频居中而不必将其包装在容器 div 中?
@snowBlind In the first example you gave, your style rules should go in a <style> tag, not a <script> tag:
Also, I tried the changes that were mentioned in this answer (see results at http://jsfiddle.net/8cXqQ/7/), but they still don't appear to work.
You can surround the video with a div and apply width and auto margins to the div to center the video (along with specifying width attribute for video, see results at http://jsfiddle.net/8cXqQ/9/).
But that doesn't seem like the simplest solution...shouldn't we be able to center a video without having to wrap it in a container div?
我在 Dreamweaver 中修改网站时遇到了类似的问题。该网站结构基于一组复杂的表格,该视频位于一个主要布局单元格中。我专门为视频创建了一个嵌套表格,然后向新表格添加了
align=center
属性:I had a similar problem in revamping a web site in Dreamweaver. The site structure is based on a complex set of tables, and this video was in one of the main layout cells. I created a nested table just for the video and then added an
align=center
attribute to the new table:您所要做的就是将视频标签设置为
display:block;
FTW,默认为inline-block
FTL。试试这个
All you have to do is set you video tag to
display:block;
FTW the default isinline-block
FTL.Try this
如果你有百分比宽度,你可以这样做:
If you have a width in percent, you can do this :
使用边距
是一个内联元素,因此您必须将
display block;
添加到您的 css 类中。您可以使用特定的,但我不会使用 px 值。使用 % 使其具有响应性,例如:
这将使视频成为视口宽度的一半。
请参阅此处的原始文档 O 万维网联盟 W3C
mystyle。 CSS
请参阅此处准备好的代码以获得更多理解。
可以在线查看Fiddle代码
Use margins
<video>
is an inline element so you'll have to adddisplay block;
to your css class.You can use specific with, but I wouldn’t use px values. Use % to make it responsive, like:
That’ll make the video half of the viewport width.
See the original documentation here O World Wide Web Consortium W3C
mystyle.css
See the code ready here for more understanding.
You can view the code online Fiddle
将您的
封装在
也可以。
Encapsulate your
<video>
within<center></center>
tags,<figure></figure>
also works.我在尝试居中对齐一对视频时发现了此页面。因此,如果我将两个视频放在一个中心
div
(我称之为中心)中,边距技巧就有效,但宽度很重要(2 个视频为 400 + 填充等)对我有用!
I found this page while trying to center align a pair of videos. So, if I enclose both videos in a center
div
(which I've called central), the margin trick works, but the width is important (2 videos at 400 + padding etc)Worked for me!
如果使用 bootstrap,则可以使用 d-block 和 m-auto 类。
If you use bootstrap, you can use d-block and m-auto classes.