【问题标题】:How to align one object to the left, and one object in the center (of the entire row) in a single table row?如何在单个表格行中将一个对象与左侧对齐,并将一个对象在(整行的)中心对齐?
【发布时间】:2014-02-14 01:39:10
【问题描述】:

我正在寻找一种方法将一个对象向左对齐,一个对象在单个表格行的中心。与中心对齐的对象应位于表格行的完整中心,而不是第一个对象之后的剩余空间。

以下是相关的代码行(现阶段比较混乱——我今天才开始):

<tr align="center">

<img src="Logo.png" width="100px" height="100px" style="padding:5px" alt="This image cannot be displayed." align="left" style="max-width:100px"/>

<div align="center" style="width:100%"> <img src="Banner.png" style="padding:9px" alt="This image cannot be displayed." align="center" style="clear:both" /> </div>

</tr>

所以它目前的显示方式是,第一个对象(左对齐)出现在我想要它的左、右位置。第二个对象(对齐中心)与剩余空间对齐,而不是与表格行的总空间对齐,这是我的目标。

如果有人知道我如何做到这一点,将不胜感激。

【问题讨论】:

  • 我建议您将&lt;img&gt;&lt;div&gt; 放在另一个&lt;div&gt; 中,然后在&lt;img&gt; 中使用float: left 并使用margin-left css 属性调整布局。
  • 对于有效的 html:tdthtr 元素 see here 中唯一允许的元素
  • 顺便说一句。为避免元素相互推挤,您可以使用position: absolutejsfiddle.net/23ny7

标签: html css html-table alignment row


【解决方案1】:

在 html 上:

<table id="myid" border=1 style="width: 100px">
    <tr> 
        <td> 1 </td>
        <td> a </td>
    </tr>
      <tr> 
        <td> 2 </td>
        <td> b </td>
    </tr>
</table>

在 JS 上(使用 jquery):

$("#myid").find("tr").each(function(){
    tds = $(this).find("td");
    tds.eq(0).attr("align","center");
    tds.eq(1).attr("align","right");
});

在这里测试它: JSFiddle

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 2017-06-08
    • 1970-01-01
    • 2011-04-14
    • 2018-08-22
    • 2021-12-18
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    相关资源
    最近更新 更多