将div rgba悬停在背景图像上,而不更改文本颜色的不透明度

时间:2014-09-04 05:28:24

标签: html css css3 twitter-bootstrap

我需要鼠标悬停的css代码,例如当你悬停图像时,它将显示1px的边框颜色和总div中rgba的颜色不透明度,但不会改变h3标签中文本的不透明度。同时当悬停文本和之前的颜色将像附加的图片一样改变。我知道我的css代码是错误的,虽然我放在这里。 这是jsFiddle Demo

这里分别是html和css代码---

HTML:

<div class="service-single-box relax-massage">
    <h3 class="service-title">Relax Massage</h3>
</div>

CSS:

.service-single-box {

    cursor: default;
    margin-top: 15px;
    margin-bottom: 15px;
    overflow: hidden;
    position: relative;
    text-align: center;
    height: 136px;
   }


.service-single-box:hover, .service-single-box:focus{

    background-color:rgba(0,0,0,0.5);
    border-top: 1px solid #ab9661; 
    border-left: 1px solid #ab9661; 
    border-bottom: 2px solid #ab9661; 
    border-right: 2px solid #ab9661; 
    cursor: default;

   }


.service-single-box img:hover, .service-single-box img:focus{

   background-color: #282318; 
   border: 1px solid #ab9661; 
   cursor: default;
   background-color:rgba(0,0,0,0.5); 

   }


.relax-massage{

    background-image: url("../images/services.png");
   } 

.relax-massage h3{

   color:#fdfdfb;
   text-transform: uppercase;
   font-size: 16px;
   font-weight: bold;
   width: 170px;
   padding-top: 20px;
  }


.relax-massage h3:hover{

   color:#a4905d; 
  }


.relax-massage h3:before {

    width: 28%;
    background: none repeat scroll 0 0 #fdfdfb;
    border-top: 1px solid #fdfdfb;
    content: "";
    height: 5px;
    top: 28px;
    position: absolute;
  }

2 个答案:

答案 0 :(得分:0)

像这样更改您的CSS(主要是您对某些可视化更改所做的操作以及所需的更改以使其按您的需要工作):

body{background:#333;}
.service-single-box {
    cursor: default;
    margin-top: 15px;
    margin-bottom: 15px;
    overflow: hidden;
    position: relative;
    text-align: center;
    width:200px;
    height: 136px;

   }


.service-single-box:hover, .service-single-box:focus{
    background-color:rgba(0,0,0,0.5);
    border-top: 1px solid #ab9661; 
    border-left: 1px solid #ab9661; 
    border-bottom: 2px solid #ab9661; 
    border-right: 2px solid #ab9661; 
    cursor: default;

   }


.service-single-box img:hover, .service-single-box img:focus{
   background-color: #282318; 
   border: 1px solid #ab9661; 
   cursor: default;
   background-color:rgba(0,0,0,0.5); 

   }


.relax-massage{
    background-image: url("http://images.nationalgeographic.com/wpf/media-live/photos/000/750/cache/5-week-in-space-278_75042_600x450.jpg");
    border:1px solid
   } 

.relax-massage h3{
   color:#fdfdfb;
   text-transform: uppercase;
   font-size: 16px;
   font-weight: bold;
   width: 170px;
   padding-top: 20px;
  }


.relax-massage h3:hover{
   color:#a4905d; 
  }


.relax-massage h3:before {

    width: 28%;
    background: none repeat scroll 0 0 #fdfdfb;
    border-top: 1px solid #fdfdfb;
    content: "";
    height: 5px;
    top: 28px;
    position: absolute;
  }

.relax-massage:hover > h3:before{
   background:#a4905d; 
    border-top:none;
  }

您可以看到fiddle here

透明度更新:

HTML code:

<div class="service-single-box relax-massage">
    <div class="overlay">
         <h3 class="service-title">Relax Massage</h3>

    </div>
</div>

CSS代码:

body {
    background:#333;
}
.service-single-box {
    cursor: default;
    margin-top: 15px;
    margin-bottom: 15px;
    overflow: hidden;
    position: relative;
    text-align: center;
    width:200px;
    height: 136px;
}
.service-single-box:hover, .service-single-box:focus {
    background-color:rgba(0, 0, 0, 0.5);
    border-top: 1px solid #ab9661;
    border-left: 1px solid #ab9661;
    border-bottom: 2px solid #ab9661;
    border-right: 2px solid #ab9661;
    cursor: default;
}
.service-single-box img:hover, .service-single-box img:focus {
    background-color: #282318;
    border: 1px solid #ab9661;
    cursor: default;
    background-color:rgba(0, 0, 0, 0.5);
}
.relax-massage {
    background-image: url("http://images.nationalgeographic.com/wpf/media-live/photos/000/750/cache/5-week-in-space-278_75042_600x450.jpg");
    border:1px solid #333;
}
.relax-massage h3 {
    color:#fdfdfb;
    text-transform: uppercase;
    font-size: 16px;
    font-weight: bold;
    width: 170px;
    padding-top: 35px;
}
.overlay:hover > h3 {
    color:#a4905d;
}
.relax-massage h3:before {
    width: 28%;
    background: none repeat scroll 0 0 #fdfdfb;
    border-top: 1px solid #fdfdfb;
    content:"";
    height: 5px;
    top: 28px;
    position: absolute;
}
.overlay:hover > h3:before {
    background:#a4905d;
    border-top:none;
}
.overlay {
    background:transparent;
    display:block;
    width:100%;
    height:100%;
    position:relative;
    top:-15px;
}
.overlay:hover {
    background:rgba(0, 0, 0, 0.5)
}

答案 1 :(得分:0)

这是另一个诽谤------------

JSFiddle

HTML代码

  <div class="service-single-box relax-massage">
        <h3 class="holder">Relax Massage</h3>
  </div>

CSS代码

 .service-single-box {

    margin-top: 22px;
    margin-bottom: 15px;
    position: relative;
    text-align: center;
    height: 140px;
    width: auto;
 }

.service-single-box:before {
    content: "";
    position: absolute;
    z-index: 1;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    opacity: 0.2;
    background-repeat: no-repeat;
}

.holder {
    z-index: 2;
    position: relative;
}

.service-single-box:hover{
    border:1px solid #a4905d;    
} 

.service-single-box h3:before {
    width: 28%;
    background: none repeat scroll 0 0 white;
    border-top: 1px solid white;
    content:"";
    height: 5px;
    top: 20px;
    position: absolute; 
}
.service-single-box:hover > h3:before {
    background:#a4905d;
    border-top:none;

}
.service-single-box:hover > h3{
    color:#a4905d;
    border-top:none;
} 

.relax-massage h3 {
    color: #fdfdfb;
    font-size: 15px;
    font-weight: bold;
    padding-top: 33px;
    text-transform: uppercase;
    width: 200px;
}
.relax-massage:before{
    background-image: url("../images/Relux massage.png");
    content: "";
    position: absolute;
    z-index: 1;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    opacity: 0.5;
    background-repeat: no-repeat;
}  
.relax-massage:hover:before{
    content: "";
    position: absolute;
    z-index: 1;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    opacity: 0.08;  
    padding-top:0px;        
}