简体   繁体   English

使用CSS在div中垂直居中放置文本

[英]Position text vertically center in div with CSS

I'd like to position all my text vertically centered within the div. 我想将所有文本垂直放置在div的中心。

Currently 'Chat to us online now' is at the top of the div. 目前,“立即在线与我们聊天”位于div的顶部。

How do I achieve this using my code? 如何使用我的代码实现这一目标?

Fiddle: http://jsfiddle.net/jL5bpmp1/ 小提琴: http : //jsfiddle.net/jL5bpmp1/

 * { margin:0 } .box { -webkit-border-radius: 20px; -moz-border-radius: 20px; border-radius: 20px; background-color:#0f89cf; height:55px; width:100%; padding:5px 0 0 0; margin-bottom:30px } .box img, .box div { float:left; margin-left:10px } .box h1 { color:#fff; font-size:18px } .box h1 span { display:block; font-size:23px } 
 <div class="is-mobile"> <div class="box"> <img src="http://placehold.it/50x50"> <div> <h1>Chat to us online now</h1> </div> </div> <div class="box"> <img src="http://placehold.it/50x50"> <div> <h1>Call the Helpline <span>101 2333 9302</span></h1> </div> </div> <div class="box"> <img src="http://placehold.it/50x50"> <div> <h1>Make a General Enquiry <span>101 2333 9303</span></h1> </div> </div> </div> 

I suggest to use display:inline-block instead of float:left , then you can set up the vertical align value easily. 我建议使用display:inline-block而不是float:left ,然后可以轻松设置垂直对齐值。

.box img, .box div {
    display:inline-block;
    vertical-align:middle;
}

jsfiddle 的jsfiddle

You can use this common vertical centering method: 您可以使用以下常见的垂直居中方法:

.box div {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

 * { margin:0 } .box { -webkit-border-radius: 20px; -moz-border-radius: 20px; border-radius: 20px; background-color:#0f89cf; height:55px; width:100%; padding:5px 0 0 0; margin-bottom:30px } .box img, .box div { float:left; margin-left:10px } .box h1 { color:#fff; font-size:18px } .box h1 span { display:block; font-size:23px } .box div { position: relative; top: 50%; transform: translateY(-50%); } 
 <div class="is-mobile"> <div class="box"> <img src="http://placehold.it/50x50"> <div> <h1>Chat to us online now</h1> </div> </div> <div class="box"> <img src="http://placehold.it/50x50"> <div> <h1>Call the Helpline <span>101 2333 9302</span></h1> </div> </div> <div class="box"> <img src="http://placehold.it/50x50"> <div> <h1>Make a General Enquiry <span>101 2333 9303</span></h1> </div> </div> </div> 

If your browser support is IE10+ you can use flex-box. 如果您的浏览器支持IE10 +,则可以使用flex-box。 You just need to add 2 lines: 您只需要添加两行:

.box {
    display: flex;
    align-items: center;
    padding: 0;
}

I also added padding: 0; 我还添加了padding: 0; as you don't need that now you're using flex-box. 因为您现在不需要它,所以您正在使用flex-box。 Welcome to the future. 欢迎来到未来。 The best place to learn about flexbox is here: 了解flexbox的最佳地点在这里:

https://css-tricks.com/snippets/css/a-guide-to-flexbox/ https://css-tricks.com/snippets/css/a-guide-to-flexbox/

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

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