モバイルファースト - ハンバーガーメニュー
モバイルファースト - ハンバーガーメニュー
HTML記述例
- ナビゲーションは「モバイル用」と「PC用」と作成し、ボタンイベントで表示を切り替える
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>【コーディング練習】CASABLANCA</title> <link rel="stylesheet" href="css/style.css"> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Italianno&display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"> </head> <body> <!-- .header --> <header class="header"> <div class="container"> <h1><a href="#">カサブランカ</a></h1> <p>カサブランカはヨーロッパビンテージ家具の輸入販売のスペシャリストです。</p> </div><!-- /.container --> </header> <!-- /.header --> <!-- .key-visual --> <div class="key-visual"> <div class="container"> <p>Casa Blanca<br> European Import Furniture</p> </div><!-- /.container --> </div> <!-- /.key-visual --> <!-- .gnav --> <!-- PC-nav --> <nav class="pcnav"> <ul class="container"> <li><a href="#">HOME</a></li> <li><a href="#">カサブランカ案内</a></li> <li><a href="#">ビンテージ家具とは?</a></li> <li><a href="#">個人情報保護</a></li> <li><a href="#">お問合せ</a></li> </ul> </nav><!-- /.pcnav --> <div class="h-btn"> <i class="fas fa-bars"></i> <i class="fas fa-times"></i> </div><!-- /.h-btn --> <!-- mb-nav --> <nav class="mbnav"> <ul> <li><a href="#">HOME</a></li> <li><a href="#">カサブランカ案内</a></li> <li><a href="#">ビンテージ家具とは?</a></li> <li><a href="#">個人情報保護</a></li> <li><a href="#">お問合せ</a></li> </ul> </nav><!-- /.mbnav --> <!-- /.gnav --> <div class="container wrapper"> <!-- .main-content --> <main class="main-content"> <section> <h2>くつろぎの空間を演出</h2> <p cla>インテリアショップカサブランカへようこそ!</p> <p>ヨーロッパ家具・インテリアをイタリア・スペインより直輸入。<br> 居心地の良いダイニングルーム、心安らぐリビング、ベッドルーム・・・・今、求められる快適空間をカサブランカはご提供します。</p> <p class="photo"><img src="img/bed.jpg" alt="イタリア製高級ベッドの画像"></p> <p>まるでフランス映画に出てくるようなスタイルの天蓋付きウッドベッドです。こんなベッドに横たわるあなたはヒロインです。プリンセスなハウススタイリングを演出するお道具として、お使いください。</p> <p>素材は質量感のあるパイン材を使用しております。マットはポケットコイルをご用意しております価格にはマットが含まれております。手作業で製作しているため、それぞれ微妙に表情が異なります。あらかじめご了承ください。</p> </section> </main> <!-- /.main-content --> z <!-- .sidebar --> <aside class="sidebar"> <h2>CATEGORIES</h2> <ul class="category-menu"> <li><a class="gnav__linke" href="#">ソファー</a></li> <li><a class="gnav__linke" href="#">ベッド</a></li> <li><a class="gnav__linke" href="#">テーブル</a></li> <li><a class="gnav__linke" href="#">キッチン</a></li> </ul> <p class="banner"><a href="#"><img src="img/banner.jpg" alt="大特価セール中の広告バナー"></a></p> </aside><!-- /.sidebar --> </div><!-- /.container .wrap --> <!-- .footer --> <footer class="footer"> <p class="container"><small>Copyright © CASA BLANCA</small></p> </footer> <!-- /.footer --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script> $(function(){ $('.h-btn').on('click', function(){ $(this).toggleClass('close'); $('.mbnav').toggleClass('slide'); }); }); </script> </body> </html>
jQueryのイベントを記述
- 2行に記述してあるi要素を、overflow: hidden; ではみ出ている部分を隠す
- ボタンにクラス名closeが追加されたときには、1行目を非表示にする
- navのクラス名slideが追加されたとき、navのpositionを移動する記述を書く
<script> $(function(){ $('.h-btn').on('click', function(){ // ボタン.h-btnが押されたとき $(this).toggleClass('close'); // ボタン自身にクラス名closeを追加(次、押されたら削除) $('.mbnav').toggleClass('slide'); // navにクラス名slideを追加(次、押されたら削除) }); });
CSSの記述例
- メディアクエリは、「コンポーネント」ごとに記述する
- navのposition変化は、left: 100%; でブラウザの右側の外に配置し、クリックされたとき left: 0; で全面に表示されるように指定する
@charset "UTF-8"; /* ------------------------------------------- reset ------------------------------------------- */ *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } ul, li { list-style: none; } a { color: inherit; text-decoration: none; } img { max-width: 100%; vertical-align: bottom; } /* ------------------------------------------ body ------------------------------------------- */ body { color: #333; font-size: 16px; font-family: /* "Helvetica Neue", */ /* Arial, */ "Hiragino Kaku Gothic ProN", "Hiragino Sans", Meiryo, sans-serif; line-height: 1.0; } /* ------------------------------------------- layout(common) ------------------------------------------- */ .wrapper { padding: 30px 0 40px; } .container { width: min(94%, 960px); margin: 0 auto; } h2 { margin-bottom: 10px; color: #900; font-size: 28px; text-align: left; } @media screen and (min-width: 768px) { .container { display: flex; justify-content: space-between; margin: 0 auto; } .main-content { width: calc(630/960 * 100%); } .sidebar { width: calc(300/960 * 100%); } } /* ------------------------------------------- header ------------------------------------------- */ .header h1 { padding: 17px 0; background: url(../img/logo.png) no-repeat center center; text-indent: 100%; white-space: nowrap; overflow: hidden; } .header p { display: none; } @media screen and (min-width: 768px) { .header > .container { display: flex; justify-content: flex-start; align-items: center; padding: 10px 0; } .header h1 { margin-right: auto; padding: 7px 0; } .header p { display: block; max-width: 66%; line-height: 1.3; } } /* ------------------------------------------- key-visual ------------------------------------------- */ .key-visual { padding: 30px 0; text-align: center; background: url(../img/sofa.jpg) no-repeat center center; background-size: cover; } .key-visual > .container { color: #fff; font-family: 'Italianno', cursive; text-shadow: 0 0 6px #900, 0 0 6px #900; font-size: 35px; text-align: center; } .key-visual p::first-line { font-size: 75px; } @media screen and (min-width: 768px) { .key-visual { padding: 120px 0; } .key-visual > .container { display: flex; font-size: 44px; text-align: left; } .key-visual p::first-line { font-size: 100px; } } /* ------------------------------------------- nav ------------------------------------------- */ /* hamburger */ .h-btn { z-index: 1000; position: fixed; top: 10px; right: 16px; width: 50px; height: 50px; overflow: hidden; border: 1px solid #4e352d; border-radius: 5px; background: #fff; text-align: center; cursor: pointer; } .h-btn i { color: #4e352d; font-size: 35px; line-height: 50px; } .close i:first-child { display: none; } /* mb nav */ .pcnav { display: none; } .mbnav { display: flex; flex-direction: column; justify-content: center; align-items: center; position: fixed; top: 0; left: 100%; width: 100%; height: 100vh; background: rgba(78, 53, 45, 0.9); transition: .3s; } .slide { left: 0; } .mbnav ul { width: 90%; border-top: 1px solid #fff; } .mbnav li { text-align: center; font-size: 20px; line-height: 60px; } .mbnav a { display: block; border-bottom: 1px solid #fff; color: #fff; } @media screen and (min-width: 768px) { /* PC nav */ .h-btn, .mbnav { display: none; } .pcnav { display: block; background: url(../img/btn_bg.png) repeat-x left top; } .pcnav ul { display: flex; } .pcnav li { width: calc(100% / 5); line-height: 44px; font-size: 14px; text-align: center; } .pcnav a { display: block; border-right: 1px solid #fff; color: #fff; } .pcnav li:first-child a { border-left: 1px solid #fff; } .pcnav a:hover { background: url(../img/btn_bg.png) repeat-x left bottom; } } /* ------------------------------------------- main ------------------------------------------- */ .main-content h2 { margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid #aaa; text-align: center; } .main-content p { margin-bottom: 10px; font-size: 14px; line-height: 1.7; } .main-content p:first-of-type { color: #d30; font-size: 16px; font-weight: bold; } .photo { text-align: center; } .photo img { border: 10px solid #bd0202; box-sizing: border-box; } @media screen and (min-width: 768px) { .main-content h2 { text-align: left; } .main-content p { font-size: 16px; } .main-content p:first-of-type { font-size: 20px; } } /* ------------------------------------------- aside ------------------------------------------- */ .sidebar h2 { margin-bottom: 10px; color: #900; font-size: 28px; text-align: center; } .sidebar ul { margin-bottom: 20px; } .sidebar li a { display: block; padding-left: 40px; background: url(../img/listmark.png) no-repeat 10px center; border-radius: 5px; border: 1px solid #aaa; background-position: left 20px center; } .sidebar li { line-height: 44px; } .sidebar li:not(:last-of-type) { margin-bottom: 5px; } .sidebar li a:hover { background-color: #fdc; } .banner { text-align: center; } @media screen and (min-width: 768px) { .sidebar h2 { margin-bottom: 0; padding-bottom: 10px; border-bottom: 1px solid #aaa; } .sidebar ul { margin-bottom: 50px; } .sidebar li:not(:last-of-type) { margin-bottom: 0; } .sidebar ul a { margin-bottom: 0; border: 0; border-bottom: 1px solid #aaa; } } /* ------------------------------------------- footer ------------------------------------------- */ .footer { padding: 30px 0; background-color: #d30; color: #fff; font-size: 15px; text-align: center; } @media screen and (min-width: 768px) { .footer { text-align: left; } }