首頁  >  問答  >  主體

如何使用 CSS 刪除 Next.js 中井字遊戲板上每行下方的空格?

為什麼每一行下面都有一個空格? (CSS)

我正在嘗試製作一個 Next.js 井字遊戲應用程式來測試我的技能。但是,我遇到了 css 問題。似乎每行板下方都存在填充問題 - 每行下方都有一個小空間。附上一張圖片。

我嘗試在幾乎所有地方將填充和邊距設為 0。這是我的 CSS。

body {
    background-color: black;
    height: 100vh;
    width: 100vw;
    margin: 0;
    padding: 0;
}

* {
    box-sizing: border-box;
}

h1, h2, h3, h4, h5, h6, p {
    margin-top: 0;
    padding: 0;
}

.square {
    height: 30vmin;
    width: 30vmin;
    font-size: 25vmin;
    border: 2px solid white;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: black;
    color: white;
    float: left;
    margin-top: -1px;
    margin-right: -1px;
    line-height: 30vmin;
    padding: 0;
}

.squareContainer {
    display: inline-block;
}

.boardContainer {
    border: 2px solid white;
    display: inline-block;
    margin: 0;
}

.container {
    height: 100vh;
    width: 100vw;
    display: flex;
    justify-content: center;
    align-items: center;
}

.boardRow:after {
    clear: both;
    content: '';
    display: table;
    padding: 0;
  }

這是主機板的 JSX:

<div className={styles.boardContainer}>
            <div className={styles.boardRow}>
                <Square value={squares[0]} onSquareClick={() => handleClick(0)}/>
                <Square value={squares[1]} onSquareClick={() => handleClick(1)}/>
                <Square value={squares[2]} onSquareClick={() => handleClick(2)}/>
            </div>
            <div className={styles.boardRow}>
                <Square value={squares[3]} onSquareClick={() => handleClick(3)}/>
                <Square value={squares[4]} onSquareClick={() => handleClick(4)}/>
                <Square value={squares[5]} onSquareClick={() => handleClick(5)}/>
            </div>
            <div className={styles.boardRow}>
                <Square value={squares[6]} onSquareClick={() => handleClick(6)}/>
                <Square value={squares[7]} onSquareClick={() => handleClick(7)}/>
                <Square value={squares[8]} onSquareClick={() => handleClick(8)}/>
            </div>
        </div>

對於正方形:

<div className={styles.squareContainer}>
            <button className={styles.square} onClick={onSquareClick}>
                <p>
                    { value }
                </p>
            </button>
        </div>

不知道問題所在,請幫忙。

P粉004287665P粉004287665218 天前1519

全部回覆(1)我來回復

  • P粉828463673

    P粉8284636732024-04-06 10:55:58

    必須猜測 HTML。希望它與你的相匹配。註解掉 .squareContainerdisplay: inline-block; 規則可以解決該問題。儘管如此,這個 css/html 對於這個目的來說還是太過分了。您可以大大簡化。

    body {
        background-color: black;
        height: 100vh;
        width: 100vw;
        margin: 0;
        padding: 0;
    }
    
    * {
        box-sizing: border-box;
    }
    
    h1, h2, h3, h4, h5, h6, p {
        margin-top: 0;
        padding: 0;
    }
    
    .square {
        height: 30vmin;
        width: 30vmin;
        font-size: 25vmin;
        border: 2px solid white;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: black;
        color: white;
        float: left;
        margin-top: -1px;
        margin-right: -1px;
        line-height: 30vmin;
        padding: 0;
    }
    
    .squareContainer {
        /* display: inline-block;  */
    }
    
    .boardContainer {
        border: 2px solid white;
        display: inline-block;
        margin: 0;
    }
    
    .container {
        height: 100vh;
        width: 100vw;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    .boardRow:after {
        clear: both;
        content: '';
        display: table;
        
        padding: 0;
      }
    <body>
      <div class="container">
        <div class="boardContainer">
          <div class="boardRow">
            <div class="squareContainer">
              <div class="square"></div>
              <div class="square"></div>
              <div class="square"></div>        
            </div>
          </div>
          <div class="boardRow">
            <div class="squareContainer">
              <div class="square"></div>
              <div class="square"></div>
              <div class="square"></div>        
            </div>
          </div>
          <div class="boardRow">
            <div class="squareContainer">
              <div class="square"></div>
              <div class="square"></div>
              <div class="square"></div>        
            </div>
          </div>
        </div>
      </div>
    </body>

    回覆
    0
  • 取消回覆