2017-05-26 109 views
0

上漿我有一個文本覆蓋一個Flexbox的頂部,一切看起來正常時,瀏覽器是全寬:文本覆蓋不Flexbox的

enter image description here

然而,當我縮小瀏覽器寬度較小,覆蓋變得比形象更廣泛: enter image description here

我的HTML看起來像這樣:

<a ng-if="::(options.link_template == 'Picture')" ng-href="{{::data.href}}" class="picture {{::options.class_name}}" target="{{::data.target}}"> 
    <img src="{{::options.picture}}" alt="" width="100%" height="100%"/> 
    <h3><span>{{::options.title}}</span></h3> 
</a> 

我的CSS是這樣的:

a.picture { 
    position: relative; 
    //width: 100%; 
    display: flex; 
    flex-direction: column; 
    align-items: center; 
    justify-content: center; 
} 

a.picture img { 
    width: 360px; 
    height: 238px; 
} 

a.picture > h3 { 
    margin: 0; 
    position: absolute; 
    width: 100%; 
    text-align: center; 
} 

a.picture > h3 span { 
    display: block; 
    color: white; 
    font-weight: bold; 
    background: rgb(0, 0, 0); /* fallback color */ 
    background: rgba(51, 103, 150, 0.6); 
    padding: 10px; 
} 

什麼我從我的CSS缺失,使得與圖像疊加「鎖」?謝謝!

回答

0

設置父容器的寬度等於圖像大小。

a.picture { 
 
     position: relative; 
 
     width: 360px; 
 
     display: flex; 
 
     \t flex-direction: column; 
 
     align-items: center; 
 
     justify-content: center; 
 
    } 
 
    
 
    a.picture img { 
 
    \t width: 360px; 
 
     height: 238px; 
 
    } 
 
    
 
    a.picture > h3 { 
 
    \t margin: 0; 
 
     position: absolute; 
 
    \t width: 100%; 
 
     text-align: center; 
 
     top: 50%; 
 
     transform: translateY(-50%); 
 
    } 
 
    
 
    a.picture > h3 span { 
 
    \t display: block; 
 
     color: white; 
 
     font-weight: bold; 
 
     background: rgb(0, 0, 0); /* fallback color */ 
 
     background: rgba(51, 103, 150, 0.6); 
 
     padding: 10px; 
 
    }
<a class="picture"> 
 
    <img src="https://static.pexels.com/photos/1510/road-sky-sand-street.jpg" width="100%" height="100%"/> 
 
    <h3><span>Content</span></h3> 
 
</a>

+0

奏效!快速跟進問題,有沒有辦法使用flexboxes,使圖像是相同的寬度和高度,沒有指定實際的高度和寬度尺寸? – Dave

+0

你的意思是相同的寬度和高度?一個正方形?另外,「沒有指定實際的高度和寬度尺寸」是什麼?圖片?還是父母? –

+0

所以不是我指定的寬度是360px,高度是238px例如,有沒有辦法只使用flexbox,以便所有圖像都是相同的尺寸。我希望圖像能夠填充父項的整個空間,但我希望父母的尺寸相同,這是否合理? – Dave