导航栏的媒体查询在移动设备上不起作用

Media query for navbar doesn't work on mobile

我在 a tutorial 之后为我的网站制作导航栏。在处理我的移动菜单响应时,我制作了一个“.burger”class,其中包含三个 "bars" 来表示菜单。由于某种原因,菜单图标根本不显示。

代码:https://gist.github.com/VlatkoStojkoski/290234f49a6f4e51019ca4b014c03f37

媒体查询中的 css 选择器有误。

将此添加到@media 屏幕和(最大宽度:775 像素)。

nav .burger {
    display: block;
}

这是因为您将display: none设置为菜单。即使您在底部添加了带有 display: block 的媒体查询,第一个将始终占据主导地位。

您应该在第一条规则中添加媒体查询,您可以删除底部的 display: block

.burger {
  @media screen and (min-width: 775px) { 
    display: none;
  }
}

这是您的完整代码:

@import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro&display=swap");
$text-color: whitesmoke;
$font-stack: "Source Sans Pro", sans-serif;
$nav-color: rgba(79, 91, 102, 0.8);

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

nav {
  display: flex;
  justify-content: space-around;
  align-items: center;
  min-height: 12vh;
  font-family: $font-stack;
  background-color: $nav-color;
  h1 {
    letter-spacing: 5px;
    font-size: 30px;
    color: $text-color;
  }
  img {
    width: 64px;
  }
  ul {
    display: flex;
    width: 35%;
    justify-content: space-around;
    a {
      font-size: 21px;
      color: $text-color;
      text-decoration: none;
    }
    li {
      list-style: none;
    }
  }
  .burger {
    @media screen and (min-width: 775px) { 
      display: none;
    }

    div {
      width: 30px;
      height: 4px;
      background-color: $text-color;
      margin: 5px;
    }
  }
}

@media screen and (max-width: 1024px) {
  nav ul {
    width: 50%;
  }
}

@media screen and (max-width: 775px) {
  body {
    overflow-x: hidden;
  }
  nav ul {
    position: absolute;
    right: 0px;
    height: 88vh;
    top: 12vh;
    background-color: $nav-color;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50%;
    transform: translateX(100%);
    li {
      opacity: 0;
    }
  }
}