1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

CSSを学びたいStep5です!今回はmediaクエリでレスポンシブな対応を実施します!
スマホやタブレット、デスクトップに応じてレイアウトの段組みを変えるのに便利です!

成果物

step5.gif

ソースコード

index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid Layout Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="card-container">
        <div class="card">カード1</div>
        <div class="card">カード2</div>
        <div class="card">カード3</div>
        <div class="card">カード4</div>
        <div class="card">カード5</div>
        <div class="card">カード6</div>
    </div>
</body>
</html>
styles.css
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #e0f7fa;
  }
  
  .card-container {
    display: grid;
    gap: 16px;
    padding: 16px;
    width: 100%;
    max-width: 1000px;
  }
  
  /* デフォルト(モバイルサイズ)では1列 */
  .card-container {
    grid-template-columns: 1fr;
  }
  
  /* タブレットサイズ(600px以上)では2列 */
  @media (min-width: 600px) {
    .card-container {
      grid-template-columns: repeat(2, 1fr);
    }
  }
  
  /* デスクトップサイズ(900px以上)では3列 */
  @media (min-width: 900px) {
    .card-container {
      grid-template-columns: repeat(3, 1fr);
    }
  }
  
  .card {
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 20px;
    text-align: center;
  }
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?