【问题标题】:CSS: div overlay that fits its td and stays on top of the td's content [duplicate]CSS:适合其td并保持在td内容之上的div覆盖[重复]
【发布时间】:2019-03-19 00:36:26
【问题描述】:

我希望 div 覆盖它们的 td 容器,同时完全填充它们(宽度和高度)。它们还应该位于 td 内容之上(在其文本之上)

问题在于表格是响应式的,因此 td 的宽度可变。

一件重要的事情:列数是未知的,用户可以实时更改。所以我不能使用 'calc( (100% /x) - 1px )'...

我怎样才能做到这一点?

这是 jsFiddle: https://jsfiddle.net/4vq8ao6r/4/

尝试使用绝对定位的 div,但我尝试了很多没有令人满意的结果。 我可以用 jQuery 实现这一点,但它的资源很重(尤其是在调整大小时),如果可能的话我想只使用 css。

html:

    <table>
      <thead>
        <tr>
          <th></th>
          <th>Col1</th>
          <th>Col2</th>
          <th>Col3</th>
          <th>Col4</th>
        </tr>
      </thead>
       <tbody>
       <tr>
          <td>Row1</td>
          <td>
              Data1
              <div class="overlay"></div>
          </td>
          <td>
              Data2
              <div class="overlay"></div>
          </td>
          <td>
              Data3
              <div class="overlay"></div>
          </td>
          <td>
              Data4
              <div class="overlay"></div>
          </td>
        </tr>
       <tr>
          <td>Row2</td>
          <td>
              Data1
              <div class="overlay"></div>
          </td>
          <td>
              Data2
              <div class="overlay"></div>
          </td>
          <td>
              Data3
              <div class="overlay"></div>
          </td>
          <td>
              Data4
              <div class="overlay"></div>
          </td>
        </tr>
      </tbody>
    </table>

CSS:

html,body{
    pointer-events: auto;
    width:100%;
    height:100%;
    margin:0px;
    padding:0px;
    background:#212933;
    color:#e5ebea;
    font-family:'lato';
    font-weight:400;
    font-size:11px;
    user-select:none;
  text-align:center;
}

table{
    display:table;
    white-space:nowrap;
    border-collapse:collapse;
    table-layout:fixed; 
    user-select:none;
    width:100%;
}

tr{
    display:table-row;
}

th {
    display:table-cell;
    text-overflow:ellipsis;
    vertical-align:middle;
    padding-right:19px;
    padding-left:10px;
    background:#2e3844;
    border:1px solid #56647c;
    overflow:hidden;
  height:40px;
}

th:first-child{
    background:#212933;
}

td {
    display:table-cell;
    vertical-align:middle;
    padding:6px;
    border:1px solid #56647c;
    overflow:hidden;
  height:40px;
}
td:first-child{
    background:#28303a;
}

.overlay{
  background:red;
  height:40px;
  position:absolute;
  opacity:0.5;
  width:100px;
}

【问题讨论】:

标签: css html-table overlay


【解决方案1】:

这个怎么样?基本上 - 使 td 位置:相对;然后你的 abs 定位覆盖可以将 top/right/bottom/left 全部设置为 0

html,body{
    pointer-events: auto;
    width:100%;
    height:100%;
    margin:0px;
    padding:0px;
    background:#212933;
    color:#e5ebea;
    font-family:'lato';
    font-weight:400;
    font-size:11px;
    user-select:none;
  text-align:center;
}

table{
    display:table;
    white-space:nowrap;
    border-collapse:collapse;
    table-layout:fixed; 
    user-select:none;
    width:100%;
}

tr{
    display:table-row;
}

th {
    display:table-cell;
    text-overflow:ellipsis;
    vertical-align:middle;
    padding-right:19px;
    padding-left:10px;
    background:#2e3844;
    border:1px solid #56647c;
    overflow:hidden;
  height:40px;
}

th:first-child{
    background:#212933;
}

td {
    display:table-cell;
    vertical-align:middle;
    padding:6px;
    border:1px solid #56647c;
    overflow:hidden;
  height:40px;
  position: relative;
}
td:first-child{
    background:#28303a;
}

.overlay{
  background:red;
  height:40px;
  position:absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  opacity:0.5;
  width:100px;
}

https://jsfiddle.net/chmxnvzb/ ?

【讨论】:

  • 该死!你打了我一拳。从.overlay 中取出宽度和高度,你就知道了。
  • Benlumley:非常感谢! @Pete:当然这并不代表我正在努力实现的目标。我给 div 设置了宽度,以便我们可以看到它们,否则它们根本不会出现,这可能会使读者感到困惑。这是我做出的视觉选择。谢谢大家!
  • @Daniel:不,没有宽度和高度,它也不起作用。不同之处在于按照 Benlumney 的建议添加了相对于 td 的位置,然后摆脱宽度和高度产生了影响。谢谢!
  • 对你们所有人:你们完全正确,它是复制品。我在发布之前进行了研究,但我使用的关键词并没有把我带到正确的地方。谢谢大家!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
相关资源
最近更新 更多