简体   繁体   English

如何将文本位于左下角的<td>标签内的图像位置设置在左上角

[英]How to set image position inside <td> tag to be on top-left corner while text is on the left-bottom

I have two pawns and I want to set them on the top-left corner while the text (cell number) is on left-bottom. 我有两个棋子,我想将它们设置在左上角, 文本(单元格编号)位于左下角。

This is what I have: 这就是我所拥有的:
在此输入图像描述

This is what I want to have: 这就是我想要的
在此输入图像描述

CSS : CSS

td {
    width: 80px;
    height: 80px;
    text-align: left;
    vertical-align: bottom;
    border: 1px solid black;
}

.soldiers
{
    width:20px;
    height:20px;
}

HTML : HTML

<tr>
<td class="oddCellBorder" row="0" col="0">57
<img src="Resources/images/player_2.png" class="soldiers">
<img src="Resources/images/player_1.png" class="soldiers">
</td>
<td class="evenCellBorder" row="0" col="1">58</td>
<td class="oddCellBorder" row="0" col="2">59</td>
<td class="evenCellBorder" row="0" col="3">60</td>
<td class="oddCellBorder" row="0" col="4">61</td>
<td class="evenCellBorder" row="0" col="5">62</td>
<td class="oddCellBorder" row="0" col="6">63</td>
<td class="evenCellBorder" row="0" col="7">64</td>
</tr>

Wrap the number in a span and position it at the bottom, and vertical-align: top; 将数字包裹在一个span并将其放在底部,并vertical-align: top; everything else. 其他一切。

 td { position: relative; vertical-align: top; width: 80px; height: 80px; text-align: left; border: 1px solid black; } td span { position: absolute; bottom: 0; } 
 <table> <tr> <td> <span>57</span> <img src="http://placehold.it/20x20/ff6a00/ff6a00" alt="" /> <img src="http://placehold.it/20x20/fb235e/fb235e" alt="" /> </td> </tr> </table> 

You are using way too much code. 您使用的代码太多了。 Using CSS counters you can make this problem completely trivial. 使用CSS计数器可以使这个问题完全无关紧要。

See this demonstration fiddle . 看这个演示小提琴 I generate the board completely in JS, allowing you to use it in an array, and then use CSS counters ( universally supported ) with pseudo elements to put the numbers in there with absolute positioning. 我在JS中完全生成了板,允许您在数组中使用它,然后使用CSS计数器( 普遍支持 )和伪元素将数字放在那里,使用绝对定位。

For the background coloring I use trivial nested nth-child selectors. 对于背景着色,我使用了琐碎的嵌套nth-child选择器。 Then just apply vertical-align:top to the cell and let the flow do the job - I use text content here but images will flow the same as they're both inline content. 然后只需将vertical-align:top应用于单元格并让流程完成工作 - 我在这里使用文本内容,但图像将流动相同,因为它们都是内联内容。

All the CSS needed: 所需的所有CSS:

table {
    counter-reset:number 65;
}
td {
    border:1px solid black;
    counter-increment:number -1;
    width:64px;
    height:64px;
    position:relative;
    vertical-align:top;
}
tr:nth-child(odd) td:nth-child(even), tr:nth-child(even) td:nth-child(odd) {
    background:#aaf;
}
td:after {
    content:counter(number);
    position:absolute;
    bottom:0;
    left:0;
}

Sample linked again here to be sure you don't miss it . 样品在这里再次链接,以确保您不会错过它

A way to do it without extra mark-up 一种没有额外标记的方法

Since you specified the dimensions of the icons (20x20), you can take advantage of that and use the nth-child selector to position the two icons to the top left of the table cell. 由于您指定了图标的尺寸(20x20),因此您可以利用它并使用第nth-child选择器将两个图标放置在表格单元格的左上角。

However, you do need to set the left offset to a specific value based on the dimension given in .soldier . 但是,您需要根据.soldier给出的尺寸将left偏移设置为特定值。

 td { width: 80px; height: 80px; text-align: left; vertical-align: bottom; border: 1px solid black; position: relative; } .soldiers { width: 20px; height: 20px; display: inline-block; margin-right: 5px; position: absolute; top: 0; } img:nth-child(1) { left: 0; } img:nth-child(2) { left: 25px; } 
 <table> <tr> <td class="oddCellBorder" row="0" col="0"> 57 <img src="http://placehold.it/10x10" class="soldiers"> <img src="http://placehold.it/10x10" class="soldiers"> </td> <td class="evenCellBorder" row="0" col="1">58</td> <td class="oddCellBorder" row="0" col="2">59</td> <td class="evenCellBorder" row="0" col="3">60</td> <td class="oddCellBorder" row="0" col="4">61</td> <td class="evenCellBorder" row="0" col="5">62</td> <td class="oddCellBorder" row="0" col="6">63</td> <td class="evenCellBorder" row="0" col="7">64</td> </tr> </table> 

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

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