使用 css 网格拟合视频背景时出现问题

发布于 2025-01-15 01:06:39 字数 1815 浏览 0 评论 0原文

这里的各个方面都是新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2025-01-22 01:06:39

使用 object-fitobject-position 缩放和定位视频。
即使元素为 1920x1080,视频内容也可能不依赖于视频宽高比,并且默认情况下会缩放以包含在元素中。

/*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;

    width:100%;
    height:100%;
    
    /* Scale video to container size */
   object-fit: cover;
   object-position: center;
    
}

.item1{
    
    grid-column: 1 / 3;
    background-color: green;
    height:1080px;

}
<div class="main-container">

        <div class="item1">
            <video id="vid" src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.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>

Use object-fit and object-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.

/*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;

    width:100%;
    height:100%;
    
    /* Scale video to container size */
   object-fit: cover;
   object-position: center;
    
}

.item1{
    
    grid-column: 1 / 3;
    background-color: green;
    height:1080px;

}
<div class="main-container">

        <div class="item1">
            <video id="vid" src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.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>

凝望流年 2025-01-22 01:06:39

在此处输入图像描述

这是带有此 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;
 

}
.主容器视频 {

position:absolute;
align-self:stretch;
justify-self:stretch;
display:block;
top:0;
bottom:0;
right:0;
left:0;
width:100%;
height:100%;
min-height:400px;
min-width:400px;

}

enter image description here

Heres the inspector with this 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 {

position:absolute;
align-self:stretch;
justify-self:stretch;
display:block;
top:0;
bottom:0;
right:0;
left:0;
width:100%;
height:100%;
min-height:400px;
min-width:400px;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文