目前我正在从事 FCC 的元素之一,Game of Life
在开始之前,我正在研究如何将网格呈现到页面上。
我希望能够更改表格的尺寸,同时仍保留在表格所在的容器内。
我使用 Materialize.css 作为 CSS 框架。
组件:
import React, { Component } from 'react'
export default class Example extends Component {
constructor(props){
super(props);
this.state = {height: 3, width: 3}
}
render(){
let rows = [];
for (var i = 0; i < this.state.height; i++){
let rowID = `row${i}`
let cell = []
for (var idx = 0; idx < this.state.width; idx++){
let cellID = `cell${i}-${idx}`
cell.push(<td key={cellID} id={cellID}></td>)
}
rows.push(<tr key={i} id={rowID}>{cell}</tr>)
}
return(
<div className="container">
<div className="row">
<div className="col s12 board"></div>
<table id="simple-board">
<tbody>
{rows}
</tbody>
</table>
</div>
</div>
)
}
}
这是它从 React 中呈现的内容:
<div className="container">
<div className="row">
<div className="col s12 board"></div>
<table id="simple-board">
<tbody>
<tr id="row0">
<td id="cell0-0"></td>
<td id="cell0-1"></td>
<td id="cell0-2"></td>
</tr>
<tr id="row1">
<td id="cell1-0"></td>
<td id="cell1-1"></td>
<td id="cell1-2"></td>
</tr>
<tr id="row2">
<td id="cell2-0"></td>
<td id="cell2-1"></td>
<td id="cell2-2"></td>
</tr>
</tbody>
</table>
</div>
</div>
CSS
table {
width: 100%;
table-layout: fixed;
overflow: hidden;
}
td{
width: 33.33%;
position: relative;
color: #101935;
background: #F2FDFF;
border: 4px solid #DBCBD8;
border-radius: 12px;
cursor: pointer;
transition: background 0.5s ease-out, color 0.5s ease-out;
}
td:hover{
background: #564787;
}
我现在遇到的问题是,虽然我可以轻松更改表的尺寸(通过在 React 中更改 state.width 和 state.height),但它会溢出容器。
也就是说,如果我想把容器设置成固定的长宽,如果我把表格的维度设置的比较大,就会溢出。
有什么办法可以覆盖它吗?还是使用表格不是实现此目标的好选择?
最佳答案
你应该使用 css3 灵活盒(flexbox)布局。
关于javascript - React.js-使用 html 动态制作网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39479528/