2013-03-25 67 views
0

在圖像庫的這個例子中:HTML img行爲 - 爲什麼浮動時沒有邊距?

http://www.w3schools.com/css/tryit.asp?filename=trycss_float_elements

<head> 
<style> 
.thumbnail 
{ 
    float:left; 
    width:110px; 
    height:90px; 
    margin:5px; 
} 
</style> 
</head> 

<body> 
<h3>Image Gallery</h3> 
<p>Try resizing the window to see what happens when the images does not have enough room.</p> 
<img class="thumbnail" src="klematis3_small.jpg" width="116" height="90"> 
<img class="thumbnail" src="klematis4_small.jpg" width="120" height="90"> 
</body> 
</html> 

如果寬度從.thumbnail類然後圖像被放在旁邊彼此而不之間的空間中除去。但對於沒有浮動的常規圖像元素,自動出現一些邊距。我需要了解一般行爲。浮動的行爲是它們彼此相鄰放置。爲什麼影響圖像元素的邊距?

感謝您的任何幫助。

回答

1

只有block elements可以有餘量。

地址:

display:block到你的CSS

+0

我使用display:block作爲.thumbnail。然而沒有保證金。 :( – 2013-03-25 18:26:44

+0

Baloney。將其更改爲50px,您將看到不同之處。 – 2013-03-25 18:35:08

0

每個形象都有自己的寬度尺寸。如果你沒有在課堂上設置寬度尺寸,那麼圖像將不會很好地對齊。如果您在類中聲明寬度,就像爲該圖像創建一個容器,不管該圖像的寬度是多少,它都將相應地調整爲110像素。是的,浮動將對齊所有的圖像。