2016-07-12 76 views
2

有圖像。在這個例子中它有一個width: 320px;和一個height: 200px;。該覆蓋做工精細用:使覆蓋容器的大小與內部圖像相同

.overlay-content { 
    width: 320px; 
    height: 200px; 
    ... 
} 

但我不能圖像的一系列的聲明,就像

img { 
    ... 
} 

有沒有辦法做到這一點不知道寬度和身高.overlay-content並且沒有對<img>設置聲明?

這裏是一個JSFiddle,和相應的示例:

img { 
 
    /* You can't do anything here */ 
 
} 
 
.overlay { 
 
    position: relative; 
 
    margin: 50px; 
 
} 
 
.overlay-content { 
 
    position: absolute; 
 
    /* This you don't know */ 
 
    width: 320px; 
 
    /* This you don't know */ 
 
    height: 200px; 
 
    top: 0; 
 
    background: grey; 
 
    opacity: 0; 
 
    transition: opacity .3s ease; 
 
} 
 
.overlay-content div { 
 
    position: absolute; 
 
    height: 100%; 
 
    width: 0%; 
 
    overflow: hidden; 
 
    right: 0; 
 
    background: orange; 
 
    transition: width .3s ease-in-out; 
 
} 
 
.overlay-content div ul { 
 
    display: block; 
 
    width: 100%; 
 
    height: 100%; 
 
    padding: 0; 
 
    margin: 0; 
 
    list-style-type: none; 
 
} 
 
.overlay-content div ul li { 
 
    position: relative; 
 
    height: 33.3333%; 
 
    width: 0%; 
 
    text-align: center; 
 
    overflow: hidden; 
 
    transition-property: width; 
 
    transition-delay: .2s; 
 
    transition-duration: .5s; 
 
    transition-timing-function: ease; 
 
} 
 
.overlay-content div ul li span, 
 
.overlay-content div ul li a { 
 
    position: absolute; 
 
    display: block; 
 
    width: 100%; 
 
    top: 50%; 
 
    margin-top: -25px; 
 
    font-size: 22px; 
 
    height: 50px; 
 
    line-height: 50px; 
 
    vertical-align: middle; 
 
    color: yellow; 
 
} 
 
.overlay:hover > .overlay-content { 
 
    opacity: 1; 
 
} 
 
.overlay:hover > .overlay-content div { 
 
    width: 50%; 
 
} 
 
.overlay:hover > .overlay-content div ul li { 
 
    width: 100%; 
 
}
<div class="overlay"> 
 
    <img src="http://placehold.it/320x200" /> 
 
    <div class="overlay-content"> 
 
    <div> 
 
     <ul> 
 
     <li><a href="#">First</a> 
 
     </li> 
 
     <li><span>Second</span> 
 
     </li> 
 
     <li><span>Third</span> 
 
     </li> 
 
     </ul> 
 
    </div> 
 
    </div> 
 
</div> 
 
<div class="overlay"> 
 
    <img src="http://placehold.it/320x200" /> 
 
    <div class="overlay-content"> 
 
    <div> 
 
     <ul> 
 
     <li><a href="#">First</a> 
 
     </li> 
 
     <li><span>Second</span> 
 
     </li> 
 
     <li><span>Third</span> 
 
     </li> 
 
     </ul> 
 
    </div> 
 
    </div> 
 
</div>

回答

1

可以設置覆蓋作爲display:inline-block;display:table;,使得它的大小將是相同的作爲圖像,因爲.overlay-content被設置爲絕對位置,所以它不在正常的內容流中。

.overlay { 
    position: relative; 
    display: inline-block; 
    ... 
} 

和覆蓋內容爲:

.overlay-content { 
    position: absolute; 
    top: 0; 
    bottom: 0; 
    left: 0; 
    right: 0; 
    ... 
} 

jsFiddle

相關問題