简体   繁体   English

将文本完美对齐到表格单元格的顶部

[英]Perfectly align text to the top of table cell

How can i perfectly align text to the top of a table cell? 如何将文本完美地对齐到表格单元格的顶部? By perfectly, I mean that the top of the letters touch the border of the table cell. 完美地说,我的意思是字母的顶部触及表格单元格的边界。

An additional difficulty in my case is that I need to use a large line height (approximately double the font height). 我遇到的另一个困难是,我需要使用较大的行高 (大约是字体高度的两倍)。 As a result, there's is a considerable space between the top of the letters and the cell border because the difference between the font height and the line height is distributed equally to both the top and bottom of the text (so called half-leading ). 结果,由于字体高度和行高之间的差异平均分布在文本的顶部和底部,因此字母的顶部和单元格边框之间存在很大的距离(所谓的Half-lead )。

I've setup a JSFiddle . 我已经设置了JSFiddle

<table>
    <tbody>
        <tr>
            <td>&nbsp;</td>
            <td>NOT PROPERLY ALIGNED TO THE TOP</td>
        </tr>
    </tbody>
</table> 

table {
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
    padding: 0;
    margin: 0;
}
td {
    height: 200px;
    vertical-align: top;
    font-size: 18px;
    line-height: 40px;
    border: solid 1px black;
    background-color: #ddd;    
}

How about wrapping necessary text in some spans and adding negative position/margin, like 如何在一些范围内包装必要的文本并添加否定位置/边距,例如

span {
 position: relative;
 top: -12px;
}

Fiddle 小提琴

But for a good readability, you'd rather don't need to remove space at top (imho) Look, 但是为了获得良好的可读性,您宁愿不需要删除顶部空间(恕我直言), 在此处输入图片说明 the text at left side looks better. 左侧的文本看起来更好。

If you changed your html to: 如果您将html更改为:

<table>
    <tbody>
        <tr>
            <td>&nbsp;</td>
            <td><span>NOT PROPERLY ALIGNED TO THE TOP</span></td>
        </tr>
    </tbody>
</table> 

adding <span> tags Then you can go: 添加<span>标签然后您可以:

td span {
    margin-top: -15px;
}

Hope this helps 希望这可以帮助

Demo 演示

The issue is because of the line-height 问题是由于line-height

You can change the line-height of the first line by using the selector :first-line like 您可以使用选择器:first-line来更改第一行的行高

td:first-line {
    line-height: 10px;    
}

I have a solution but it goes with JS, not pure CSS only. 我有一个解决方案,但它与JS一起使用,而不仅仅是纯CSS。

HTML (added the 'span' tag inside 'td'): HTML(在“ td”中添加了“ span”标签):

<table>
    <tbody>
        <tr>
            <td>&nbsp;</td>
            <td>
                <span id="myText">PROPERLY ALIGNED TO THE TOP</span>
            </td>
        </tr>
    </tbody>
</table> 

JS: JS:

$(document).ready(function(){
    var span       = $("#myText");
    var lineHeight = parseInt(span.css("line-height"));
    var fontSize   = parseInt(span.css("font-size"));
    var shift      = (lineHeight-fontSize)/2;
    var shiftPx    = "-"+shift+"px";

    //alert(lineHeight);
    //alert(fontSize);

    span.css({
        "position":"relative",
        "top":shiftPx
    });
});

Full jsfiddle: http://jsfiddle.net/jondinham/p8g1cx6b/ 完整的jsfiddle: http : //jsfiddle.net/jondinham/p8g1cx6b/

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

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