[英]Layer Text Between Background Color Background Image
I have a block of text and it want it to be above the block's background color and below the block's background image. 我有一个文本块,它希望它位于该块的背景颜色之上和该块的背景图像之下。 I know I could just create multiple blocks and
z-index
them. 我知道我可以创建多个块并对其进行
z-index
。 I'm wanting to keep the code as clean as possible and not have a bunch of unnecessary stuff. 我想保持代码尽可能的干净,并且不要有一堆不必要的东西。
I'm guessing that the answer is "no", but maybe you guys know something I do (maybe a nifty new background property or JavaScript function). 我猜答案是“否”,但是也许你们知道我所做的事情(也许是一个漂亮的新背景属性或JavaScript函数)。
HTML Code: HTML代码:
<section role="main" id="content">
<h1 class="pageheading">Home Page</h1>
</section>
CSS Code: CSS代码:
#content {
background-color:#69583b;
background-image:url(tattoo-256x256.png);
background-position:center bottom;
background-repeat:no-repeat;
}
Obviously, as it stands, the text "Home Page" sits on top of the image. 显然,按原样,文本“首页”位于图像顶部。
The logical thought would simply be to use <img>
and position:absolute
it, but, again, that's not my intention. 逻辑上的想法只是使用
<img>
和position:absolute
,但这再次不是我的意图。 I know multiple, other, ways it can be done. 我知道可以采用多种其他方式。 I'm just trying to find out if I can do it, this way.
我只是想找出我是否可以这样做。
You could do it with a CSS pseudo class. 您可以使用CSS伪类来实现。 (I've added in opacity just as part of the demo):
(作为演示的一部分,我添加了不透明度):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
#content {
background-color:#69583b;
background-position:center bottom;
background-repeat:no-repeat;
position: relative;
}
#content::after {
content: "";
position: absolute;
z-index: 2;
width: 100%;
top: 0; bottom: 0; left: 0; right: 0;
background: url(http://placehold.it/256x256);
opacity: 0.8;
}
</style>
</head>
<body>
<section role="main" id="content">
<h1 class="pageheading">Home Page</h1>
</section>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.