2016-01-13 49 views
0

我制作了一个复选框作为森林图像。我需要从html参数(数据森林)的森林checbox图像放置另一个透明背景,但我只能通过CSS来做到这一点。我已经尝试了很多解决方案,但没有人能正常工作。任何人有一些想法?如何在没有HTML的情况下将背景与文字叠加到另一个背景图像

悬停最终效果

enter image description here

的jsfiddle:https://jsfiddle.net/ac9z8sgd/

HTML

<form action="action" method="get"> 
    <input type="hidden" name="test" value="test" /> 
    <ul> 
    <li><input id="checkbox" type="checkbox" name="forest_type" value="forest_1"><label for="checkbox_forest" data-forest="Estern forest">Forest 1</label></li> 
    </ul> 
    <button type="submit">Submit</button> 
</form> 

CSS

/* Default check button */ 
#checkbox + label { 

    background: url('http://i.imgur.com/Ds7gh7b.jpg?1'); 
    background-repeat: no-repeat; 
    height: 250px; 
    width: 250px; 
    padding: 15px 15px; 
    display: inline-block; 
    position: relative; 
} 
/* Hover action */ 
#checkbox + label[data-forest]:hover { 
    background: rgba(0,0,0,0.8); 
    content: attr(data-forest); 
    display: inline-block; 
    font-size: 20px; 
    color: white; 
    position: relative; 
} 
+0

那么,你的选择是错误的...'标签+ label'表明您正在寻找的是另一个标签的兄弟标签。你为什么不使用'#checkbox + label:hover'?如果你可以用这个问题创建一个jsfiddle,你很可能会得到更快的答案。 –

+0

是的,它是旧版本。我更新了代码并粘贴jsfiddle链接。谢谢! – XingD123

+0

你可以修改HTML吗? –

回答

0

使用伪元素。

/* Default check button */ 
 

 
#checkbox + label { 
 
    background: url('http://i.imgur.com/Ds7gh7b.jpg?1'); 
 
    background-repeat: no-repeat; 
 
    height: 275px; 
 
    width: 184px; 
 
    display: inline-block; 
 
    position: relative; 
 
} 
 
/* Hover action */ 
 

 
#checkbox + label[data-forest]:hover::after { 
 
    background: rgba(0, 0, 0, 0.8); 
 
    content: attr(data-forest); 
 
    position: absolute; 
 
    bottom: 0; 
 
    left: 0; 
 
    width: 100%; 
 
    height: 50%; 
 
    font-size: 20px; 
 
    color: white; 
 
    display: flex; 
 
}
<form action="action" method="get"> 
 
    <input type="hidden" name="test" value="test" /> 
 
    <ul> 
 
    <li> 
 
     <input id="checkbox" type="checkbox" name="forest_type" value="forest_1"> 
 
     <label for="checkbox_forest" data-forest="Estern forest">Forest 1</label> 
 
    </li> 
 
    </ul> 
 
    <button type="submit">Submit</button> 
 
</form>

JSfiddle with Transitions

+0

它看起来非常好。谢谢! Maybye同意有一个想法来围绕透明背景的2个底角? – XingD123

+0

当然,你可以在伪元素上放一个边框半径...但它可能看起来不像你期望的那样 - https://jsfiddle.net/ac9z8sgd/3/ –

+0

是的,它看起来不错,因为原始图像是圆润。悬停动作应显示1)带有文字的背景和2)图像四周的四边形边框。我做了第二个效果,但现在1和2互相咬人。我在你的文字背景周围找到了小边框 - https://jsfiddle.net/ac9z8sgd/4/ – XingD123

0

我的建议:

label[data-forest]:hover:before { 
    background: rgba(0,0,0,0.8); 
    content: attr(data-forest); 
    font-size: 20px; 
    color: white; 
    margin-top: 240px; 
    margin-left: -15px; 
    position: absolute; 
    width: 100%; 
    text-align: center; 
} 

BTW:也许是0.8太多可察觉透明度高值。我会做到0.4。

+0

不错,我会添加一些额外的复选框(图像),我会使用通用标签选择器。谢谢 – XingD123