简体   繁体   English

垂直文本对齐在div中不起作用

[英]vertical text alignment wont work within a div

I'm trying to get the text in the div to be centered and in the middle but cant get it to move to the middle. 我正在尝试使div中的文本居中并在中间,但无法使其移动到中间。 I've tried putting it within a span but still to no avail. 我尝试将其放在一个范围内,但仍然无济于事。

 #head { Border-radius:6px; width:80%; height:50px; background-color:#F43434; color:white; margin: auto; text-align:center; margin-top:50px; vertical-align:middle; } span { display: inline-block; vertical-align: middle; } 
 <div id="head"> <span><b><i>Kilbride Classic Cuisine</i></b></span> </div> 

here is what you want 这就是你想要的

html html

<div id="head">
<span><b><i>Kilbride Classic Cuisine</i></b></span>
</div>

css 的CSS

#head {
    Border-radius:6px;
    width:80%;
    height:50px;
    background-color:#F43434;
    color:white;
    margin: auto;
    text-align:center;
    margin-top:50px;
    vertical-align:middle;
    border:1px solid;
}

span {
    display: inline-block;
    vertical-align: middle;
    line-height:50px;
}

here is demo: 这是演示:

https://jsfiddle.net/2Lqt039x/1/ https://jsfiddle.net/2Lqt039x/1/

Try to apply text-align: center; 尝试应用text-align: center; and vertical-align: middle; vertical-align: middle; to your text. 到您的文字。 Set line-height to the height of your head div . line-height设置为head div的高度。

 #head { Border-radius:6px; width:80%; height:50px; background-color:#F43434; color:white; margin: auto; margin-top:50px; } p { text-align: center; vertical-align: middle; line-height: 50px; } 
 <div id="head"> <p> <b><i>Kilbride Classic Cuisine</i></b> </p> </div> 

All you need is two lines with flexbox . 您需要做的是用flexbox两行flexbox display:flex on the parent and margin:auto on the child. display:flex在父级上, margin:auto在子级上。 The child will be centered both vertically and horizontally regardless of parent size. 无论父级大小如何,孩子都会在垂直和水平方向上居中。

Guide to Flexbox Flexbox指南

 #head { margin: auto; margin-top:50px; Border-radius:6px; border:1px solid; background-color:#F43434; color:white; width:80%; height:50px; } 
  <div id = 'head' style ='display:flex'> <div style = 'margin:auto'>Kilbride ... </div> </div> 

There are a number of different methods for vertically centring text and elements. 有多种方法可以使文本和元素垂直居中。 In this case, you can achieve it, without the need for any extra markup, by using flexbox . 在这种情况下,您可以使用flexbox来实现它,而无需任何额外的标记。 The advantage of using this method rather than setting a line-height equal to the height if the div is that the text can break onto multiple lines and still be centred. 如果div是文本可以分成多行并且仍然居中,则使用此方法而不是将line-height设置为等于高度的优点。

 #head { align-items:center; background-color:#f43434; border-radius:6px; color:#fff; display:flex; font-style:italic; font-weight:bold; height:50px; justify-content:center; margin:50px auto 0; text-align:center; width:80%; } 
 <div id="head">Kilbride Classic Cuisine</div> 

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

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