【问题标题】:How to position some elements on top of table-cell and some in the bottom?如何将一些元素定位在表格单元格的顶部和底部?
【发布时间】:2012-11-10 06:40:26
【问题描述】:

我需要在表格单元格的顶角放置一个元素,并且文本应该堆叠在底部。我使用 vertical-align: bottom 将所有文本向下移动,但现在我遇到了顶部元素的问题,因为 table-cell 不支持相对定位......任何想法如何做到这一点?

<div style = "display: table-cell; vertical-align: bottom">
    <div class = "top"> Top corner</div>
    <h2> some text in the bottom </h2>
    <h3> some more text in the bottom </h3>
</div>

编辑:它应该看起来像这样

+-----------+   +-----------+  +------------+
|top text   |   |top text   |  |top text    |
|           |   |           |  |            |
|some long  |   |           |  |            |
|text       |   |           |  |            |
|more long  |   |           |  |            |
|text       |   |           |  |            |
|end of     |   |           |  |some text   |
|text       |   |text       |  |text        |
|<button>   |   |<button>   |  |<button>    |
+-----------+   +-----------+  +------------+

这一切都包含在一个带有 display:table 的 div 中。 我想在这里使用 display:table-cell 的原因是保持这些列的高度相同,并且仍然可以灵活地适应列中不同长度的文本。

【问题讨论】:

  • 您能描述一下您希望它看起来多一点吗?你包装 div 是否应用了任何样式(内联样式除外)。有固定高度吗?您希望 .top div 位于哪个顶角?也许图像模型可能有助于获得一些好的答案。
  • 你想要这样的东西吗? jsfiddle.net/Kse3g/1 只是不通过在 css 中使用显式高度来伪造结果?
  • 无法加载该链接,抱歉!我在描述中添加了一个小“图片”:)
  • 我认为这不可能在使用 display: table-cell 的父元素内有一个绝对定位的元素,具有动态灵活的高度。

标签: html css


【解决方案1】:

http://jsfiddle.net/Hwvd5/

基本上我所做的是将一个全尺寸的包装 div 放入其中,即position: relative。希望这适用于跨浏览器,仅在 chromium 中测试...

这种方法的问题:您将无法让浏览器自动为与内容匹配的列选择高度而不让它们重叠。 :( 我想不出办法来解决这个问题。

来自 JSFiddle 的文件

<br /> <br />

<div class="table">
    <div>
        First cell  <br />    
        First cell  <br />    
        First cell  <br />    
        First cell  <br />    
        First cell  <br />    
        First cell  <br />    
        First cell  <br />    
        First cell  <br />    
    </div>
    <div>
        <div class="wrapper">
          <div class = "top"></div>
          <h2> some text in the top </h2>
          <h3> some more text in the bottom </h3>
        </div>
    </div>
</div>

CSS:

.top {
    position: absolute;        
    top: 0;
    right: 0;
    width: 10px;
    height: 10px;
    background: red;
}

h3 {
    position: absolute;
    bottom: 0;    
}

.table {
    display: table-row;
}

.table > div {
    display: table-cell; 
    vertical-align: bottom; 
    border: 1px solid black; 
    padding: 10px; 
    height: 200px;   
}

.table > div > div.wrapper {
    position: relative; 
    width: 100%; 
    height: 100%;   
}​
​

【讨论】:

  • 我也会使用定位将它们设置在底部。子级 -> 父级 div。
【解决方案2】:

将此添加到&lt;td&gt;:

style="vertical-align:top"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 2016-01-07
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多