【问题标题】:The table body will be right-aligned表格主体将右对齐
【发布时间】:2022-02-04 15:43:42
【问题描述】:

我正在使用反应。
表格将右对齐。
我想完整显示它而不是右对齐。 (宽度:100%)。
我想扩展表格,使其不是右对齐,而是全宽,使其成为父元素的 div 标签(宽度:84vw)。

code

import "./styles.css";

export default function App() {
  return (
    <>
      <div
        style={{
          borderWidth: "2px",
          borderColor: "#C7CED7",
          borderStyle: "solid",
          width: "84vw"
        }}
      >
        <table
          style={{
            width: "100%",
            display: "flex",
            flexDirection: "column"
          }}
        >
          <thead>
            <tr>
              <th>boxA</th>
              <th>boxB</th>
              <th>boxC</th>
              <th>BoxD</th>
              <th>BoxE</th>
              <th>BoxF</th>
            </tr>
          </thead>
          <tbody style={{ height: "100px", overflowY: "auto" }}>
            {data.map((d) => (
              <tr key={d.id}>
                <td>{d.a}</td>
                <td>{d.b}</td>
                <td>{d.c}</td>
                <td>{d.d}</td>
                <td>{d.e}</td>
                <td>{d.f}</td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </>
  );
}

【问题讨论】:

  • 从表格样式中移除“display: flex”。
  • 你想让thead 粘在上面吗?
  • 如果你移除 flex,body 将无法滚动。
  • 你必须改变一些结构。
  • 更改标签和html。

标签: html css reactjs


【解决方案1】:

请参考sandbox

我的反应代码和你的代码有些相似,我添加了 css 代码来做到这一点。

重要提示:如果您在thead 标签中添加td 而不是th,则它不起作用。
position: sticky 不适用于thead,它适用于@ 987654327@.

.table-container {
  width: 84vw;
  margin: auto;
  max-height: 400px;
  overflow-y: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  text-align: center;
}

thead {
  position: sticky;
  top: 0;
}

td,
th {
  padding: 1em 0;
}

th {
  background-color: #99ff99;
}

td {
  border-bottom: 1px solid #99ff99;
}

【讨论】:

  • 我想给thead添加一个borderBottom样式,但是当我添加borderBottom时边框没有出现。你知道如何解决这个问题吗?
  • 我会检查的。
  • 我使用::after 伪元素制作了红色边框。请检查沙盒。
  • table 元素中,边框有点奇怪,所以我认为这是最好的方法。
猜你喜欢
  • 2012-08-19
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 2017-03-07
  • 2011-04-09
  • 1970-01-01
  • 2019-01-16
  • 2017-03-28
相关资源
最近更新 更多