使 Table 个单元格(不是 3x3、2x4!)正方形?

Make Table Cells (Not 3x3, 2x4!) Square?

我正在尝试制作一些正方形 table 单元格的响应。我知道这个问题的答案,但它们似乎都是针对 3x3 网格的,我找不到 2x4 版本。我也不希望它必须滚动,它需要适合菜单。它应该始终是方形的,如果它不能适应高度,请按比例缩小。我希望它水平和垂直居中。我该怎么做呢? 这是有问题的网站。 The Site。 您可以看到右侧的网格如何适合高度和宽度,但不是正方形。我该如何解决这个问题?

.menu{
  background: #222222;
  position: absolute;
  top: 50%;
  /*left: 50%;*/
}
.menu.right{
  width: 25%;
  height: 70%;
  /*transform: translate(-50%, -50%);*/
  top: 5%;
  right: 0;
  float: right;
  /*display: grid;
  grid-gap: 5px;
  */
  /*grid-template-columns: auto auto;*/
  /*grid-template-rows: auto auto auto auto;
  grid-template-columns: repeat(2, 50%);
  */
  /*grid-template: repeat(4, 1fr) / repeat(2, 1fr);
  padding: 5px;*/
  display: flex;
  flex-direction: column;
}
#statwrap{
  box-sizing: border-box;
  display: flex;
  width: 100%;
  height: 100%;
  flex-direction: row;
  padding: 5px;
}
#stattab{
  width: 100%;
  height: 100%;
  flex: 1;
  border-spacing: 10px 10px;
}
#statbody{

}
.statcell{
  background: #333333;
  border-radius: 15px;
  width: 50%;
}
<div class="menu right">
  <!--
  <div class="stat one"></div>
  <div class="stat two"></div>
  <div class="stat three"></div>
  <div class="stat four"></div>
  <div class="stat five"></div>
  <div class="stat six"></div>
  <div class="stat seven"></div>
  <div class="stat eight"></div>
  -->
  <div id="statwrap">
    <table id="stattab">
      <tbody id="statbody">
        <tr>
          <td class="statcell"></td>
          <td class="statcell"></td>
        </tr>
        <tr>
          <td class="statcell"></td>
          <td class="statcell"></td>
        </tr>
        <tr>
          <td class="statcell"></td>
          <td class="statcell"></td>
        </tr>
        <tr>
          <td class="statcell"></td>
          <td class="statcell"></td>
        </tr>
      </tbody>
    </table>
</div>
</div>

谢谢。

const adjustSquare = () => {
    let squareWidth = document.querySelector('.menu.right').offsetHeight / 4;
    document.getElementById('stattab').style.maxWidth = (squareWidth * 2) + 'px';
}

document.onreadystatechange = () => {
    if (document.readyState == 'interactive') {
        adjustSquare()
    }
}

window.onresize = adjustSquare
.menu {
    background: #222222;
    position: absolute;
    top: 50%;
}
.menu.right {
    width: 25%;
    height: 70%;
    top: 5%;
    right: 0;
    float: right;
    display: flex;
    flex-direction: column;
    overflow-y: scroll;
    justify-content: center;
}
#statwrap {
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    padding: 5px;
    justify-content: center;
    align-items: center;
}
#stattab {
    width: 100%;
    height: 100%;
    flex: 1;
    border-spacing: 10px 10px;
}
.statcell {
    background: #333333;
    border-radius: 15px;
    width: 50%;
    position: relative;
}
.statcell .square {
    padding-top: 100%;
}
.statcell .content {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 5px;
    color: #ffffff;
}
<div class="menu right">
  <div id="statwrap">
    <table id="stattab">
      <tbody id="statbody">
        <tr>
          <td class="statcell">
            <div class="square"></div>
            <div class="content"></div>
          </td>
          <td class="statcell">
            <div class="square"></div>
            <div class="content"></div>
          </td>
        </tr>
        <tr>
          <td class="statcell">
            <div class="square"></div>
            <div class="content"></div>
          </td>
          <td class="statcell">
            <div class="square"></div>
            <div class="content"></div>
          </td>
        </tr>
        <tr>
          <td class="statcell">
            <div class="square"></div>
            <div class="content"></div>
          </td>
          <td class="statcell">
            <div class="square"></div>
            <div class="content"></div>
          </td>
        </tr>
        <tr>
          <td class="statcell">
            <div class="square"></div>
            <div class="content"></div>
          </td>
          <td class="statcell">
            <div class="square"></div>
            <div class="content"></div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

在单元格内,您可以使用垂直填充为 100% 的浮动伪元素。它将以 td 的宽度为参考并将其拉伸为至少一个正方形。

一个 table 单元格无论如何都会增长以匹配内容的高度,如果它超出了您的初始正方形

.menu.right {
  width: 25%;
  height: 70%; 
  max-width:40vmin;
  max-height:90vmin;
  min-width:0;
  float: right; 
  display: flex;
  flex-direction: column;
  border:solid;
}

#statwrap {
  box-sizing: border-box;
  display: flex;
  width: 100%;
  height: 100%;
  flex-direction: row;
  padding: 5px;
}

#stattab {
  width: 100%;
  height: 100%;
  flex: 1;
  border-spacing: 10px 10px;
}

#statbody {}

.statcell {
  background: #333333;
  border-radius: 15px;
  width: 50%;
}

.statcell::before {
  content: '';
  float: left;
  padding-top: 100%;
}
<div class="menu right">
  <!--
  <div class="stat one"></div>
  <div class="stat two"></div>
  <div class="stat three"></div>
  <div class="stat four"></div>
  <div class="stat five"></div>
  <div class="stat six"></div>
  <div class="stat seven"></div>
  <div class="stat eight"></div>
  -->
  <div id="statwrap">
    <table id="stattab">
      <tbody id="statbody">
        <tr>
          <td class="statcell"></td>
          <td class="statcell"></td>
        </tr>
        <tr>
          <td class="statcell"></td>
          <td class="statcell"></td>
        </tr>
        <tr>
          <td class="statcell"></td>
          <td class="statcell"></td>
        </tr>
        <tr>
          <td class="statcell"></td>
          <td class="statcell"></td>
        </tr>
      </tbody>
    </table>
  </div>
</div>