我一直試圖在引導媒體對象中使用svg,並發現如果寬度沒有明確設置,它不會佔用最小寬度。例如。與PNG圖像考慮簡單情況:爲什麼svg不能在引導媒體對象中工作?
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="https://www.tutorialspoint.com/bootstrap/images/64.jpg" alt="...">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Media heading</h4>
sample text. This is some sample text. This is some sample text. This is some sample text. This is some s
</div>
</div>
使用PNG圖像具有64PX的缺省寬度從而它表明作爲64PX寬的圖像。現在考慮同樣的例子與SVG IMG:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="http://www.iconninja.com/files/93/412/839/social-online-media-pay-paypay-pal-icon.svg" alt="...">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Media heading</h4>
The random text that shows that the text that was suypposed to be the text.
</div>
</div>
我們可以看到,在第二個例子中,SVG不採取它就像一個PNG或JPG確實最小默認寬度。另一方面,如果我只在body標籤中有兩個圖像標籤,第一個圖像是png,第二個是svg,並且兩個圖像都沒有明確設置寬度,那麼這兩個圖像都將採用其默認寬度。
我的問題是爲什麼在引導媒體對象的SVG採取它的默認寬度?
但是如果我在body標籤中有兩個圖像:'
如果SVG是'display:block'(這是你的情況),瀏覽器將根據默認的寬度/高度或容器的寬度/高度來顯示圖像。由於容器沒有寬度 - svg-image也沒有寬度。 – Dekel
[@ user31782](http://stackoverflow.com/users/3429430/user31782)。很有可能'example.svg'(在你的評論中提到過)明確指出'width'和'height'屬性,因此沒有問題。如果您檢查'img'標記,那麼在原始要點中具有'media-object'類的標記可以看到它沒有顯式的css'width'屬性。 [@Dekel](http://stackoverflow.com/users/5037551/dekel)的答案是解決問題的最佳方法。但是,如果您必須鏈接到外部託管的'.svg',並且無法修復svg資源,則可以將'style =「width:64px; height:64px; float:left;」'內聯到'img'標籤。 – RobC