简体   繁体   English

表格单元格内容:在HTML中将文本的左上角和中间对齐

[英]Table cell content: align text top-left and image in the middle in HTML

I have a table in html. 我在html中有一个表格。 The content of this table is text and an image. 该表的内容是文本和图像。 I would align the text in the top-left corner and the image in the middle (vertical-align). 我将在左上角对齐文本,在中间对齐图像(垂直对齐)。 I tried in this way: 我以这种方式尝试过:

CSS: CSS:

table td {border-collapse: collapse;}
#tabella {border: 1px solid black; text-align: left; vertical-align: top;}
#variante {vertical-align: middle;}

HTML: HTML:

<td id="tabella" style="padding:6px 8px; border-left: 1px solid #eeeeee;">text
<br>
<img id="variante" width="75" border="0" src="www.favetta.com/image.png">
</td>

But in this way I obtain all (text and image) aligned in the top-left corner of the cell. 但是通过这种方式,我获得了所有在单元格左上角对齐的(文本和图像)。 Any suggestion? 有什么建议吗?

Are you doing this for an email? 您是否正在为此发送电子邮件? If so inline styling is fine (although won't work in all email clients so have a default. 如果是这样,则可以使用内联样式(尽管并非在所有电子邮件客户端中都适用,所以请使用默认样式。

If email do something like... 如果电子邮件做类似...

<table>
    <tr>
        <td align="center">
             <table width="100%">
                  <tr>
                        <td align="left">This is text</td>
                  </tr>
             </table>
             <br/><br/>
             <img src="http://s27.postimg.org/fs9k8zewj/cow1.png">
             <br/><br/><br/><br/>
        </td>
    </tr>    
<table>

It looks crude but some browsers and email clients will ignore 'height='. 它看起来很粗糙,但是某些浏览器和电子邮件客户端将忽略“ height =”。 This is purely what Ive found from years of email templating. 这纯粹是Ive从多年的电子邮件模板中发现的。

If not email, try and avoid tables - but if you can't then try something like... 如果没有通过电子邮件发送,请尝试避免使用表格-但是,如果您无法通过电子邮件发送,则尝试...

<table>
    <tr>
        <td class="content">
            This is text
            <img src="http://s27.postimg.org/fs9k8zewj/cow1.png">
        </td>
    </tr>    
<table>

css CSS

table{
    border:1px solid grey;
    width:100%;
}
.content{
    text-align:left;
}
.content img{
    width:75px;
    vertical-align:middle;
    transform: translate(-50%, -50%);
    margin: 100px 50% 50px 50%; 
}

https://jsfiddle.net/qbss1f0t/ https://jsfiddle.net/qbss1f0t/

Here is a simple example: 这是一个简单的示例:

 table{ border:1px solid #000; } table tr{ height:200px; } table td{ width:200px; text-align:center; } .textNode{ text-align:left; padding:0; margin:0; vertical-align:top; } .imgNode img{ width:75px; margin: auto; } 
 <table> <tr> <td class="textNode">This is text</td> <td class="imgNode"><img src="http://s27.postimg.org/fs9k8zewj/cow1.png"></td> </tr> <table> 

Here is a fiddle 是一个小提琴

This should get you to where you want. 这应该使您到达想要的地方。

Side Note : inline styling is not a good practice. 注意 :内联样式不是一个好习惯。

Use this may help you   

<table width="100%">
        <tr>
        <td id="tabella" style="padding:6px 8px; border-left: 1px solid #eeeeee;">text</td>
        <td><img id="variante" width="75" border="0" src="www.favetta.com/image.png"></td>

        </tr>    
    <table>

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

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