2017-10-19 83 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