2017-10-19 80 views
0

嗨,大家好,我正在试图让我的按钮在bootstrap 3重叠我的形象,我明白z-index 1的使用,但我似乎无法让它工作。获取按钮鼠标悬停在图像

HTML:

.btn-warning { 
 
    color: #fff; 
 
    background-color: rgba(255, 198, 0, 0.9); 
 
    border: 0; 
 
    padding: 20px; 
 
    border-radius: 26px; 
 
    line-height: 0.428571; 
 
} 
 

 
.aboutpic { 
 
    margin-bottom: 19px; 
 
}
<div class="aboutpic"> 
 
    <img src="//via.placeholder.com/350x150" class="img-responsive"> 
 
    <button type="button" class="btn btn-warning">Download CV</button> 
 
</div>

回答

2

试试这个:

.btn-warning { 
color: #fff; 
background-color: rgba(255,198,0,0.9); 
border: 0; 
padding: 20px; 
border-radius: 26px; 
line-height: 0.428571; 
position: absolute; 
z-index: 1; 
top: 50%; 
left: 50%; 
transform: translate(-50%, -50%); 
} 

你需要添加一个position: absolute;z-index: 1的按钮,以便它可以坐在图像的顶部。

codepen.io/lauraeddy/pen/KXEwOW

+0

Dosent在现在都显示按钮 – RonTheOld

+0

到这里看看:https://codepen.io/lauraeddy/pen/KXEwOW – Laura

+0

才是好我拿出顶部,左侧和改造当它在浏览器缩小的时候,它在中间显示正确,并且响应很快:) – RonTheOld

0

请尝试以下,

.btn-warning { 
 
    color: #fff; 
 
    background-color: rgba(255,198,0,0.9); 
 
    border: 0; 
 
    padding: 20px; 
 
    border-radius: 26px; 
 
     line-height: 0.428571; 
 
} 
 
img{ 
 
    height:200px; 
 
    width:200px; 
 
} 
 

 
.aboutpic{ 
 
    position:relative; 
 
} 
 

 
.first{ 
 
    position:absolute; 
 
    z-index:1; 
 
} 
 
.second{ 
 
    position:absolute; 
 
    z-index:2; 
 
    left:20px; 
 
    top:50px; 
 
}
<div class="aboutpic"> 
 
    <div class="first"> 
 
      <img src="https://static.pexels.com/photos/377911/pexels-photo-377911.jpeg" class="img-responsive"> 
 
    </div> 
 
    <div class="second"> 
 
      <button type="button" class="btn btn-warning">Download CV</button> 
 
    </div> 
 

 
    </div>

希望这有助于。

1

可以的aboutpicrelative位置和按钮设置为absolute,如在下面的例子。

UPDATE

添加样式属性在一个任意大小的图像中心的按钮,利用calc(50% - [button width or button height])

.btn-warning { 
 
    color: #fff; 
 
    background-color: rgba(255, 198, 0, 0.9); 
 
    border: 0; 
 
    padding: 20px; 
 
    border-radius: 26px; 
 
    line-height: 0.428571; 
 
    width: 120px; 
 
    height: 50px; 
 
} 
 

 
.aboutpic button { 
 
    position: absolute; 
 
    top: calc(50% - 25px); 
 
    left: calc(50% - 60px); 
 
} 
 

 
.aboutpic { 
 
    margin-bottom: 19px; 
 
    position: relative; 
 
    width: 350px; 
 
    height: 150px; 
 
}
<div class="aboutpic"> 
 
    <img src="http://via.placeholder.com/350x150" class="img-responsive"> 
 
    <button type="button" class="btn btn-warning">Download CV</button> 
 
</div>

+0

这个工作,但我不得不改变底部:149px;和右:510px;有没有更好的方式把它放在图像的中间?或者是最好的方法 – RonTheOld

+0

是的,还有更好的方法,请参阅答案的更新。 – jfeferman