css - 对齐 2 个图像,一个向右对齐另一个向左对齐 div

标签 css html

我的网页中有一个包含 2 张图片的图片。我希望一张图片向左对齐,另一张图片向右对齐。

JsFiddle

这是我的 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; 将它们定位到左下角和右下角

Demo

<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/

相关文章:

javascript - PowerBI Embedded 垂直滚动条不可见

css - 连接字符串和 var less css

java.util.Date 来自<输入类型 ="datetime-local"/>

javascript - CSS/HTML 通过 Javascript 动态调整 Iframe 的大小

html - Bootstrap 3 并排两个 50% 宽度的 div

javascript - 使用 jQuery 特色 slider 更改目标 DIV 的类

Css3 动画在 Firefox 版本 44 中不起作用

javascript - 在文本字段中键入时设置文本格式

javascript - jQuery:如何使用 jquery 勾选复选框而不执行它的绑定(bind)事件?

javascript - 为什么 onclick 事件不影响 CSS3 animation-duration 属性?