使用 css 网格拟合视频背景时出现问题
这里的各个方面都是新的 xd 抱歉我的英语不好。 我试图在我的页面上制作一个典型的“背景”视频,但无论我做什么,我都无法使视频适合网格单元的内容。
这个想法是视频保持在 z-index -1 并适合 1920x1080px,但由于某种原因它被调整大小并保持在中心 o 做疯狂的事情。
第一个是带有 div 的网格模板,我将第一个设为 1920x1080px,2 列。 然后,当我将视频放入该单元格时(视频为 1920x1080px),就会发生这种情况: 在此处输入图像描述
我尝试使用 pos: Absolute 放置视频,我也尝试使用容器,分配高度,宽度,对齐内容,顶部:0,显示:块......我现在没有任何线索,我尝试了几乎所有我知道的和谷歌搜索的东西,但我做不到。
这是我的 html:
<?php get_header(); ?>
<div class="main-container">
<div class="item1">1
<video id="vid" src="http://localhost/wordpress/wp-content/themes/twentytwentyone-child/img/coffee1.mp4" width="1920" height="1080" autoplay loop muted type="video/mp4"> </video>
</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
<div class="item9">9</div>
<div class="item10">10</div>
</div>
<?php get_footer(); ?>
和我的 css
/*GRID index*/
.main-container {
display:grid;
background-color: red;
grid-template-columns: auto auto;
box-sizing:border-box;
}
.main-container div {
color:black;
border: 1px solid black;
}
.main-container video {
display:block;
top:0;
bottom:0;
right:0;
left:0;
width:100%;
height:100%;
}
.item1{
grid-column: 1 / 3;
background-color: green;
height:1080px;
}
你能帮助我吗?我做错了什么?
new here at every aspect xd Sry for my bad english.
Im trying to make a typical "background" video at my page but i can't to make the video fit the content of the grid cell whatever I do.
The idea is that the video stay at z-index -1 and fit 1920x1080px but for some reason it got resized and stay at the center o do crazy things.
The first I do its a grid template with divs, and I make the first of them 1920x1080px, 2 columns.
Then this happens when I put in the video on that cell (video is 1920x1080px):
enter image description here
I tried putting video with pos: absolute, I tried with the container too, assigning height, widths, justify-content, top:0, display: block ... i have no clues now, ive tried almost everything that I knew and googled but I couldnt do do the shit.
Heres my html:
<?php get_header(); ?>
<div class="main-container">
<div class="item1">1
<video id="vid" src="http://localhost/wordpress/wp-content/themes/twentytwentyone-child/img/coffee1.mp4" width="1920" height="1080" autoplay loop muted type="video/mp4"> </video>
</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
<div class="item9">9</div>
<div class="item10">10</div>
</div>
<?php get_footer(); ?>
And my css
/*GRID index*/
.main-container {
display:grid;
background-color: red;
grid-template-columns: auto auto;
box-sizing:border-box;
}
.main-container div {
color:black;
border: 1px solid black;
}
.main-container video {
display:block;
top:0;
bottom:0;
right:0;
left:0;
width:100%;
height:100%;
}
.item1{
grid-column: 1 / 3;
background-color: green;
height:1080px;
}
Can u help me? What I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
object-fit
和object-position
缩放和定位视频。即使元素为 1920x1080,视频内容也可能不依赖于视频宽高比,并且默认情况下会缩放以包含在元素中。
Use
object-fit
andobject-position
to scale and position the video.Even if the element is 1920x1080 the video contents may not depending on the videos aspect ratio and by default get scaled to be contained in the element.
在此处输入图像描述
这是带有此 css 的检查器:
.main-container {
}
.main-container div {
}
.主容器视频 {
}
enter image description here
Heres the inspector with this css:
.main-container {
}
.main-container div {
}
.main-container video {
}