LoginSignup
0
0

More than 1 year has passed since last update.

JavaScript不要!CSSだけでハンバーガーメニューを実装する方法

Posted at

参考記事
https://www.asobou.co.jp/blog/web/css-menu

ビューページ

<header class="phone_area">
  <div class="header_phone">
    <div class="hamburger-menu">
      <input type="checkbox" id="menu-btn-check">
      <label for="menu-btn-check" class="btn menu-btn"><span></span></label>
      <!--ここからメニュー-->
      <div class="menu-content">
        <ul>
          <li>
            <a href="#">ログイン</a>
          </li>
          <li>
            <a href="#">会員登録</a>
          </li>
        </ul>
      </div>
      <!--ここまでメニュー-->
    </div>
  </div>
</header>

css

/* ヘッダーphoneバージョン */
input:checked + label {
    color: #9A7B4E;
    border-color: #9A7B4E;
    background-image:url("/img/x.svg");
    background-repeat: no-repeat;
    background-position: center;
    box-shadow: none;
  }

.menu-btn {
    /* position: fixed; */
    /* top: .75rem;
    right: 1rem; */
    display: flex;
    height: 45px;
    width: 45px;
    justify-content: center;
    align-items: center;
    z-index: 90;
    background-image:url("/img/MENU.svg");
    background-repeat: no-repeat;
    background-position: center;
    color: #9A7B4E;
    border-color: #9A7B4E;
}
.menu-content {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 80;
    background-color: #F7F6F1;
}
.menu-content ul {
    padding: 5rem 1rem;
}
.menu-content ul li {
    border-bottom: solid 2px #fff;
    list-style: none;
}
.menu-content ul li a {
    display: block;
    width: 100%;
    font-size: 15px;
    box-sizing: border-box;
    color:#222;
    text-decoration: none;
    padding: 15px 15px 15px 0;
    position: relative;
}
.menu-content ul li a::before {
    content: "";
    width: 7px;
    height: 7px;
    border-top: solid 1px #222;
    border-right: solid 1px #222;
    transform: rotate(45deg);
    position: absolute;
    right: 11px;
    top: 25px;
}
#menu-btn-check {
    display: none;
}

/* ボタンを押したらメニューが出てくるように */
.menu-content {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 100%;/*leftの値を変更してメニューを画面外へ*/
    z-index: 80;
    background-color: #F7F6F1;
    transition: all 0.5s;/*アニメーション設定*/
}

#menu-btn-check:checked ~ .menu-content {
    left: 0;/*メニューを画面内へ*/
}

.header_phone
{
    position: relative;

    display: flex;

    padding: .75rem 1rem; 

    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
}
0
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
0
0