【发布时间】:2019-02-20 01:14:52
【问题描述】:
我一直在尝试将横幅图像放在顶部标题中的文本后面,但格式似乎并不奏效。
我还在样式表中尝试了background-image: url(/imgs/header_pic.jpg);,但这样它甚至都没有出现。目前它显示,在文本后面,图像无法居中或调整最大高度,并且导航(列表项)也与图像重叠。
div#header-background {
position: absolute;
max-height: 4em;
z-index: -1;
border-radius: 10%;
opacity: 0.25;
}
h1#header-text {
position: relative;
top: 0;
text-align: center;
font-size: 3em;
}
<body>
<header>
<div id="header-background"><img src="imgs/header_pic.jpg"></div>
<h1 id="header-text">John Doe</h1>
</header>
<nav>
<ul style="list-style-type: none">
<li><a href="index.html">Main</a></li>
<li><a href="about.html">about</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
<main></main>
<footer></footer>
</body>
更新:检查答案后(谢谢!)。我做了一些研究并将其修改为以下代码。现在我只需要将文本垂直移动到中心,因为奇怪的是它不是完全居中而是偏离了一点。 大家觉得呢?
header{
width: 100%;
height: 12em;
position: relative;
}
h1#header-text{
position: absolute;
z-index:999;
text-align: center;
font-size: 3em;
width: 100%;
height: 100%;
}
header:after{
content: "";
position: absolute;
top: 0px;
width: 100%;
height: 100%;
background-image: url(imgs/header.jpg);
background-position: center;
background-repeat: no-repeat;
background-size: contain;
opacity: 0.25;
}
【问题讨论】:
标签: css html http-headers background-image