【发布时间】:2015-01-21 04:34:25
【问题描述】:
使用web-tiki's responsive square grid lay-out's,我制作了一些带有背景图像和文字的响应方块,如下所示:
HTML:
<div class="square bg imgautumn1">
<div class="content">
<div class="table">
<div class="table-cell months">
VISIBLE TEXT
</div>
</div>
</div>
</div>
CSS:
.square {
float: left;
position: relative;
margin: 0.25%;
width: 50%;
padding-bottom : 50%; /* = width for a 1:1 aspect ratio */
background-color: #1E1E1E;
overflow: hidden;
}
.content {
position: absolute;
height: 90%; /* = 100% - 2*5% padding */
width: 90%; /* = 100% - 2*5% padding */
padding: 5%;
}
.table {
display: table;
width: 100%;
height: 100%;
}
.table-cell {
display: table-cell;
vertical-align: middle;
}
.months {
font-size: 40px;
font-weight: 900;
}
.imgautumn1:before {
background-color: black;
}
/* For responsive images as background */
.bg:before {
background-position: center center;
background-repeat: no-repeat;
background-size: cover; /* you change this to "contain" if you don't want the images to be cropped */
content:'';
position:absolute;
top:0;left:0;
right:0;bottom:0;
}
.bg{color: #fff;}
/*CHANGE OPACITY ON HOVER*/
.bg:hover:before{opacity:0.2;}
现在我试图只使背景透明,而不是文本。
在imgautumn1 CSS 类上使用opacity: 0.3 属性时,图像变得透明,其中的文本也变得透明。其他技术,如来自this SO-answer 的技术使用单独的 div 作为背景,或者使用来自here 的:after 元素作为背景加上不透明度的技术使背景的定位出错(即,图像不是居中),我发现很难实施。另一种可能性可能是在图像顶部放置一个透明的 div 方块,但我认为 background-image 属性是不可能的。
我希望这里有人可以为我提供一些关于如何只使背景透明而不是文本透明的帮助。
JSFiddle:http://jsfiddle.net/L7m5psrm/
【问题讨论】:
-
stackoverflow.com/questions/10422949/css-background-opacity 的副本。正如一个获得 200 人赞成的公认答案所说,你不能那样做。
标签: html css image background opacity