[英]Vertical align of text HTML/CSS
I see that many people ask this there, but I didn't found solution for my problem in given answers.我看到很多人在那里问这个问题,但我没有在给定的答案中找到解决我的问题的方法。
I have very simple situation there.我在那里有非常简单的情况。
<div id="tooltipDesc"> <span id="longDesc">Lorem ipsum</span> </div>
I need text to be verticaly aligned to the middle od div.我需要文本垂直对齐到中间的 od div。
This is my recent CSS:这是我最近的 CSS:
#tooltipDesc{
position: absolute;
width: 300px;
height: 80px;
background-color: rgb(255, 255, 255);
color: #6c6c6c;
border: 1px black solid;
}
#longDesc{
color: #6c6c6c;
display: inline-block;
vertical-align: middle;
line-height: normal;
width: 280px;
height: 78px;
}
You can easily achieve this with flexbox, or with absolute text positioning.您可以使用 flexbox 或绝对文本定位轻松实现这一点。 Personally I prefer to use flexbox, with the following:
我个人更喜欢使用 flexbox,如下:
display: flex;
justify-content: center;
align-items: center;
Just add line-height, like this:只需添加行高,如下所示:
#longDesc{
color: #6c6c6c;
display: inline-block;
vertical-align: middle;
line-height: normal;
width: 280px;
height: 78px;
line-height: 78px;
}
If it's just text use:如果只是文本使用:
html: html:
<p class= "example"; > Lorem ipsum </p>
css: css:
.example{
text-align: center;
}
useful link: https://www.w3schools.com/cssref/pr_text_text-align.asp有用的链接: https : //www.w3schools.com/cssref/pr_text_text-align.asp
change in in longDesc div
the value of property text-align
to center
, then add the following position:absolute; top:35%;
在
longDesc div
中将属性text-align
的值更改为center
,然后添加以下position:absolute; top:35%;
position:absolute; top:35%;
finaly make a change in tooltipDesc position:relative
as illustrated below.最后对
tooltipDesc position:relative
进行更改,如下图所示。
#tooltipDesc{
position: relative; /* has been changed */
width: 300px;
height: 80px;
background-color: rgb(255, 255, 255);
color: #6c6c6c;
border: 1px black solid;
}
#longDesc{
position:absolute; /* has been added */
color: #6c6c6c;
display: inline-block;
text-align: center; /* has been changed */
top:35%; /* has been added */
line-height: normal;
width: 280px;
height: 78px;
}
You can use flexbox你可以使用弹性盒
display: flex;
align-items: center;
See in action: https://jsfiddle.net/ex5gww6y/2/见实战: https : //jsfiddle.net/ex5gww6y/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.