【问题标题】:Float span right in table using spans as columns使用跨度作为列在表中浮动跨度
【发布时间】:2012-09-30 01:50:01
【问题描述】:

由于一些nuances with jQuery's Sortable,我在 HTML 中显示了一个表格元素,但使用 's 来创建列而不是 's(在this fiddle 下方和中):

<style>        
  /* Title row for an action/item */
  .item-row {
    display: table-row;
  }

  /* Keep icon columns narrow */
  .icon_column { 
    display: table-cell;
    width: 20px;
  }

  /* Item columns that are not icons */
  .name_column {
    display: table-cell;
  }    

  .extra_column {
    display: table-cell;
    float: right;
  }
</style>
<table class='table table-condensed'>
  <thead>
    <tr class='table_header'>
      <th>
        <div class='item-row'>
          <span class='icon_column'>*</span>
          <span class='icon_column'>*</span>
          <span class='icon_column'>*</span>
          <span class='name_column'>Name</span>
          <span class='extra_column'>Date</span>
        </div>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <div class='item_row'>
          <span class='icon_column'>*</span>
          <span class='icon_column'>*</span>
          <span class='icon_column'>*</span>
          <span class='name_column'>Name 1</span>
          <span class='extra_column'>Date 1</span>
        </div>
      </td>
    </tr>
    <tr>
      <td>
        <div class='item_row'>
          <span class='icon_column'>*</span>
          <span class='icon_column'>*</span>
          <span class='icon_column'>*</span>
          <span class='name_column'>Row 2 Name 2</span>
          <span class='extra_column'>Date 2</span>
        </div>
      </td>
    </tr>
    <tr>
      <td>
        <div class='item_row'>
          <span class='icon_column'>*</span>
          <span class='icon_column'>*</span>
          <span class='icon_column'>*</span>
          <span class='name_column'>Name 3</span>
          <span class='extra_column'>Date 3</span>
        </div>
      </td>
    </tr>
  </tbody>
</table>

我希望每个项目行显示为一行,其中 extra_column 一直浮动到右侧。 extra_column 似乎在右侧,但整个 item-row 似乎默认为两行。

如何更正此 CSS? ​

【问题讨论】:

  • “一直向右浮动”,你的意思是 Luis 提议的吗?它(文本)向右对齐。它实际上也应该看起来像一张桌子吗?换句话说,一行中的最后一个“单元格”是否应该相互对齐?
  • 抱歉,item-row 与 item_row 位只是我在这段代码中的疏忽
  • @TweeZz,是的,我希望它像一张真正的桌子一样排列。最后一个单元格应垂直对齐。

标签: html css html-table


【解决方案1】:

这是你需要的吗?

http://jsfiddle.net/QgpRY/2/

CSS 标记:

.extra_column {
    display: table-cell;
    text-align: right; /*use this instead*/
    /*float: right;*/ /*this was the problem*/
  }

正如您在this last example 中看到的,它向右对齐。


更新

这应该是您所要求的一个很好的解决方案:

http://jsfiddle.net/QgpRY/12/

  /* Keep icon columns narrow */
  .icon_column { 
    display: table-cell;
    width: 20px !important;
  }

  /* Item columns that are not icons */
  .name_column {
    display: table-cell;
    width:81% !important;
  }    

  .extra_column {
    display: table-cell;
    text-align: right;
    width: 50px !important;
  }

如果你能全部改成&lt;div&gt;'s会容易很多

希望对你有帮助!

【讨论】:

  • 这很好,但我希望每个 item-row 的宽度相同,并且 extra_column 与其他 extra_column 垂直对齐。
  • 好的,我们可能已经接近了。一些 cmets:我希望 icon_columns 为 20px 宽。假设extra_column 应该是50px。并且整个项目行应该是区域/屏幕宽度的 100%。这可能吗?
  • 这很好,但是如果页面足够宽,硬编码 81% 仍然会在右侧留出不需要的空间
【解决方案2】:

this 是你要找的吗?

我基本上将两组跨度包装在另一个跨度中。第一组获得浮动:左,第二组获得浮动:右。

CSS:

.left { float:left; }
.right { float:right;}

HTML:

<div class='item-row'>
  <span class='left'>
    <span class='icon_column'>*</span>
    <span class='icon_column'>*</span>
    <span class='icon_column'>*</span>
    <span class='name_column'>Name</span>
  </span>
  <span class='right'>
    <span class='extra_column'>Date</span>
  </span>
</div>

我必须补充一点,当 1 行的内容扩展它所获得的空间(宽度)时,就会出错。我不确定如何实现 :)

[更新]
通过giving the name_column a fixed width(比如150px),可以摆脱上面提到的问题。当最后一列数据太多时,它仍然会出错。这可能也可以通过给定一个固定宽度来解决。 如果您希望它跨越“页面”,则可以摆脱 div 包装器(“table_container”)。

【讨论】:

  • 有趣的解决方案。是否可以在不硬编码 name_column 的宽度的情况下使 item-row 成为屏幕的整个宽度?
  • 这是最接近我想要的功能。我可能会在 name_column 上使用另一个 div 并溢出:隐藏以截断太长的文本。
猜你喜欢
  • 2015-10-29
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多