简体   繁体   English

浮动div的垂直对齐

[英]Vertical alignment of a floated div

I'm trying to center vertically a floated div, here is the JSFiddle 我试图垂直居中浮动div, 这是JSFiddle

 .wrapper{ width:250px; background-color:red; } #SmileImg{ height:100px; width:auto; } .textwrapper{ float:right; } 
 <div class="wrapper"> <img src="http://img.xcitefun.net/users/2010/02/147370,xcitefun-smile-4.png" id="SmileImg" /> <div class="textwrapper"> <p>Please Smile!</p> </div> </div> 

I do not know how to put the text "Please Smile" vertically centered. 我不知道如何将“请微笑”文本垂直居中。

Hope someone will help me. 希望有人能帮助我。 Please be patient I am not an expert! 请耐心,我不是专家! Thank you in advance! 先感谢您!

Floating elements cannot be vertically aligned, you should either use inline blocks, or set line height equal to image height for your text. 浮动元素不能垂直对齐,您应该使用嵌入式块,或者将行高设置为等于文本图像高度。 One more way is to use "display: table-cell" 另一种方法是使用“显示:表格单元”

example with inline blocks: 内联块的示例:

 .wrapper{ width:250px; background-color:red; } #SmileImg{ height:100px; display: inline-block; vertical-align: middle; } .textwrapper{ display: inline-block; } 
 <div class="wrapper"> <img src="http://img.xcitefun.net/users/2010/02/147370,xcitefun-smile-4.png" id="SmileImg" /> <div class="textwrapper"> <p>Please Smile!</p> </div> </div> 

use display: table 使用display: table

 *{ padding: 0; margin: 0; box-sizing: border-box; } .wrapper{ width:250px; background-color:red; display: table; } .item{ display: table-cell; vertical-align: middle; } #SmileImg{ height:100px; width:auto; } 
 <div class="wrapper"> <div class="item"> <img src="http://img.xcitefun.net/users/2010/02/147370,xcitefun-smile-4.png" id="SmileImg" /> </div> <div class="item"> <p>Please Smile!</p> </div> </div> 

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

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