2015-07-22 33 views
1

我在td中設置了imgwidthheight設置爲100%。在Chrome中width服從,但height不符合。它似乎在所有其他瀏覽器中按預期工作。image在鉻中沒有遵守的高度尺寸

不知道爲什麼?

div 
 
{ 
 
    background-color: red; 
 
    width: 100px; 
 
    height: 100px; 
 
} 
 

 
table 
 
{ 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
img 
 
{ 
 
    width: 100%; 
 
    height: 100%; 
 
}
<div> 
 
    <table border="1"> 
 
     <tr> 
 
      <td> 
 
       <img src="https://www.google.com/images/srpr/logo11w.png" /> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
</div>

回答

0

以供將來參考,我能夠得到它通過在div包裹img工作。

div.one 
 
{ 
 
    background-color: red; 
 
    width: 100px; 
 
    height: 100px; 
 
} 
 

 
table 
 
{ 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
img 
 
{ 
 
    width: 100%; 
 
    height: 100%; 
 
}
<div class="one"> 
 
    <table border="1"> 
 
     <tr> 
 
      <td> 
 
       <div> 
 
        <img src="https://www.google.com/images/srpr/logo11w.png" /> 
 
       </div> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
</div>

2

,則應該設置最大高度最大寬度避免內部元件延伸超過outter元件。

HTML

<div> 
    <img src="https://www.google.com/images/srpr/logo11w.png" /> 
</div> 

CSS

div 
{ 
    background-color: red; 
    max-width: 100px; 
    width: 100px; 
    max-height: 100px; 
    height: 100px; 
} 

記住要刪除所有margin和padding表。

+0

心靈更新此琴告訴我你是什麼意思?我嘗試了你所擁有的,但沒有奏效。 http://jsfiddle.net/imthenachoman/kv4zpkuu/1/ – IMTheNachoMan

+0

好的,在小提琴中更新。它需要一個div包裝圖像。 – Heroselohim

+0

http://jsfiddle.net/heroselohim/qbLtaprc/ – Heroselohim

1

問題出在桌子上。該圖像將其高度設置爲其容器的100%,問題在於td從圖像原始高度(190px)獲得高度。我的建議是一起擺脫桌子,只需將圖像放在div中即可完美運行。否則,您需要將td高度設置爲100px。

也許這看起來很接近你想要的東西:http://jsfiddle.net/ericjbasti/kv4zpkuu/6/

div{ 
    width: 100px; 
    height: 100px; 
    background: red; 
} 

div img{ 
    width: 100%; 
    height: 100%; 
    vertical-align:top; 
    border: 4px ridge; 
    box-sizing: border-box; 
} 
+0

謝謝!這有幫助。我實際上是通過將'img'封裝在一個'div'中來工作的。 – IMTheNachoMan