2017-12-26 12 views
1

Ich versuche, ein reaktives Nav mit einem Logo aus dem W3C Beispiel zu erstellen. Ich habe jedoch ein Problem mit dem Nav gefunden. Wenn ich das Element auf der rechten Seite der Seite schwebe, ist die Reihenfolge der Nav-Elemente durcheinander. Ich habe sie auf der Desktop-Version neu geordnet, aber ich kann nicht scheinen, sie im mobilen nav richtig zu bestellen. Ich habe versucht, Schwimmern zu verwenden, aber das vermasselt nur das Styling des Dropdowns.Responsive nav Reihenfolge umgekehrt, wenn es in mobile Medienabfrage geht

Codepen

</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> 

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 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; 
    } 


} 

JS

function myFunction() { 
    var x = document.getElementById("myTopnav"); 
    if (x.className === "topnav") { 
     x.className += " responsive"; 
    } else { 
     x.className = "topnav"; 
    } 
} 

Antwort

0

ich zusätzliche Markup hinzufügen würde und Ordnung in HTML ändern und dann auf CSS-Selektoren zwei .Behälter-nav hinzufügen

function myFunction() { 
 
    var x = document.getElementById("myTopnav"); 
 
    if (x.className === "topnav") { 
 
     x.className += " responsive"; 
 
    } else { 
 
     x.className = "topnav"; 
 
    } 
 
}
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; 
 
    /* \t 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; 
 
    /* \t \t 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; 
 
    /* \t \t position: relative;*/ 
 
    top: 0px; 
 
    /* \t \t 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; 
 
    } 
 
}
</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>

Ich kann auch vorschlagen, ein wenig über die Spezifität zu lesen :)

+0

Danke, das hat funktioniert, aber jetzt ist das Hamburgermenü unter dem Logo, das eine größere Navigationsleiste erstellt. es geht aber an die richtige Stelle wenn es angeklickt wird. – 3245737

+0

Ich habe CSS geändert. Probieren Sie es jetzt! :) – sakul

0

ich Ihren Code ein wenig geändert haben, vor allem einen Behälter für die klickbare Links hinzugefügt, verschoben auf das Symbol in der top und bestellung kann gleich sein.

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> 

Die oben sollte in der gleichen Reihenfolge angezeigt werden können, die Sie wollen. Hoffe das hilft!