2013-03-25 64 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像素。是的,浮动将对齐所有的图像。