问题
如何在包含 div
的内部对齐图像?
示例
在我的例子中,我需要在 <div>
中使用 class ="frame
垂直居中 <img>
“:
<div class="frame" style="height: 25px;">
<img src="http://jsfiddle.net/img/logo.png" />
</div>
.frame
's height is fixed and image'的身高未知 . 我可以在 .frame
中添加新元素,如果's the only solution. I'm试图在IE≥7,Webkit,Gecko上执行此操作 .
看jsfiddle here
.frame {
height: 25px; /* equals max image height */
line-height: 25px;
width: 160px;
border: 1px solid red;
text-align: center; margin: 1em 0;
}
img {
background: #3A6F9A;
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=250 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=25 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=23 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=21 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=19 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=17 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=15 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=13 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=11 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=9 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=7 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=5 />
</div>
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=3 />
</div>
30 回答
你可以尝试下面的代码
你可以这样做:
演示
http://jsfiddle.net/DZ8vW/1
CSS
javascript
这可能很有用:
参考:http://www.student.oulu.fi/~laurirai/www/css/middle/
我有同样的问题 . 这对我有用:
matejkramny's solution is a good start, but oversized images have a wrong ratio.
这是我的分叉:
演示:https://jsbin.com/lidebapomi/edit?html,css,output
HTML:
CSS:
最好的解决方案是
此外,您可以使用Flexbox来获得正确的结果:
Background image solution 我一起删除了图像元素并将其设置为div的背景,类为
.frame
http://jsfiddle.net/URVKa/2/
这至少适用于IE8,FF6和Chrome13
我检查过,这个解决方案无法缩小大于25px高度的图像 . 有一个名为
background-size
的属性设置了元素的大小,但它是CSS3,它会与IE7要求冲突 .我建议您重做浏览器优先级并设计最佳浏览器,或者如果您想使用此解决方案,请获取一些服务器端代码来调整图像大小
我的解决方案:http://jsfiddle.net/XNAj6/2/
您可以尝试将PI的CSS设置为
display: table-cell; vertical-align: middle;
尝试使用纯css的这个解决方案http://jsfiddle.net/sandeep/4RPFa/72/可能是你的html的主要问题 . 在html中定义c
lass
&image height
时,不要使用引号 .CSS:
EDIT :
当我使用
img
标签时,它会从top
离开3px to 2px
空间 . 现在我减少line-height
&它的工作 . CSS:line-height
属性在不同的浏览器中有不同的render
. 所以;我们必须定义不同的line-height
属性浏览器检查此示例http://jsfiddle.net/sandeep/4be8t/11/
在不同的浏览器中查看关于
line-height
不同的示例input height differences in Firefox and Chrome不确定IE,但在Firefox和Chrome下,如果
div
容器中有img
,则以下css应该有效 . 至少对我来说效果很好:我一直在玩填充物用于中心对齐 . 您需要定义顶级外部容器大小,但内部容器应调整大小,并且可以将填充设置为不同的百分比值 .
jsfiddle
使用
table
和table-cell
方法完成这项工作,特别是因为您也定位了一些旧浏览器,我为您创建了一个片段,您可以运行它并检查结果:想要对齐文本/ Headers 之后的图像并且两者都在div中吗?
请参阅JSfiddle或运行代码段 .
请确保在所有元素(div,img,title等)上都有ID或类 .
对我来说,在所有浏览器上使用此解决方案(对于移动设备,您必须使用以下代码调整代码:@media) .
这个怎么样?
你可以改变
font-size
一个纯粹的CSS解决方案:
http://jsfiddle.net/3MVPM/
CSS:
关键的东西
这适用于现代浏览器(2016年编辑时),如demo on codepen所示
为图像提供类或使用继承来定位需要居中的图像非常重要 . 在这个例子中,我们使用
.frame img {}
,这样只有一个由.frame
类的div包装的图像才会被定位 .用于将图像集中在容器内(可能是徽标),除了这样的一些文本:
基本上你包装图像
http://jsfiddle.net/MBs64/
关键属性是display:table-cell;对于.frame . Div.frame显示为与此内联,因此您需要将其包装在块元素中 .
这适用于FF,Opera,Chrome,Safari和IE8 .
UPDATE
对于IE7,我们需要添加一个css表达式:
我所知道的唯一(也是最好的跨浏览器)方式是在两个元素上使用带有
height: 100%
和vertical-align: middle
的inline-block
帮助器 .所以有一个解决方案:http://jsfiddle.net/kizu/4RPFa/4570/
或者,如果您不想在现代浏览器中有额外的元素并且不介意使用IE表达式,您可以使用伪元素并使用方便的表达式将其添加到IE,每个元素只运行一次,所以没有任何性能问题:
用于IE的
:before
和expression()
的解决方案:http://jsfiddle.net/kizu/4RPFa/4571/这个怎么运作:
inline-block
元素彼此靠近时,你可以将每个元素对齐到另一边,所以使用vertical-align: middle
你会得到这样的东西:当具有固定高度的块(在
px
,em
或其他绝对单位中)时,可以在%
中设置内部块的高度 .因此,在具有固定高度的块中添加
inline-block
和height: 100%
将在其中对齐另一个inline-block
元素(在您的情况下为<img/>
)垂直靠近它 .super easy 解决方案 super easy
我在这里发布的旧JSFiddle链接不再工作,这可能是downvote的原因 . 只是为了这是一个有效的答案我仍然想再次发布jQuery解决方案 . 这适用于应用了
v_align
类的每个元素 . 它将在父级中心垂直,并且在重新调整窗口大小时将重新计算 .Link to the JSFiddle
如果您可以使用像素大小的边距,只需将
font-size: 1px;
添加到.frame
即可 . 但请记住,现在.frame
1em = 1px,这意味着,您还需要设置以像素为单位的边距 .http://jsfiddle.net/feeela/4RPFa/96/
编辑:现在它不再集中在Opera ...
这样您可以垂直居中图像(demo):
对我有用的简单方法:
非常适合谷歌浏览器 . 在不同的浏览器上尝试这个 .
解决方案使用表和表格单元格
有时它应该通过显示为table / table-cell来解决 . 例如快速 Headers 屏幕 . 这也是W3推荐的方式 . 我建议您从W3C.org查看名为Centering things的链接
这里使用的提示是:
绝对定位容器显示为表格
垂直对齐到显示为表格单元格的中心内容
就个人而言,我实际上不同意为此目的使用助手
3线解决方案:
这适用于任何事情 .
来自here .
对于那些登陆这篇文章并且对更现代的解决方案感兴趣并且不需要支持旧版浏览器的人,您可以这样做:
这是一支笔:http://codepen.io/ricardozea/pen/aa0ee8e6021087b6e2460664a0fa3f3e
你可以用这个:
}