我的网页中有一个包含 2 张图片的图片。我希望一张图片向左对齐,另一张图片向右对齐。
这是我的 HTML:
<div class="header">
<img id ="ttl" src="Home_files/images/ttl.png">
<img id ="se" src="Home_files/images/se.png">
</div>
和 CSS:
.header {
position: relative;
top: 0%;
height: 20%;
}
/*Header CSS*/
img#ttl {
position: relative;
top:50%;
height: 50%;
left: 0px;
}
img#se {
position: relative;
top:60%;
height:30%;
vertical-align:right;
margin-right: 2%;
}
PS:我试过 float:right;
。它适用于 Chrome 和 FF,但不适用于 IE。
当然,这个 div 有一个父 div。但我认为这不会成为问题。
最佳答案
您可以将图像包装在相对位置容器中,并使用 position: absolute;
将它们定位到左下角和右下角
<div class="wrap">
<img src="http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif" />
<img src="http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif" />
</div>
div.wrap {
width: 600px;
border: 1px solid #f00;
height: 300px;
position: relative;
}
.wrap img {
border: 1px solid blue;
position: absolute;
bottom: 0;
}
.wrap img:nth-of-type(1) {
left: 0;
}
.wrap img:nth-of-type(2) {
right: 0;
}
Note: Am using
nth-of-type
to select images so that I don't have to declare classes for each image, if you want to support older browsers, you need to add class for each image and replace:nth-of-type
with those classes
关于css - 对齐 2 个图像,一个向右对齐另一个向左对齐 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16603884/