为什么我的媒体查询激活后,我的网格为什么不起作用?

问题描述 投票:0回答:1

我正在尝试设计一个简单的导航栏。我想将<nav>内容(导航链接)放置在标题内,并与徽标(unsplash.it中的随机图像)对齐。我希望此设置在780px以上激活,因此我将代码放入了媒体查询中,但它不起作用。浏览器甚至没有“看到”我给了它display: grid命令。

* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
  font-family: Century Gothic, Tahoma, sans-serif;
  font-size: 16px;
}

a {
  color: inherit;
  text-decoration: none;
}

body {
  background: black;
  margin-bottom: 1000px;
}


/* -----DROPDOWN MOBILE MENU----- */

header {
  background: rgba(217, 30, 24, 1);
  position: fixed;
  width: 100vw;
  z-index: 999;
  border: solid white 0px;
  display: flex;
  justify-content: center;
}

header .img {
  width: 10em;
  height: 3em;
  margin: 1em;
  border: solid black 1px;
  transform: scale(1, 1);
  transition: transform ease-in-out 250ms;
  border-radius: 5px;
}

header .img:hover {
  transform: scale(1.025, 1.025);
}

header nav {
  background: rgba(217, 30, 24, .85);
  width: 100vw;
  border: solid white 0px;
  position: absolute;
  top: 100%;
  left: 0;
  display: flex;
  justify-content: center;
}

header nav ul {
  width: 20%;
  border: solid white 0px;
  display: flex;
  flex-flow: column;
  align-items: center;
}

header nav ul li {
  list-style: none;
  color: white;
  padding: .5rem;
}

header nav ul li:hover {
  color: rgba(245, 245, 245, .7);
  cursor: pointer;
}


/* -----QUERY FOR LARGE SCREENS----- */

@media (min-width:780px) {
  header {
    border: solid white 1px;
    display: grid;
    grid-template-columns: 1fr auto 1fr 1fr;
    align-items: center;
  }
  header .img {
    border: solid blac 3px;
    border-radius: 5px;
    width: 10rem;
    height: 3rem;
    transform: scale(1, 1);
    transition: transform ease-in-out 250ms;
    grid-column: 2 / 3;
  }
  header .img:hover {
    transform: scale(1.025, 1.025);
  }
  header nav {
    background: rgba(217, 30, 24, 1);
    border: solid white 1px;
    width: 50vw;
    grid-column: 4 / 5;
  }
  header nav ul {
    display: flex;
    flex-flow: row;
    list-style: none;
    border: solid white 0px;
    width: 100%;
    justify-content: space-around;
    cursor: pointer;
  }
  header nav ul li::before {
    content: '';
    display: block;
    height: 2px;
    width: 0%;
    background: ivory;
    position: absolute;
    bottom: 0px;
    transition: ease-in-out .25s;
    -webkit-transition-duration: .25s;
    -o-transition-duration: .25s;
    -moz-transition-duration: .25s;
  }
  header nav ul li {
    color: white;
    display: flex;
    border: solid black 0px;
    justify-content: center;
    align-items: center;
    transition-duration: .5s;
    -webkit-transition-duration: .5s;
    -o-transition-duration: .5s;
    -moz-transition-duration: .5s;
    position: relative;
    padding: 0;
    padding-bottom: .5rem;
  }
  header nav ul li:hover {
    color: rgba(245, 245, 245, .7);
  }
  header nav ul li:hover::before {
    width: 100%;
  }
}


/* -----END LARGE SCREENS QUERY----- */
<header>
  <a href="#"> <img class="img" src="//unsplash.it/300/100">
  </a>
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Products</a></li>
      <li><a href="#">Support us</a></li>
      <li><a href="#">Contacts</a></li>
    </ul>
  </nav>
</header>
html css media-queries css-grid
1个回答
1
投票

您的<nav>具有

position: absolute

一旦删除它,您的<nav>将进入网格。

© www.soinside.com 2019 - 2024. All rights reserved.