简体   繁体   English

在CSS表中对齐图像和文本

[英]Aligning Image and Text in CSS Table

I have seen several CSS alignment tips, but none relating to a table. 我已经看到了几个CSS对齐技巧,但没有一个与表格有关。

My code for the table properties are as follows: 我的表属性代码如下:

    table.pic {
    border-spacing: 1;
    }
    table.pic td {
    padding: 5px;
    border: 4px solid #cccccc;
    }
    table.pic tr:nth-child(odd) {
    background-color: #f2f2f2;
        }

    table.pic div {
        display:table-cell; 
        vertical-align:middle;
    }

This is my code for the first table row: 这是我在第一行表格中的代码:

<table class="pic" style="width: 100%;">
<tbody>
<tr>
<td><img src="/sites/default/files/images/img.jpg" alt="" width="146" height="218" /></td>
<td>
<p>blahblah</p>
<p>poodle</p>
<p>noodle</p>
</td>
</tr>
</tbody>
</table>

No matter what change I try to make, the text still ends up on the cell to the right, but vertically it starts on the bottom line of the image. 不管我尝试进行什么更改,文本仍将最终显示在右侧的单元格上,但垂直方向是从图像的底行开始。 Anyone know what my issue is? 有人知道我的问题是什么吗?

On jsfiddle the text is not align with the bottom edge of the picture, it's vertically centered : http://jsfiddle.net/u3AW2/ 在jsfiddle上,文本未与图片的底部边缘对齐,而是垂直居中: http : //jsfiddle.net/u3AW2/

Maybe you have overwrites somewhere. 也许您在某个地方覆盖了。

The text is also vertically centered with : 文本也垂直居中:

   table.pic td{
        display:table-cell; 
        vertical-align:middle;
    }
table.pic div {
        display:table-cell; 
        vertical-align:middle;
    }

aligns div elements, which are children of tables with a class of pic. 对齐div元素,这些元素是具有一类pic的表的子级。 I don't see the div in your html. 我在您的html中看不到div。

I assume you want 我想你要

table.pic td {
            display:table-cell; 
            vertical-align:middle;
       }

to align all td elements of the table to vertical center. 使表格的所有td元素与垂直中心对齐。

Updade: Updade:

to attempt to override any theme rules you can get more selective 尝试覆盖任何主题规则,您可以更具选择性

table.pic {
border-spacing: 1;
}
table.pic td {
padding: 5px;
border: 4px solid #cccccc;
}
table.pic tr:nth-child(odd) {
background-color: #f2f2f2;
    }

/* use more selectivity */
html body table.pic tr td {
    display:table-cell !important; 
    vertical-align:middle !important;
}

I had exactly the same issue and found it was because I was using a CSS Reset. 我遇到了完全相同的问题,发现这是因为我使用的是CSS Reset。 For some reason this was conflicting with my style sheet. 由于某种原因,这与我的样式表冲突。 I was specifically using the one on Meyerweb. 我专门在Meyerweb上使用它。 Are you using this reset? 您是否正在使用此重置? If so, try changing the first rule as follows: 如果是这样,请尝试如下更改第一条规则:

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend, caption, tfoot, thead,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

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

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