Html 使用<;部门>;?

Html 使用<;部门>;?,html,css,Html,Css,所以,我尝试对齐四个图像,两个在顶部,两个在底部。这四个部分一起构成一张地图。更清楚的是:我把一张地图的图片切成四等份,用PS表示,现在我想用HTML代码把这些部分放在一起 <div id="container"> <a href="topleft.html"><img src="topleft.png" height="50%"></a> <a href=""><img src="topright.png" height="50

所以,我尝试对齐四个图像,两个在顶部,两个在底部。这四个部分一起构成一张地图。更清楚的是:我把一张地图的图片切成四等份,用PS表示,现在我想用HTML代码把这些部分放在一起

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
代码如下所示

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
HTML:

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>

当我的图像周围没有div时,大小(
height=“50%”
)是正确的,并且它们的浮动方式是:左对齐(除了我使用的
br style=“clear:both.”/
。但当我将它们放在div中时,“我的大小”属性不起作用,并且每张图片后都有一个换行符,因此它们彼此堆叠在一起。

图像是内联元素,就像文本一样。div是占据浏览器全宽的块级元素。您将块级元素包裹在内联元素周围。t这就是为什么你的内联图像不再像你希望的那样工作。即使是浮动也不能解决这个问题,因为图像是浮动的,但是div占据了整个宽度

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
解决此问题的一种方法是将
display:inline;
display:inline block
设置为您的div,或者您可以浮动div。

您可以尝试以下方法:

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
img {
        display:inline;
        float:left;
        padding:0px;
        margin: 0px;
        position: relative; 

    }  

#container {
    border: 10px solid black;
    height: 2000px;
    width:1500px;
    position: relative;
    margin: 20px auto;
    padding-top: 25px;
    padding-right: 25px;
    padding-bottom: 25px;
    padding-left: 25px; 
    display:inline;
}

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
但当我把它们放在div中时,“我的大小”属性不起作用 每张照片后面都有一个换行符,所以它们都叠在上面 彼此的

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
当您将图像放入
div
中时,它们将被包装在一个
div
中,默认情况下,该值为
div
。块元素使用其容器的所有可用宽度。因此
div
s将垂直堆叠

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
HTML
height
属性的工作方式与预期不同,因为使用百分比值时,不会根据图像大小计算
height

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
从:

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
请注意,以百分比表示的长度基于水平方向 或当前可用的垂直空间,而不是建筑的自然尺寸 图像、对象或小程序

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
要使用现代(CSS3)布局技术快速、高效地实现布局,请尝试以下方法:

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
HTML

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>
<div id="container">
    <a href=""><img src="http://i.imgur.com/60PVLis.png" alt=""></a>
    <a href=""><img src="http://i.imgur.com/60PVLis.png" alt=""></a>
    <a href=""><img src="http://i.imgur.com/60PVLis.png" alt=""></a>
    <a href=""><img src="http://i.imgur.com/60PVLis.png" alt=""></a>
</div>

<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>

请注意,所有主要浏览器都支持flexbox。一些最新的浏览器版本,如Safari 8和IE10,需要。要快速添加所需的所有前缀,请在左侧面板中发布CSS:。

始终从
jsfiddle.net
发送演示,以帮助您快速获得此网站上有用的答案。没有ob结扎术。这只是提高高质量内容的一种方法。
<div id="container">
<a href="topleft.html"><img src="topleft.png" height="50%"></a>
<a href=""><img src="topright.png" height="50%"></a>
<br style="clear:both"/>
<a href=""><img src="bottomleft.png" height="50%"></a>
<a href=""><img src="bottomright.png" height="50%"></a>
</div>

</body>