center vertically the content of a div ( not by line-height )

只愿长相守 提交于 2019-12-02 12:53:30

You have to add a second div to achieve this.

<div draggable="false" id="coffee" style="position: absolute; overflow: auto; text-align: left; font-size: 23px; color: rgb(26, 66, 108); font-family: roboto, Helvetica; font-style: normal; font-weight: bold; -webkit-transition: none; transition: none; left: 0px; top: 67.125px; height: 234.93749999999997px; opacity: 1;">
    <div class="inner">
    Free coffee for all the people who visit my restaurant
    </div>
</div>

#coffee {
    display: table;
    width: 300px;
    background-color: red;
}
#coffee .inner{
    vertical-align: middle;   
    display: table-cell;
}

Example: http://jsfiddle.net/2Mb39/12/

Is this what you're trying to achieve?

HTML

<div draggable="false" id="coffee" style="position: absolute; overflow: auto; vertical-align: middle; font-size: 23px; color: rgb(26, 66, 108); font-family: roboto, Helvetica; font-style: normal; font-weight: bold; -webkit-transition: none; transition: none; left: 0px; top: 67px; height: 235px; opacity: 1;"><span class="marqtext">Free coffee for all the people who visit my restaurant</span></div>

CSS

#coffee {
  line-height: 235px;
  width: 300px;
}
.marqtext {
  display:table-cell;
  position:relative;
  top:180px;
  text-align:center;
}

If your inner div has a flexible height you can center it using CSS transforms:

#coffee {
    width: 300px;
    position:relative;
}
#coffee > div {
    background:#ddffff;
    position:absolute;
    top:50%;
    max-height:100%;
    -webkit-transform:translateY(-50%);
    -moz-transform:translateY(-50%);
    transform:translateY(-50%);
}

http://jsfiddle.net/2Mb39/16/

Line-height will give a vertical height to all the lines in the para, since you have absolute positioned div and there is a paragraph inside, you'll need a child div to align them vertically!

DEMO

CSS :

#coffee {
    width: 300px;
}
#coffee > .mid {
    margin-top:25%;
    text-align:center
}

HTML

<div draggable="false" id="coffee" style="position: absolute; overflow: auto; text-align: left; font-size: 23px; color: rgb(26, 66, 108); font-family: roboto, Helvetica; font-style: normal; font-weight: bold; -webkit-transition: none; transition: none; left: 0px; top: 67.125px; height: 234.93749999999997px; opacity: 1;border:1px solid #000">
    <div class="mid">
    Free coffee for all the people who visit my restaurant
    </div>
</div>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!