【ソースコード】入門編:レシピサイト/トップページ

目次

コードの解説はこちらを参照

ディレクトリ構成

    
recipe
 ├─img
 │  ├─favicon.ico
 │  ├─mainvisual.jpg
 │  ├─recipe1.jpg
 │  ├─recipe2.jpg
 │  └─recipe3.jpg
 │
 ├─css
 │  └─style.css
 │
 └─index.html
    
  

HTML(index.html)

別タブで開く
index.html

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <title>Recipe Diary</title>
    <meta name="description" content="テキストテキストテキストテキストテキストテキストテキストテキスト">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="img/favicon.ico">
    <link rel="stylesheet" href="https://unpkg.com/ress/dist/ress.min.css">
    <link rel="stylesheet" href="css/style.css">
  </head>

  <body>
    <main>
      <div id="mainvisual">
        <img src="img/mainvisual.jpg" alt="">
      </div>

      <div class="text">
        <h1 class="site-title">Recipe Diary</h1>
        <p>
          日々の料理レシピをまとめています。<br>
          和食や洋食、中華、お菓子までいろいろな料理レシピをアップしていますので、<br>
          みなさんの献立にお役立てくださいね!
        </p>
      </div>

      <ul class="flex">
        <li><img src="img/recipe1.jpg" alt=""></li>
        <li><img src="img/recipe2.jpg" alt=""></li>
        <li><img src="img/recipe3.jpg" alt=""></li>
      </ul>

      <div class="text">
        <a class="btn" href="#">レシピ一覧を見る</a>
      </div>
    </main>

    <footer id="footer">
      <ul class="sns">
        <li><a href="#">Instagram</a></li>
        <li><a href="#">Twitter</a></li>
        <li><a href="#">Facebook</a></li>
      </ul>
      <p>&copy; 2021 Recipe Diary</p>
    </footer>
  </body>
</html>

CSS(style.css)

別タブで開く
style.css

@charset "UTF-8";

html {
  font-size: 100%;
}
body {
  color: #2b2a27;
  font-family: "Helvetica Neue", "Arial", "Hiragino Sans", "Meiryo", sans-serif;
}
img {
  max-width: 100%;
}
ul {
  list-style: none;
}
a {
  color: #2b2a27;
}

/*-------------------------------------------
Mainvisual
-------------------------------------------*/
/*
「width: 100%;」と「height: 100vh;」で全画面表示にする。
「object-fit: cover;」でトリミングを行い、「object-position: center top;」で
横は中央、縦をトップに配置する。
*/
#mainvisual img {
  width: 100%;
  height: 100vh;
  object-fit: cover;
  object-position: center top;
  margin-bottom: 80px;
}

/*-------------------------------------------
Text
-------------------------------------------*/
.text {
  text-align: center;
  padding: 0 20px;
  margin-bottom: 80px;
}
.text .site-title {
  margin-bottom: 20px;
}
/*
「display: inline-block;」でaタグに幅と高さを持たせる。
paddingでボタンのサイズを調整。
*/
.text .btn {
  display: inline-block;
  border: solid 1px #2b2a27;
  font-size: 0.875rem;
  padding: 18px 60px;
  text-decoration: none;
}

/*-------------------------------------------
Image
-------------------------------------------*/
.flex {
  display: flex;
  margin-bottom: 60px;
}
/*
「calc(100%/3);」で、widthの値が3等分になるよう計算する。
※「calc」は、割り切れない数で均等に配置したい場合などに便利。
*/
.flex li {
  width: calc(100%/3);
}
/*
「object-fit: cover;」で、高さを固定したまま
画面幅に合わせて画像を伸縮させることができる。
(※IEでは正しく動作しないため注意が必要)
「vertical-align: bottom;」で画像の下にできる隙間を消す。
*/
.flex li img {
  width: 100%;
  height: 500px;
  object-fit: cover;
  vertical-align: bottom;
}

/*-------------------------------------------
Footer
-------------------------------------------*/
#footer {
  font-size: 0.75rem;
  padding: 20px;
  text-align: center;
}
/*
リンクはul、liタグで記述
SNSのリンク集という、一つの意味をもったリスト群になるので、ul、liタグでグルーピング
※リストタグを使用しなくても問題ないが、使用した方が検索エンジンに対して
より適切に意味を伝えることができると考えリストタグを選択しています。
*/
#footer .sns {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}
#footer .sns li {
  margin: 0 10px;
}

/*-------------------------------------------
SP
-------------------------------------------*/
@media screen and (max-width: 834px) {
  /*-------------------------------------------
  Image
  -------------------------------------------*/
  /*
  「flex-direction: column;」で縦並びにする
  */
  .flex {
    flex-direction: column;
  }
  .flex li {
    width: 100%;
  }
}