HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
<title>Cool Effect</title>
</head>
<body>
<div class="imageHolder">
<span class="note">Hello!</span>
<img src="picture1.jpeg">
</div>
</body>
</html>
CSS:
.imageHolder {
position: relative;
border: 1px solid black;
width: 300px;
height: 250px;
text-align: center;
overflow: hidden;
background-color: black;
}
.note {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
border: 2px solid white;
padding: 8px;
color: white;
opacity: 1;
font-size: 24px;
display: inline-block;
}
img {
width: 300;
opacity: 0.4;
height: 250px;
}
fiddle :https://jsfiddle.net/xsqosw0c/1/
跨度中的文本看起来有点透明,而不是纯白色。我不确定为什么会这样。如果将 font-size 调大一点会更清楚。
谢谢。
最佳答案
这是因为文字在图片后面。使用 z-index
解决这个问题:
.note {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
border: 2px solid white;
padding: 8px;
color: white;
opacity: 1;
font-size: 24px;
display: inline-block;
z-index:2000;
}
z-index
将元素向前或向后移动到 CSS“层”中。默认情况下,HTML 元素将按照它们在 HTML 代码中显示的顺序“分层”。您可以重新排序 HTML 行,或应用 z-index
关于html - 为什么文字是透明的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31570647/