简体   繁体   English

在div中垂直对齐图像和文本

[英]Aligning images and text vertically inside div

Is the only way of aligning images and text vertically within a div possible via absolute/relative positioning? 是否可以通过绝对/相对定位在div内垂直对齐图像和文本的唯一方法?

I need to do that, and I am trying to use as few classes/hacks as possible. 我需要这样做,并且我正在尝试使用尽可能少的类/ hack。

Is the only other way to do like display:table-cell; 像display:table-cell;一样是唯一的其他方法。 vertical-align:middle;? vertical-align:middle ;?

Example code: 示例代码:

http://jsfiddle.net/ndreckshage/UYxXG/ http://jsfiddle.net/ndreckshage/UYxXG/

One way to do this is to use display: table-cell with vertical-align: middle . 一种方法是使用display:table-cellvertical-align:middle

See an example: 看一个例子:

http://jsfiddle.net/UYxXG/3/ http://jsfiddle.net/UYxXG/3/

One problem with this method is, is that the property "table-cell" is not supported by IE7. 这种方法的一个问题是,IE7不支持属性“ table-cell”。

Posted above in comment, but this code works for anyone else who is interested ---> 在上面发表评论,但此代码适用于其他有兴趣的人--->

http://jsfiddle.net/ndreckshage/7SV9f/ http://jsfiddle.net/ndreckshage/7SV9f/

Vertical align is one way, absolute/relative positioning is another, but there is also the html align attribute. 垂直对齐是一种方式,绝对/相对定位是另一种方式,但是也有html align属性。 http://www.w3schools.com/html/tryit.asp?filename=tryhtml_image_align http://www.w3schools.com/html/tryit.asp?filename=tryhtml_image_align

In my opinion adding to your css with vertical-align is the smartest way. 我认为,使用vertical-align添加到css是最聪明的方法。

.test
vertical-align: middle;

Chris Coyier has a great article on CSS Tricks that shows how to center single and multiline text vertically with css. Chris Coyier在CSS Tricks上有一篇很棒的文章,展示了如何使用CSS在垂直方向上将单行和多行文本居中。 http://css-tricks.com/vertically-center-multi-lined-text/ http://css-tricks.com/vertically-center-multi-lined-text/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM