简体   繁体   English

为什么我的导航栏对齐水平和垂直媒体查询?

[英]Why is my navbar alignment out for both horizontal and vertical media queries?

I am trying to create a navbar in pure html/css that at less than 600px width is horizontal with a horizontal set of links inline (no bullet points).我试图在纯 html/css 中创建一个导航栏,宽度小于 600px 是水平的,带有一组水平的内联链接(没有项目符号)。 At more than 600px width, I want a vertical navbar with a vertical list.宽度超过 600 像素,我想要一个带有垂直列表的垂直导航栏。

However, my horizontal version keeps giving me a vertically stacked set with bullet points and my vertical version has the word 'links' inline with 'paragraph' when I want it underneath.然而,我的水平版本一直给我一个带有项目符号的垂直堆叠集合,当我想要它在下面时,我的垂直版本将“链接”这个词与“段落”内联。

I'm at a fault to see what I'm doing wrong.我有错,看看我做错了什么。 I tried scrubbing it out and starting again which worked until I put in my media query and it all went wrong again.我试着把它擦掉并重新开始,直到我输入媒体查询,这一切又出错了。 Any help appreciated.任何帮助表示赞赏。

<header>
  <nav id="navbar">
    <h1>HTML Basics</h1>
    <ul>
      <li>Documents</li>
      <li>Headings</li>
      <li>Paragraph</li>
      <li>Links</li>
      <li>Images</li>
    </ul>
  </nav>
</header>
#navbar{
  background: red;
  width: 100%;
  height: 20%;
  margin: auto;
  position: fixed;
  display: inline;
  float: top;
  top: 0;
  left: 0;
}

@media screen and (min-width:600px){
  #navbar{
  background: red;
  width: 22%;
  height: 100%;
  margin: auto;
  position: fixed;
  display: block;
  top: 0;
  left: 0;
}


li{
  list-style: none;
 padding-right: 20px;
  padding-bottom: 20px;
  display: inline-block;
  margin-left: 10%;
  float: left;
    font-family: 'Nunito Sans', sans-serif;
  text-decoration: none;
  font-size: 12px;
}

@media only screen and (min-width:600px){
  li{
  list-style-type: none;
  display: block;
  font-family: 'Nunito Sans', sans-serif;
  text-decoration: none;
  font-size: 20px;
}
}

li:hover{
  color:#911f32;
  font-size: 18px;

}

You're missing a closing curly brace in your first Media Query.您在第一个媒体查询中缺少右花括号。 Try this:尝试这个:

#navbar {
  background: red;
  width: 100%;
  height: 20%;
  margin: auto;
  position: fixed;
  display: inline;
  float: top;
  top: 0;
  left: 0;
}

@media screen and (min-width:600px) {
  #navbar {
    background: red;
    width: 22%;
    height: 100%;
    margin: auto;
    position: fixed;
    display: block;
    top: 0;
    left: 0;
  } // <-- NOTE! This curly brace was missing...
}


li {
  list-style: none;
  padding-right: 20px;
  padding-bottom: 20px;
  display: inline-block;
  margin-left: 10%;
  float: left;
  font-family: 'Nunito Sans', sans-serif;
  text-decoration: none;
  font-size: 12px;
}

@media only screen and (min-width:600px){
  li {
    list-style-type: none;
    display: block;
    font-family: 'Nunito Sans', sans-serif;
    text-decoration: none;
    font-size: 20px;
  }
}

li:hover{
  color: #911f32;
  font-size: 18px;
}

You had some errors in there:你在那里有一些错误:

  • You used min-width both times instead of min/max您两次都使用了min-width而不是 min/max

  • For the horizontal version you need to allow full width (100%)对于水平版本,您需要允许全宽 (100%)

  • You forgot a closing curly brace你忘记了右花括号

  • There is no float: top;没有float: top; (but that has no effect anyway) (但这无论如何都没有效果)

 #navbar { background: red; width: 100%; height: 20%; margin: auto; position: fixed; display: inline; top: 0; left: 0; } @media screen and (max-width:600px) { #navbar { background: red; margin: auto; position: fixed; display: block; top: 0; left: 0; } li { list-style-type: none; padding-right: 20px; padding-bottom: 20px; display: inline-block; margin-left: 10%; float: left; font-family: 'Nunito Sans', sans-serif; text-decoration: none; font-size: 12px; } } @media only screen and (min-width:600px) { #navbar { width: 20%; height: 100%; } li { list-style-type: none; display: block; font-family: 'Nunito Sans', sans-serif; text-decoration: none; font-size: 20px; } } li:hover { color: #911f32; font-size: 18px; }
 <header> <nav id="navbar"> <h1>HTML Basics</h1> <ul> <li>Documents</li> <li>Headings</li> <li>Paragraph</li> <li>Links</li> <li>Images</li> </ul> </nav> </header>

  1. Add style for your list-item element (600px and down).为您的列表项元素添加样式(600 像素及以下)。
  2. Comment unnecessary style (600px and up).注释不必要的样式(600px 及以上)。

Look at CSS code below.看看下面的 CSS 代码。

 #navbar{ /* ... */ } /* === Add this code */ li { list-style-type: none; display: inline-block; } @media screen and (min-width:600px){ #navbar { /* ... */ } /* === You don't need "display: inline-block" and "float: left" */ li { list-style: none; padding-right: 20px; padding-bottom: 20px; /* display: inline-block; */ margin-left: 10%; /* float: left; */ font-family: 'Nunito Sans', sans-serif; text-decoration: none; font-size: 12px; } /*...*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM