gpt4 book ai didi

html - CSS 元素在其他元素之上

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

我是 CSS 和网络开发的新手。我已经阅读了一些与定位元素相关的堆栈溢出文章,但是我仍然无法让它工作。我想要完成的是以下内容:

1) background.png 总是在底部。
2) "header"类中的任何内容都在背景之上。
3) “导航”类中的任何内容都在横幅上方并相对于横幅定位。

<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>My Title</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<div class = "container">
<div class = "background">
<img class = "background-img" src="img/background.png">
</div>

<div class = "header">
<img class ="banner" src ="img/banner.png">
<div class = "navigation">
<p>Everything under navigation will go overtop of banner</p>
</div>
</div>
</div>




</body>

</html>

这是 CSS:

@CHARSET "US-ASCII";

body, div, h1, h2, h3, h4, h5, h6, p, ul, ol, li, dl, dt, dd, img, form, fieldset, blockquote {

border: 0; padding: 0; margin: 0;

}

body {

font-family: Helvetica, Arial, Sans-Serif; line-height: 24px;
background: #444444;
}


.container{
width: 1400px;
margin: auto;
left: 0; right: 0; top: 0;
position: absolute;
}

.background{
z-index: -1;
}

.header{
margin-top: 23px;
}

.banner{
z-index: 2;
}

.navigation{
z-index: 3;
}

最佳答案

要使用 z-index 属性,您必须为您的元素指定一个位置。所以在这个例子中你必须做这样的事情

    .header{
margin-top: 23px;
position: relative;
z-index: 2;
}

.navigation{
z-index: 3;
position: absolute;
}

关于html - CSS 元素在其他元素之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25343187/

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