我正在try 使用视频作为循环和静音的背景.问题是,我想要有一个标题和副标题在视频的中心.不过,当在视频中使用位置:绝对这样的内容时,它会 destruct 页面的完全响应性.

我试着在视频上设置绝对位置,如果它不应该有react 的话,它确实可以工作.我也try 了一些相对位置的东西,尽管文本在视频的右侧(因为它是行中的第二个弹性项).如何正确地将文本居中放置在背景视频上?对于图片,我倾向于使用background-image标签,根据我的研究,这对视频不起作用.

<div className='homeImage'>
   <video loop autoPlay muted>
      <source src={video} type="video/mp4"/>
   </video>
   <div className='home-content'>
      <h1 className='mb-4'>My Name</h1>
      <h2>This is a blog</h2>
   </div>
</div>
.homeImage{
    background-color: black;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
}
   
.home-content{
    height: 100vh;
    width: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.home-content h1{
    font-size: 90px !important;
    color: #fff !important;
    z-index: 10;
}

.home-content h2{
    font-size: 40px !important;
    color: #fff !important;
    z-index: 10;
}

.homeImage img,video{
    opacity: 0.5;
    height: 100vh;
    z-index: 0;
    width: 100%;
    object-fit: cover;
    position: absolute;
}

@media (max-width: 767px){
    .home-content h1{
        font-size: 60px !important;
    }

    .home-content h2{
        font-size: 35px !important;
    }
}

@media (max-width: 497px){
    .home-content h1{
        font-size: 30px !important;
    }

    .home-content h2{
        font-size: 20px !important;
    }
}

This is how it looks after modifying the CSS: enter image description here

所以遗憾的是它仍然没有react

推荐答案

将文本放置在视频(或IMG)上.您可以将文本和视频包装在一个div中(您已经这样做了).将div设置为position: relative;,并将文本定位为position: absolute;.这样,内容保留在页面流中,同时允许您随心所欲地放置文本!

.homeImage{
    background-color: black;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;

    /* added code */
    position: relative;
}
   
.home-content{
    height: 100vh;
    width: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;

    /* added code */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

.home-content h1{
    font-size: 90px !important;
    color: #fff !important;
    z-index: 10;
}

.home-content h2{
    font-size: 40px !important;
    color: #fff !important;
    z-index: 10;
}

.homeImage img,video{
    opacity: 0.5;
    height: 100vh;
    z-index: 0;
    width: 100%;
    object-fit: cover;
    /* remove position: absolute; from here*/
}

@media (max-width: 767px){
    .home-content h1{
        font-size: 60px !important;
    }

    .home-content h2{
        font-size: 35px !important;
    }
}

@media (max-width: 497px){
    .home-content h1{
        font-size: 30px !important;
    }

    .home-content h2{
        font-size: 20px !important;
    }
}
<div className='homeImage'>
   <video loop autoPlay muted>
      <source src={video} type="video/mp4"/>
   </video>
   <div className='home-content'>
      <h1 className='mb-4'>My Name</h1>
      <h2>This is a blog</h2>
   </div>
</div>

Html相关问答推荐

如何正确使用counter()、counter—increment()和counter—reset()嵌套标题?

自动字间距以适应行CSS

HTML5章节或文章

在Chrome中使用手写笔时,滚动条方向反转

如何在所有屏幕大小下使带有背景图像的伪元素作为标题的衬底?

我无法使用背景图像css,因为我在VS代码中将照片放在不同的文件夹中

摆动的html标签

有没有办法根据另一个项目调整FlexBox项目的大小?

带有图标和悬停过渡的CSS按钮

Tailwind CSS 忽略我的元素的填充

Flex:第一个 div 有列,下一个 div 有行

为 HTML5 文本字段设置最后六位正则表达式模式

我如何使用 html 在表格的单元格内实现下面显示的背景 colored颜色 加载器(请判断设计参考的图像链接)

使用flex wrap时,如何设置另一个元素的宽度与换行后的元素对齐?

将图像的高度限制为固定的纵横比,并在超过时使用 object-fit

在 iPhone 上分隔 HTML 邮箱输入中的多个邮箱条目

如何让一个 div 元素粘在另一个元素上?

为四开本网站主页添加背景图片

Angular Material Hide Password 在输入时切换

如何在锚点可点击的伪元素周围制作空白?