响应的导航顺序在进入移动媒体查询时反转

时间:2017-12-26 15:59:09

标签: html5 css3 responsive-design nav

我尝试使用w3c示例中的徽标创建响应式导航。但是,我遇到了导航问题。当我将元素浮动到页面右侧时,导航项的顺序混乱。我已经在桌面版上重新订购了它们,但我似乎无法在移动导航中正确订购它们。我尝试过使用浮动,但这只是弄乱了下拉列表的样式。

Codepen

alert.show();
//setContentView(R.layout.activity_main); // <-- Needs to be removed

CSS

    </head>
<body>
<div id="container">
<nav class="topnav" id="myTopnav">
   <img src="img/logo.png" alt="Logo">
         <a href="#home" class="active">Home</a>
            <a href="#contact">Contact</a>
            <a href="#news">News</a>
            <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
</nav>

</body>
</html>

JS

  html{
    min-height:100%;
}

body{
    margin:0 auto;
    padding:0;
    font-family: 'Roboto', sans-serif;
    height:100%;
    width:100%;
}

#container{
    margin:0 auto;
    height:100%;
    width:100%;
/* min-height:100%;*/
/*   position:relative;*/
}


/* Add a black background color to the top navigation */
.topnav {
    background-color: #333;
    overflow: hidden;
padding-left:20%;
    padding-right:20%;
    margin:0 auto;
}


.topnav img{
    float:left;
    padding-top:5px;
    padding-bottom:5px;
    padding-left:5px;
    display: inline-block;
    padding-right:40px;


}

/* Style the links inside the navigation bar */
.topnav a {
    float: right;
/*    position: relative;*/
    display: inline;
/*    display: inline-block;*/
    color: #f2f2f2;
      padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
    background-color: #ddd;
    color: black;
}

/* Add an active class to highlight the current page */
.active {
    background-color: #4CAF50;
    color: white;
}

/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
    display: none;
}



/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 600px) {

    .topnav {
        background-color: #333;
    overflow: hidden;
padding: 0;
    margin:0 auto;
/*      clear: both;*/
}


  .topnav.responsive {
        position: relative;
        height:210px; 

    }

  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
        clear: both;
/*      position: relative;*/
        top:0px;
/*      height:100%;*/
    float: none;
    display: block;
/*    text-align: center;*/
  }

  .topnav a {
        display: none;
    }

  .topnav a.icon {
    float: right;
    display: block;
  } 


}

2 个答案:

答案 0 :(得分:0)

我会在html中添加额外的标记和更改顺序,然后添加到css两个选择器 .container-nav

&#13;
&#13;
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
&#13;
html {
  min-height: 100%;
}

body {
/*   margin: 0 auto; */
/*   padding: 0; */
/*   font-family: "Roboto", sans-serif; */
/*   height: 100%; */
/*   width: 100%; */
}

#container {
/*   margin: 0 auto; */
/*   height: 100%; */
/*   width: 100%; */
  /* min-height:100%;*/
  /*   position:relative;*/
}

/* Add a black background color to the top navigation */
.topnav {
  background-color: #333;
  overflow: hidden;
  padding-left: 20%;
  padding-right: 20%;
/*   margin: 0 auto; */
}

.topnav img {
  float: left;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 5px;
  display: inline-block;
  padding-right: 40px;
}

/* Style the links inside the navigation bar */
.topnav .container-nav{
  float:right;
}
.topnav a {
  float: left;
  /*	  position: relative;*/
/*   display: inline-block; */
/*      display: inline-block; */
  color: #f2f2f2;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}

/* Add an active class to highlight the current page */
.active {
  background-color: #4caf00;
  color: white;
}

/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
  display: none;
}

/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 600px) {
  .topnav {
    background-color: #333;
    height: 52px;
    overflow: hidden;
    padding: 0;
    margin: 0 auto;
    /*		clear: both;*/
  }

  .topnav.responsive {
    position: relative;
    height: 210px;
  }

  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav .container-nav{
    width: 100%;
  }
  .topnav.responsive a {
    clear: both;
    /*		position: relative;*/
    top: 0px;
    /*		height:100%;*/
    float: none;
    display: block;
/*        text-align: center; */
  }

  .topnav a {
    display: none;
  }

  .topnav .icon {
    position: relative;
    right: 0;
    top: -31.6px;
    float: right;
    display: block;
  }
}
&#13;
</head>

<body>
  <div id="container">
    <nav class="topnav" id="myTopnav">
      <img src="img/logo.png" alt="Logo">
      
      <div class="container-nav">
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
      <a href="#home" class="active">Home</a>
      </div>

      <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
    </nav>

</body>

</html>
&#13;
&#13;
&#13;

我还建议您阅读一些关于特异性的内容:)

答案 1 :(得分:0)

我稍微修改了你的代码,主要为可点击的链接添加了一个容器,在顶部移动了图标,顺序可以相同。

CSS:

html{
    min-height:100%;
}

body{
    margin:0 auto;
    padding:0;
    font-family: 'Roboto', sans-serif;
    height:100%;
    width:100%;
}

#container{
    margin:0 auto;
    height:100%;
    width:100%;
/* min-height:100%;*/
/*   position:relative;*/
}


/* Add a black background color to the top navigation */
.topnav {
    background-color: #333;
    overflow: hidden;
padding-left:20%;
    padding-right:20%;
    margin:0 auto;
}

.topnav .nav-item-container {
  float:right;
}
.topnav img{
    float:left;
    padding-top:5px;
    padding-bottom:5px;
    padding-left:5px;
    display: inline-block;
    padding-right:40px;


}

/* Style the links inside the navigation bar */
.topnav a {
    float: left;
/*    position: relative;*/
    display: inline;
/*    display: inline-block;*/
    color: #f2f2f2;
      padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
    background-color: #ddd;
    color: black;
}

/* Add an active class to highlight the current page */
.active {
    background-color: #4CAF50;
    color: white;
}

/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
    display: none;
}



/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 600px) {

    .topnav {
        background-color: #333;
    overflow: hidden;
padding: 0;
    margin:0 auto;
/*      clear: both;*/
}


  .topnav.responsive {
        position: relative;
        height:210px; 

    }

  .topnav .nav-item-container{
    padding-top:    0px; 
  }
  .topnav.responsive a.icon {
    position: relative;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
        clear: both;
/*      position: relative;*/
        top:0px;
/*      height:100%;*/
    float: none;
    display: block;
/*    text-align: center;*/
  }

  .topnav a {
        display: none;
    }

  .topnav a.icon {
    float: right;
    display: block;
  } 


}

HTML:

</head>
<body>
<div id="container">
<nav class="topnav" id="myTopnav">
<img src="img/logo.png" alt="Logo">
<div class="nav-item-container">
    <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
<a href="#home" class="active">Home</a>
            <a href="#contact">Contact</a>
            <a href="#news">News</a>


</div>
</nav>

</body>
</html>

以上应该能够以您想要的相同顺序显示。希望这有帮助!