Lea*_*ner 3 html css html-table css-position absolute
我有一个固定的前三列的表。我的前三列是固定的。我想的是,其余的列应计算它们的height和width自动的基础上,他们的内容。
所以我的 CSS 看起来像这样:
.outer {
    position:relative;
    background-color: hotpink;
}
.inner {
    overflow-x:scroll;
    overflow-y: visible;
    width: calc(100% - 1500px);
    margin-left: 18em;
    background-color: greenyellow;
}
table {
    table-layout: fixed;
    width: 100%;
}
td, th {
    vertical-align: top;
    border-top: 1px solid #ccc;
    padding: 0.8em;
    width: 150px;
    height: 42px;
    word-break: break-all;
}
.col1 {
    position:absolute;
    left: 0em;
    width: 6em;
}
.col2 {
    position:absolute;
    left: 6em;
    width: 6em;
 }
.col3 {
    position:absolute;
    left: 12em;
    width: 6em;
}
.emptyrow {
    position:absolute;
    left: 0em;
    width: 18em;
}
这是表格的 HTML:
<div class="outer">
    <div class="inner">
      <table>
        <thead>
          <tr>
            <th class="col1">Header 1</th>
            <th class="col2">Header 2</th>
            <th class="col3">Header 3</th>
            <th>Header 4</th>
            <th>Header 5</th>
            <th>Header 6</th>
            <th>Header 7</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="emptyrow">This column should have value.</td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td class="col1">col 1 - B</td>
            <td class="col2">col 2 - B</td>
            <td class="col3">col 3 - B</td>
            <td>col 4 - B</td>
            <td>col 5 - B</td>
            <td>col 6 - B</td>
            <td>col 7 - B</td>
          </tr>
          <tr>
            <td class="col1">col 1 - C</td>
            <td class="col2">col 2 - C</td>
            <td class="col3">col 3 - C</td>
            <td>col 4 - C</td>
            <td>col 5 - C</td>
            <td>col 6 - C</td>
            <td>col 7 - C</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
是否可以避免在td, 中设置宽度和高度th?
我的意思是避免使用height和的硬编码值width:
td, th {
  ...
  width:6em;
  height: 4em;
}
是否可以只使用纯 CSS 而不使用 JavaScript?如果无法使用此解决方案实施,那么看到另一种方法真的很棒。
要求:
Padding 在所有列中应该是 0.8emWidth和Height的td,th不应该被硬编码。它们应该适合内容。border-top: 1px solid #ccc; 这个属性应该保留恐怕没有完美的解决方案。
我会给你不同的选择,你需要决定放弃什么。
? 纯 CSS - ? 旧浏览器 - ? 灵活的宽度 - ? 适当突出
关键是为width前三列设置一个fixed ,然后设置元素的margin-left属性.wrapped等于列的总宽度。
.wrapper {
  overflow-x: scroll;
  overflow-y: visible;
  margin-left: 18em;
  width: 15em;
  background-color: greenyellow;
}
td,
th {
  vertical-align: top;
  border-top: 1px solid #ccc;
  padding: 0.8em;
}
th, tr:nth-of-type(1) td {
  height: 1em;
}
th:nth-of-type(1),
td:nth-of-type(1) {
  position: absolute;
  left: 0em;
  width: 6em;
}
th:nth-of-type(2),
td:nth-of-type(2) {
  position: absolute;
  left: 6em;
  width: 6em;
}
th:nth-of-type(3),
td:nth-of-type(3) {
  position: absolute;
  left: 12em;
  width: 6em;
}
tr:hover td {
  background-color: yellow;
}<div class="wrapper">
  <table>
    <thead>
      <tr>
        <th>Header-1</th>
        <th>Header-2</th>
        <th>Header-3</th>
        <th>Header-4</th>
        <th>Header-5</th>
        <th>Header-5</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td>col 1 - A</td>
        <td>col 2 - A</td>
        <td>col 3 - A</td>
        <td>col 4 - A (WITH LONGER CONTENT)</td>
        <td>col 5 - A</td>
        <td>col 6 - A</td>
      </tr>
      <tr>
        <td>col 1 - B</td>
        <td>col 2 - B</td>
        <td>col 3 - B</td>
        <td>col 4 - B</td>
        <td>col 5 - B</td>
        <td>col 6 - B</td>
      </tr>
      <tr>
        <td>col 1 - C</td>
        <td>col 2 - C</td>
        <td>col 3 - C</td>
        <td>col 4 - C</td>
        <td>col 5 - C</td>
        <td>col 6 - C (WITH_A_LONG_WORD)</td>
      </tr>
    </tbody>
  </table>? 纯 CSS - ? 旧浏览器 - ? 灵活的宽度 - ? 适当突出
在这里,width我们没有预先对列的进行硬编码,而是使用 JS 来计算和设置width.
一个更好的 JS 实现是可能的。举个例子吧。
let wrapper = document.querySelector('.wrapper');
let cols = document.querySelectorAll('th');
let widthCol0 = cols[0].offsetWidth;
let widthCol1 = cols[1].offsetWidth;
let widthCol2 = cols[2].offsetWidth;
stylesheet = document.styleSheets[0]
stylesheet.insertRule('th:nth-of-type(1), td:nth-of-type(1) { left: 0px; position: absolute; width: ' + widthCol0 + 'px;}', 0);
stylesheet.insertRule('th:nth-of-type(2), td:nth-of-type(2) { left: ' +  widthCol0 + 'px; position: absolute; width: ' + widthCol1 + 'px;}', 0);
stylesheet.insertRule('th:nth-of-type(3), td:nth-of-type(3) { left: ' +  (widthCol0 + widthCol1) + 'px; position: absolute; width: ' + widthCol2 + 'px;}', 0);
wrapper.style.marginLeft = (widthCol0 + widthCol1 + widthCol2) + 'px';.wrapper {
  overflow-x: scroll;
  overflow-y: visible;
  width: 15em;
  background-color: greenyellow;
}
td,
th {
  vertical-align: top;
  border-top: 1px solid #ccc;
  padding: 0.8em;
}
th, tr:nth-of-type(1) td {
  height: 1em;
}
tr:hover td {
  background-color: yellow;
}<div class="wrapper">
  <table>
    <thead>
      <tr>
        <th>Header-1</th>
        <th>Header-2</th>
        <th>Header-3</th>
        <th>Header-4</th>
        <th>Header-5</th>
        <th>Header-5</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td>col 1 - A (WITH LONGER CONTENT)</td>
        <td>col 2 - A</td>
        <td>col 3 - A</td>
        <td>col 4 - A (WITH LONGER CONTENT)</td>
        <td>col 5 - A</td>
        <td>col 6 - A</td>
      </tr>
      <tr>
        <td>col 1 - B</td>
        <td>col 2 - B</td>
        <td>col 3 - B</td>
        <td>col 4 - B</td>
        <td>col 5 - B</td>
        <td>col 6 - B</td>
      </tr>
      <tr>
        <td>col 1 - C</td>
        <td>col 2 - C (WITH_A_LONG_WORD)</td>
        <td>col 3 - C</td>
        <td>col 4 - C</td>
        <td>col 5 - C</td>
        <td>col 6 - C (WITH_A_LONG_WORD)</td>
      </tr>
    </tbody>
  </table>? 纯 CSS - ? 旧浏览器 - ? 灵活的宽度 - ? 适当突出
我真的很喜欢position: sticky@Dominic 展示的解决方案。如果您不需要支持 IE ,这是要走的路。
放弃 IE 支持是有回报的。您可以有适当的突出显示,前三列的宽度将适应内容。
尽管如此,即使您不需要对 a 进行硬编码width,也需要对left属性进行硬编码以设置列在哪个点变得粘滞。那种挫败灵活点的意思width。没有办法。
.wrapper {
  width: 40em;
  overflow-x: scroll;
}
td,
th {
  vertical-align: top;
  border-top: 1px solid #ccc;
  padding: 0.8em;
}
th,
tr:nth-of-type(1) td {
  height: 1em;
}
th:nth-of-type(1), td:nth-of-type(1),
th:nth-of-type(2), td:nth-of-type(2),
th:nth-of-type(3), td:nth-of-type(3) {
  background-color: white;
  position: sticky;
}
th:nth-of-type(1), td:nth-of-type(1) {
  left: 0em;
}
th:nth-of-type(2), td:nth-of-type(2) {
  left: 6em;
}
th:nth-of-type(3), td:nth-of-type(3) {
  left: 12em;
}
tr:hover td{
  background-color: yellow;
}<div class="wrapper">
<table>
  <thead>
    <tr>
      <th>Header-1</th>
      <th>Header-2</th>
      <th>Header-3</th>
      <th>Header-4</th>
      <th>Header-5</th>
      <th>Header-6</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td>col 1 - A (WITH LONGER CONTENT)</td>
      <td>col 2 - A</td>
      <td>col 3 - A</td>
      <td>col 4 - A (WITH LONGER CONTENT)</td>
      <td>col 5 - A</td>
      <td>col 6 - A</td>
    </tr>
    <tr>
      <td>col 1 - B</td>
      <td>col 2 - B</td>
      <td>col 3 - B</td>
      <td>col 4 - B</td>
      <td>col 5 - B</td>
      <td>col 6 - B</td>
    </tr>
    <tr>
      <td>col 1 - C</td>
      <td>col 2 - C (WITH_A_LONG_WORD)</td>
      <td>col 3 - C</td>
      <td>col 4 - C</td>
      <td>col 5 - C</td>
      <td>col 6 - C (WITH_A_LONG_WORD)</td>
    </tr>
  </tbody>
</table>
</div>? 纯 CSS - ? 旧浏览器 - ? 灵活的宽度 - ? 适当突出
此示例与上面的示例具有完全相同的 HTML 和 CSS。
就像在我们的第二个例子中一样,我们使用 JS 来计算width列的 。在这种情况下,我们使用它来覆盖该left属性。此外,JS 代码更直接。
left如果我们稍后要用 JS 设置它,为什么要在 CSS 中设置它?因为如果客户端不运行 JS,我们不希望列完全折叠,破坏我们的布局。
let cols = document.querySelectorAll('th');
let widthCol0 = cols[0].offsetWidth;
let widthCol1 = cols[1].offsetWidth;
stylesheet = document.styleSheets[0];
stylesheet.insertRule('th:nth-of-type(2), td:nth-of-type(2) { left: ' +  widthCol0 + 'px !important;}', 0);
stylesheet.insertRule('th:nth-of-type(3), td:nth-of-type(3) { left: ' +  (widthCol0 + widthCol1) + 'px !important;', 0);.wrapper {
  width: 40em;
  overflow-x: scroll;
}
td,
th {
  vertical-align: top;
  border-top: 1px solid #ccc;
  padding: 0.8em;
}
th,
tr:nth-of-type(1) td {
  height: 1em;
}
th:nth-of-type(1), td:nth-of-type(1),
th:nth-of-type(2), td:nth-of-type(2),
th:nth-of-type(3), td:nth-of-type(3) {
  background-color: white;
  position: sticky;
}
th:nth-of-type(1), td:nth-of-type(1) {
  left: 0em;
}
th:nth-of-type(2), td:nth-of-type(2) {
  left: 6em;
}
th:nth-of-type(3), td:nth-of-type(3) {
  left: 12em;
}
tr:hover td{
  background-color: yellow;
}<div class="wrapper">
<table>
  <thead>
    <tr>
      <th>Header-1</th>
      <th>Header-2</th>
      <th>Header-3</th>
      <th>Header-4</th>
      <th>Header-5</th>
      <th>Header-6</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td>col 1 - A (WITH LONGER CONTENT)</td>
      <td>col 2 - A</td>
      <td>col 3 - A</td>
      <td>col 4 - A (WITH LONGER CONTENT)</td>
      <td>col 5 - A</td>
      <td>col 6 - A</td>
    </tr>
    <tr>
      <td>col 1 - B</td>
      <td>col 2 - B</td>
      <td>col 3 - B</td>
      <td>col 4 - B</td>
      <td>col 5 - B</td>
      <td>col 6 - B</td>
    </tr>
    <tr>
      <td>col 1 - C</td>
      <td>col 2 - C (WITH_A_LONG_WORD)</td>
      <td>col 3 - C</td>
      <td>col 4 - C</td>
      <td>col 5 - C</td>
      <td>col 6 - C (WITH_A_LONG_WORD)</td>
    </tr>
  </tbody>
</table>
</div>由您决定哪种方法最适合您的需求。
就个人而言,我认为第四个最强大,因为:
如果 JS 被禁用,列将崩溃超过最佳点,但一切都会正常工作。只需使用leftCSS 中的默认属性即可。
在 IE11 下,该表将很好地回退到非固定列表。
我不认为这有什么大不了的。当然,如果您正在为 IE11 是首选浏览器的 Intranet 进行编程,请采用第一种或第二种方法。
| 归档时间: | 
 | 
| 查看次数: | 1129 次 | 
| 最近记录: |