如何使背景图像在响应方形网格中透明?

Erw*_*ers 9 html css background image opacity

使用web-tiki的响应方形网格布局我已经制作了一些带有背景图像和文本的响应方块,如下所示:

HTML:

<div class="square bg imgautumn1">
   <div class="content">
        <div class="table">
            <div class="table-cell months">
                VISIBLE TEXT
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

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;}
Run Code Online (Sandbox Code Playgroud)

现在我试图只使背景透明,而不是文本.

在CSS类opacity: 0.3上使用该属性时,imgautumn1图像变为透明,但也包含其中的文本.其他技术,例如来自这个SO-answer的一个使用单独的div作为背景,或者使用此处:after元素作为背景加上不透明度的技术使得背景的定位出错(即图像不居中)和I发现很难实施.另一种可能性是在图像顶部放置一个透明的div广场,但我不认为该属性是可行的.background-image

我希望这里的某位人员可以为我提供一些如何使背景透明而不是文本的帮助.

JSFiddle:http://jsfiddle.net/L7m5psrm/

Gab*_*oli 2

:after如果您使用/:before解决方案(将图像设置为背景),似乎工作正常

您只需要确保应用相同的background属性即可。

.imgautumn1:before {
    background-image: url('https://raw.githubusercontent.com/erooijak/zaaikalender/master/Zk/Content/Images/Autumn/1.jpg');
}
/*  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;
}
Run Code Online (Sandbox Code Playgroud)

演示地址:http://jsfiddle.net/L7m5psrm/2/