コンテンツにスキップ

CSSでスタイリング

この章では、ゲームの見た目を本家2048に近いデザインに整えます。

src/App.css の内容をすべて書き換えてください。

/* ページ全体 */
body {
margin: 0;
background-color: #faf8ef;
font-family: 'Arial', sans-serif;
display: flex;
justify-content: center;
padding: 20px;
}
/* ゲーム全体のコンテナ */
.game-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 16px;
}
/* タイトル */
h1 {
font-size: 64px;
font-weight: bold;
color: #776e65;
margin: 0;
}
/* スコア表示 */
.score-box {
background-color: #bbada0;
color: #f9f6f2;
border-radius: 6px;
padding: 8px 20px;
font-size: 18px;
font-weight: bold;
}
.score-label {
font-size: 12px;
text-transform: uppercase;
opacity: 0.7;
}
/* リスタートボタン */
button {
background-color: #8f7a66;
color: #f9f6f2;
border: none;
border-radius: 4px;
padding: 10px 20px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
}
button:hover {
background-color: #7a6657;
}
/* ゲームボード */
.board {
display: grid;
grid-template-columns: repeat(4, 100px);
grid-template-rows: repeat(4, 100px);
gap: 12px;
background-color: #bbada0;
padding: 12px;
border-radius: 8px;
width: fit-content;
}
/* 各マス(空きマス) */
.cell {
display: flex;
align-items: center;
justify-content: center;
background-color: rgba(238, 228, 218, 0.35);
border-radius: 4px;
font-size: 28px;
font-weight: bold;
}
/* 数字のあるタイル */
.tile {
transition: background-color 0.1s ease;
}
/* ゲームオーバー表示 */
.game-over {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(238, 228, 218, 0.73);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-radius: 8px;
}
.game-over p {
font-size: 48px;
font-weight: bold;
color: #776e65;
margin: 0;
}
/* ボード周辺のラッパー(ゲームオーバーのoverlay用) */
.board-wrapper {
position: relative;
}

src/App.tsx の return 文の中にある <p>スコア:{score}</p> の行を、次のように書き換えてください。

<div className="score-box">
<div className="score-label">スコア</div>
<div>{score}</div>
</div>

ゲームオーバーのオーバーレイを修正する

Section titled “ゲームオーバーのオーバーレイを修正する”

ゲームオーバー表示をボードの上に重ねるため、src/App.tsx の return 文全体を次のように書き換えてください。

return (
<div className="game-container">
<h1>2048</h1>
<div className="score-box">
<div className="score-label">スコア</div>
<div>{score}</div>
</div>
<button onClick={handleRestart}>リスタート</button>
<div className="board-wrapper">
{gameOver && (
<div className="game-over">
<p>ゲームオーバー!</p>
</div>
)}
<Board board={board} />
</div>
</div>
);

タイルのフォントサイズを調整する

Section titled “タイルのフォントサイズを調整する”

数字が4桁(1024・2048)になるとタイルからはみ出すことがあります。Tile.tsx でフォントサイズを動的に変えましょう。

src/components/Tile.tsx の Tile 関数だけを次のように書き換えてください(TILE_COLORS や DEFAULT_TILE_STYLE などは変更不要です)。

function Tile({ value }: TileProps) {
if (value === 0) return <div className="cell" />;
const style = TILE_COLORS[value] ?? DEFAULT_TILE_STYLE;
const fontSize: string = value >= 1024 ? '20px' : value >= 128 ? '24px' : '28px';
return (
<div
className="cell tile"
style={{
backgroundColor: style.background,
color: style.color,
fontSize,
}}
>
{value}
</div>
);
}

fontSize に : string の型アノテーションをつけています。三項演算子(? :)が連なっていますが、結果は必ず文字列なので string 型と明示するとコードの意図が伝わりやすくなります。


ここまでの変更を保存してブラウザを確認してください。本家2048に近い見た目になっているはずです。

  • タイトル「2048」が大きく表示される
  • スコアがボックスで表示される
  • タイルの色が数字に応じて変わる
  • ゲームオーバー時にオーバーレイが表示される

  • display: grid と gap でボードのレイアウトを整える
  • position: relative / absolute でオーバーレイを重ねる
  • 数字の桁数に応じてフォントサイズを変えると見た目がきれいになる

次の最終章では、チュートリアル全体を振り返り、次のステップを紹介します。