gpt4 book ai didi

html - CSS背景图片,内容脱离页面

转载 作者:行者123 更新时间:2023-11-28 02:25:45 25 4
gpt4 key购买 nike

我试图仅使用图像的底部作为布局的背景。这是使图像以我想要的方式显示的 CSS。

CSS:

    .image{
position: absolute;
background-image: url("hero.jpg");
width: 1440px;
height: 1315px;
top:-760px;
}

所以现在当我像这样将它放入我的 html 文档时,我添加的任何内容都看不到,因为我将 -760px 放在图像的顶部。

<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Apple</title>
</head>
<body>

<div class="container">
<div class="image">


</div>
</div>

</div>

</body>
</html>

我的问题是如何在不让任何添加内容出现在页面上方和页面外的情况下使用此图像。

其余的 CSS:

body{
margin:0;
padding:0;
}

.container{
min-height:100%;
position:relative;
}

.image{
position: absolute;
background-image: url("hero.jpg");
width: 1440px;
height: 1315px;
top:-760px;
}

最佳答案

而不是拥有一个全尺寸的容器并使用负的顶部值向上移动它,如下所示:

body {
margin: 0;
padding: 0;
}

.container {
min-height: 100%;
position: relative;
}

.image {
position: absolute;
background-image: url("http://via.placeholder.com/1280x315");
width: 1280px;
height: 315px;
top: -160px;
}
<div class="container">
<div class="image">


</div>
</div>

尝试使用 background-position: 将图像容器的高度设置为需要显示的高度。在下面的代码片段中,我们使用了 background-position: center bottom;(它将容器中设置为背景的图像与底部中心对齐)

body {
margin: 0;
padding: 0;
}

.container {
min-height: 100%;
position: relative;
}

.image {
position: absolute;
background-image: url("http://via.placeholder.com/1280x315");
width: 1280px;
height: 155px; /* 315 - 160 */
background-position: center bottom;
}
<div class="container">
<div class="image">


</div>
</div>

要了解有关背景 CSS 属性的更多信息,请参阅:

https://www.w3schools.com/cssref/pr_background-position.asp

关于html - CSS背景图片,内容脱离页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47964801/

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