【问题标题】:How do I change the styling of a table using css?如何使用 css 更改表格的样式?
【发布时间】:2019-08-30 10:49:06
【问题描述】:

我正在使用 CSS 中的表类名称来尝试更改样式,但它并没有改变任何东西。我刚刚尝试更改表格的背景颜色和边框以查看它是否有效。

在我的 CSS 文件中使用了数以百万计的其他表格样式,它们可能会覆盖一些东西,其中一些我在下面显示,但我想如果我访问类名 .table.slb-table 和 @987654323 @, .slb-tbody 那么他们应该覆盖这些其他样式。 HTML 表中的bg-color="red" className="text-white" 有效。

被映射的数组中的值并不重要。如何在 CSS 中设置表格组件的样式?

我的 React 组件:

tableData = () => {
  return this.props.sbranch.map((row, row_key) => {
    return (
      <tr class="slb-tr" key={`row-${row_key}`}>
        {this.tableHeader().map((value, key) => {
          if (value === 'invoice_val' || value === 'invoice_val_net' || value === 'profit_margin_val') {
            return (
              <td scope="col" className="align-middle" key={`data-${key}`}>{`£ ${row[value]}`}</td>
            )
          } else if (value === 'profit_margin_percent' || value === 'return_rate') {
            return (
              <td scope="col" className="align-middle" key={`data-${key}`}>{`${row[value]} %`}</td>
            )
          } else {
            return (
              <td scope="col" className="align-middle" key={`data-${key}`}>{row[value]}</td>
            )
          }
        })}
      </tr>
    )
  })
}

render() {
  return (
    <table className="slb-table">
      <thead class="slb-thead">
        <tr bgcolor="red" className="text-white" >
          {this.tableHeader().map((name, key) => (
            <th scope="col" className="align-middle"
              key={name}>{name}</th>
          ))}
        </tr>
      </thead>
      <tbody className="slb-tbody">
        {this.tableData()}
      </tbody>
    </table>
  )
}

我的 CSS:

    .table .slb-table{
      background-color: #0004ff;
      border: black 10px;
    }

    .table .slb-table > .slb-tbody{
      background-color: #0004ff;
      border: black 10px;
    }

    .table {
      width: 100%;
      margin-bottom: 1rem;
      background-color: transparent; 
    }

    .table th,
    .table td {
      padding: 0.75rem;
      vertical-align: top;
      border-top: 1px solid #dee2e6; 
    }

    .table thead th {
      vertical-align: bottom;
      border-bottom: 2px solid #dee2e6; 
    }

    .table tbody + tbody {
      border-top: 2px solid #dee2e6; 
    }

    .table .table {
      background-color: #FFFFFF; 
    }

【问题讨论】:

  • 很难看出这里到底有什么问题。您能否制作 codeendbox repo 或类似的东西,以便我们能够更清楚地看到问题并帮助您
  • 您是否尝试过使用!important
  • 我无法执行沙盒,并且 !important 不起作用。谢谢。

标签: css reactjs


【解决方案1】:

你可以用 css 改变一切

例如

table, thead, tfoot, tbody{display:block;}
tr {display:flex; flex-wrap:wrap;}
th , td{flex:1;}

【讨论】:

  • 你能告诉我输入什么来改变一个组件的背景颜色吗?然后我也许可以自己解决如何更改其他组件中的其他内容。
  • 当你问你的问题时,如果你想改变背景颜色,很难确切知道你想要什么:.yourClass{background-color:red !important;}
  • 那行不通。每当我做 .table .slb-table {background-color: green} 时,什么都没有发生。每当我尝试更改任何内容或访问任何表格组件时,都会发生这种情况。或者如果我只是做 .slb-table{background-color: green} 它也不起作用。添加 !important 也没有区别。
  • 某处可能有一个 javascrip
猜你喜欢
  • 2014-10-30
  • 1970-01-01
  • 2012-06-07
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多