gpt4 book ai didi

具有负值和高度的 CSS 相对定位

转载 作者:技术小花猫 更新时间:2023-10-29 11:17:16 26 4
gpt4 key购买 nike

我正在尝试对 DIV 元素进行负定位(在示例中是#content),但我的问题是 div 的容器 (#wrapper2) 的高度过高(实际上是 #content 给出的高度,但是当我向上移动内容时,我想相应地降低#wrapper2 的高度)。

在这里,我给你一个例子来展示我正在努力实现的目标。如果您尝试示例,您会发现页脚与容器的距离过远。我可以在这里做一个肮脏的 hack 并使 footer top:-200px 也可以,但是窗口的滚动条会越过页脚。

<!DOCTYPE html>
<html>
<head>
<title>Relative positioning demo</title>
<style>
/* RESET STUFF */
html {
margin:0;
padding:0;
border:0;
}

body, div, p, h1 {
margin: 0;
padding: 0;
border: 0;
}
/* END RESET */

h1 {
background-color: yellow;
}

p {
margin-bottom: 1em;
}

/* LAYOUT */
#wrapper1 {
text-align: center;
height: 250px;
background-color: lightgray;
}
#wrapper2 {
background-color: lightblue;
}
#content {
width: 950px;
margin: 0 auto;
background-color: white;
padding: 5px;
height: 560px;

/* HERE's my problem */
position: relative;
top: -200px;
}
#footer {
background-color: black;
color: white;
height: 40px;
line-height: 40px;
text-align: center;
}
</style>
</head>
<body>
<div id="wrapper1">
<h1>This is my heading</h1>
</div>
<div id="wrapper2">
<div id="content">
My content here
</div>
</div>
<div id="footer">
lorem ipsum
</div>
</body>
</html>

如果您有任何建议,请记住我必须同时看到浅灰色和浅蓝色背景(它们是我网站上的图片),所以 margin-top: -200px 不是一个选项(就像相关文章中有人建议的那样)我搜索过的问题)

谢谢!

最佳答案

top 属性更改为 margin-top

Demo

        position: relative;
top: -200px;

改为

        margin-top: -200px;

关于具有负值和高度的 CSS 相对定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7893620/

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