2015-02-06 88 views
0

我想將播放按鈕(第一個圖像)放到第二個圖像的中央。我可以用絕對位置和邊緣位置來做到這一點,但這怎麼可能是動態的?如果我在一個循環中,第二個圖像的高度並不總是一樣的話呢?在圖像的頂部放置一個圖像

DEMO http://jsfiddle.net/yusuuqck/

<div> 
    <img class="ply" src="http://maxcdn.clubcooee.com/img/icons/play-button2.png"/> 
<img src="http://www.howtorecordpodcasts.com/wp-content/uploads/2012/10/YouTube-Background-Pop-4.jpg"/> 
</div> 
+0

你不能這樣做,使用IMG,你必須在背景圖像使用一個div,固定寬度和股利或圖像的高度,圖像未經HTML不是塊並可以擴大比divs,看這個http://jsfiddle.net/L51auybr/ – ppascualv 2015-02-06 07:10:48

回答

0

這可能幫助

<div class="bg_img"> 
    <img src="http://www.howtorecordpodcasts.com/wp-content/uploads/2012/10/YouTube-Background-Pop-4.jpg"> 
    <span></span> 
</div> 

CSS

.bg_img { position: relative; } 

.bg_img span { 
    display: block; 
    position: absolute; 
    top: 0; 
    left: 0; 
    bottom: 0; 
    right: 0; 
    z-index: 1; 
    background: url(http://maxcdn.clubcooee.com/img/icons/play-button2.png) no-repeat center center; 
} 
0

使用DIV顯示爲表格單元格。

http://jsfiddle.net/yusuuqck/2/

HTML

<div> 
    <img class="ply" src="http://maxcdn.clubcooee.com/img/icons/play-button2.png"/> 
    <img src="http://www.howtorecordpodcasts.com/wp-content/uploads/2012/10/YouTube-Background-Pop-4.jpg"/> 
</div> 

CSS

div { 
    position:relative; 
    width:100%; 
    height:100%; 
    display: table-cell; 
} 

.ply{ 
    position:absolute; 
    top: 50%; 
    left: 50%; 
    width: 50px; 
    height: 50px; 
    margin-top: -25px; /* Half the height */ 
    margin-left: -25px; /* Half the width */ 
} 
0

檢查這個代碼。

div{ 
 
    position:relative; 
 
    display:flex; 
 
    img{ 
 
     width:100%; 
 
     height:auto; 
 
    } 
 
} 
 

 
.ply{ 
 
    position:absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    width: 50px; 
 
    height: 50px; 
 
    margin-top: -25px; /* Half the height */ 
 
    margin-left: -25px; /* Half the width */ 
 
}
<div> 
 
    <img class="ply" src="http://maxcdn.clubcooee.com/img/icons/play-button2.png"/> 
 
<img src="http://www.howtorecordpodcasts.com/wp-content/uploads/2012/10/YouTube-Background-Pop-4.jpg"/> 
 
</div>