简体   繁体   English

CSS垂直对齐,但不完全

[英]css vertical alignment, but not quite

How do I have a block of text be aligned such that the top of the first line is 50% down from the top of the td? 如何对齐文本块,以使第一行的顶部比td的顶部低50%?

So imagine this, but the top of first row is exactly 50% down. 想象一下,但是第一行的顶部正好下降了50%。

--------------
|             |       
|             | 
|             |
|     xxxx    |
|   xxxxxxxx  |
|  xxxxxxxxxx |
|             |
|             |
--------------

I can do it with a containing div and padding, but I want pure css option that won't break if I change the table height, line height, etc.... 我可以使用包含div和padding的方法来做到这一点,但是我想要纯CSS选项在更改表高度,行高等时不会中断。

Edit -- 编辑-

here is a fiddle that display correctly but uses "hack" css that will break if I change the height and whatnot. 这是一个显示正确的小提琴,但是使用了“ hack” css,如果我更改高度和其他功能,它将中断。

http://jsfiddle.net/5q9K8/ http://jsfiddle.net/5q9K8/

It is showing correctly, but I don't want to have that div. 它显示正确,但我不想拥有该div。 I also don't understand why the padding-top needs to be 90%; 我也不明白为什么填充率必须达到90%; would have thought 50% would do it. 本来以为50%会做到这一点。

Check this fiddle: http://jsfiddle.net/5q9K8/10/ 检查此小提琴: http : //jsfiddle.net/5q9K8/10/

HTML: HTML:

<table>
    <tr>
        <td>
            xxxx<br/>
            xxxxxx<br/>
            xxxxxxx
        </td>
        <td>
            xxxx
        </td>
    </tr>
</table>

Css: CSS:

td {
    height: 200px;
    border: 1px solid black;
    width: 100px;
    text-align: center;

    vertical-align:top;    
}
td:before{
    background: none repeat scroll 0 0 transparent;
    content: "";
    display: block;
    height: 50%;
}

Logic: Using before element to occupy 50% of the TD space so that text starts exactly from the middle & adding vertical align top to keep the text align top from center. 逻辑:使用before元素占据TD空间的50%,以便文本从中间准确开始,并添加垂直对齐顶部以使文本顶部与中心对齐。

Change the margin of the block. 更改块的边距。 Use margin-top:50%; 使用margin-top:50%;
Or try a vertica-align:center; 或者尝试vertica-align:center;
That would align it to the middle, i suppose that's why you want those 50% 那会使它与中间对齐,我想这就是为什么您想要那50%

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

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