gpt4 book ai didi

css - 制作一个空的 DIV 高度和其容器的宽度

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:08 25 4
gpt4 key购买 nike

我的 HTML 当前包含一个随视口(viewport)拉伸(stretch)的背景图像。在其中,我计划放置一个 div,它延伸到视口(viewport)的高度和宽度,并具有 50% 不透明度的黑色背景。

我已将 div 设置为 100% 宽度和高度。 div 没有拉伸(stretch),我不明白为什么!

HTML:

<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Tester</title>
<meta charset="UTF-8"/>
<style type="text/css">
html {
background: url(http://cjpstudio.com/wp-content/uploads/2011/12/cityrock1.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

#background-color {
width: 100%;
height: 100%;
background-color: #000000;
}
</style>
</head>
<body>
<div id="background-color">
</div>
</body>

最佳答案

要替代 widthheight,您可以使用 position:

#background-color {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.5);
}

这将允许或可能允许用户滚动将移动 #background-color 元素的页面,以避免用户移动图像的可能性,使用 position:改为固定

如果 #background-color 元素的祖先元素的 position 不是 static,则该元素将相对于该祖先元素定位,而不是页面/文档(对于 position: absolute)或视口(viewport)(对于 position: fixed)。

虽然这是为了防止用户与您网站的内容进行交互,但它当然注定要失败。


编辑以回应 OP 的评论,如下:

Can you explain why? I don't understand how/were the div is getting instructions to fill the window. I thought I thoroughly understood position though... I guess not. Anyway, an explanation would be really helpful.

当然,这并没有太多的解释,这只是通过使用 absolutefixed 定位元素,然后将其轴定位在viewport,0pxtop0pxleft 侧,0px右侧底部0px

关于css - 制作一个空的 DIV 高度和其容器的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11301151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com