【问题标题】:How to vertically align first lines of text in table cell?如何垂直对齐表格单元格中的第一行文本?
【发布时间】:2014-08-10 19:41:50
【问题描述】:

我有这种情况,第一个表格单元格包含“名称”,第二个单元格包含一些文本。文本可能有嵌入的图像。当图像出现在第一行时,它会向下推整个第一行。它使第一个文本行不再与第一个单元格文本对齐。我想让两个表格单元格的第一行对齐。

<table>
    <tr>
        <td class="align">one</td>
        <td>two <img src="http://www.emofaces.com/en/emoticons/v/vampire-emoticon-before-lunch.gif" /> three three three three three three three three three three three three three three three three three three three</td>
    </tr>
</table>

http://jsfiddle.net/gdx0qhcc/31/

在下面的小提琴中,目标是将单词“one”与单词“two”对齐。我知道我可以垂直对齐:顶部;在图像上,但它会将第二个单元格的文本拉起,看起来很糟糕,因此它不是一个选项。任何类型的解决方案都可以真正起作用,无论是 css/javascript/webkit-only 解决方案。

编辑: 请注意,图像的高度和宽度可能会有所不同。我还更新了图像对齐为“中间”的小提琴。它并没有真正改变情况。

编辑2: 最终解决方案“破解”http://jsfiddle.net/Lqcx2vfg/ 谢谢大家

【问题讨论】:

  • 嗯,我不确定我是否理解这个问题 - 如果两个单元格中的文本都对齐到底部是否可以(例如:jsfiddle.net/gdx0qhcc/2)?还是?
  • 不是真的没有。它就像 IM 聊天窗口,其中“一个”是发件人的姓名,第二个单元格是消息。名称应该在消息的顶部,但与第一行对齐。我在想,如果我们能以某种方式只获取第一行中的文本,那么可以检查该行上图像的高度,并且可以使用 javascript 手动调整每行上的第一个单元格。但我也不知道如何在第一行获取文本。

标签: javascript html css


【解决方案1】:

HTML:

<table>
<tr>
    <td class="align">one</td>
    <td>two <img src="http://www.emofaces.com/en/emoticons/v/vampire-emoticon-before-lunch.gif" /> three three three three three three three three three three three three three three three three three three three</td>
</tr>

CSS:

*{
    margin: 0;
    padding: 0;
}
table, tr, td {
    border: solid 1px red;
    line-height: 16px;
}
.align {
     vertical-align: top; 
}

JavaScript:

$(document).ready(function() {
$('.align').css('padding-top',(parseInt($('img').css('height'))-16)+"px");

//16 是行高 });

【讨论】:

  • 嘿伙计,它非常接近,谢谢!唯一的问题是,当图像在第二行时,对齐就会出错。但我很确定有 js 解决方法。摆弄它atm。
  • 嘿,我想出了其余的。非常感谢你,这是最后的小提琴jsfiddle.net/Lqcx2vfg
【解决方案2】:

你可以把图片放到第二行:修改HTML和CSS如下图:

HTML

<table>
    <tr>
        <td class="align">one</td>
        <td>two three three three three three three three three three three three three three three three three three three three</td>
    </tr>
    <tr>
        <td style="border-top:none;">
        </td>
        <td>
            <img src="http://www.emofaces.com/en/emoticons/v/vampire-emoticon-before-lunch.gif" /> 
        </td>
    </tr>
</table>

CSS

table, tr, td {
    border: solid 1px red;
}

.align {
    vertical-align: top;
    border-bottom:none;
}

问候,

【讨论】:

  • 图片应该在文本的中间。现在它在完全错误的地方。
【解决方案3】:

另一个想法......我认为你想要的需要 JavaScript,是的......

span {
    display: inline-block;
    vertical-align: middle;
    height: 10px; width: 50px;
}
span img {
    float: left;
    position:absolute;
    margin-top: -25px; /* Try with -5px. */
}

JSFIDDLE

【讨论】:

    【解决方案4】:

    你可以使用 span 元素:

    <table>
        <tr>
            <td class="align"><span class="align">one</span></td>
            <td><span class="align">two </span><img valing src="http://www.emofaces.com/en/emoticons/v/vampire-emoticon-before-lunch.gif" /><span class="align"> three three three three three three three three three three three three three three three three three three three</span></td>
        </tr>
    </table>
    

    【讨论】:

    • 带垂直对齐:顶部;它将文本推到顶部,与使用 vertical-align: top; 相同。在图像上。但我想将第一个单元格的文本向下推一点,而不是将第二个单元格的文本向上推。
    • 像这样:&lt;table&gt; &lt;tr&gt; &lt;td style="vertical-align: bottom"&gt;one&lt;/td&gt; &lt;td&gt;two &lt;img src="http://www.emofaces.com/en/emoticons/v/vampire-emoticon-before-lunch.gif" /&gt; three three three three three three three three three three three three three three three three three three three&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;
    • 当文本单元格中有多行时,第一个单元格将与底线对齐,而不是与顶线对齐。这并不像看起来那么简单。
    • 我认为你应该更好地陈述你的问题,因为在每个答案之后都有一个“但是在这个新场景中......”。
    【解决方案5】:

    在您的具体问题中,这将起作用

    .align {
        vertical-align: center;
        padding-top:15px;
    }
    

    DEMO

    【讨论】:

    • 没有vertical-align: center; 这样的东西 - 所以该规则什么都不做...删除该规则并查看结果是否相同
    • 但是图像可以是不同的大小,然后它会再次错位。
    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多