简体   繁体   中英

Keeping an icon in place

I'm still new to CSS, and I'm having a go at building a responsive navbar. I have it so it's initially horizontal, but all elements apart from the first disappear when screen width is under 600px, and then be displayed vertically when an icon is clicked. The issue I'm having is I would like the icon to stay inline with the first element in my navbar, but once clicked, it appears at the bottom of the bar. This makes sense, because in my code they're being displayed vertically in order, but I'm not sure what properties to look at to achieve what I want.

Here is the HTML

<div id="navbar">
<a href="#" class="navbar-content">a</a>
<a href="#" class="navbar-content">b</a>
<a href="#" class="navbar-content">c</a>
<a href="#" class="navbar-content">d</a>
<a href="#" class="navbar-content">e</a>
<a href="#" class="navbar-content">f</a>
<i class="navbar-content fa fa-bars"></i>
</div>

& CSS

#navbar {
      text-align: right;
      width: 100%;
 }

 .navbar-content {
      vertical-align: middle;
      text-decoration: none;
 }

 .fa-bars {
      display: none;
 }

 body {
     background-color: gray;
 }

 @media screen and (max-width: 600px) {
     #navbar .navbar-content:not(:first-child) {
          display: none;
     }

     #navbar .navbar-content.fa-bars {
          display: inline-block;
     }

     #navbar.responsive .navbar-content:not(:last-child) {
          display: block;
          text-align: left;
     }
}

& Js

let navButton = document.querySelector(".fa-bars");
let navbar = document.querySelector("#navbar");

navButton.addEventListener("click", function() {
    if (navbar.className === "") {
        navbar.className = "responsive";
    } else {
        navbar.className = "";
    }
});

Edit: Solution -

@media screen and (max-width: 600px) {
    #navbar {
    position: relative;
}

#navbar .navbar-content:not(:first-child) {
    display: none;
}

#navbar .navbar-content:first-child {
    padding-right: 28px;
}

#navbar .navbar-content.fa-bars {
    display: inline-block;
    position: absolute;
    top: 3px;
    right: 5px;
}

#navbar.responsive .navbar-content:not(:last-child) {
    display: block;
    text-align: left;
}

plz try this code..

css

*,
*:after,
*:before {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

@media screen and (max-width: 600px) {
    #navbar {
        position: relative;
        padding-right: 40px;
    }
    #navbar .navbar-content.fa-bars {
        position: absolute;
        right: 20px;
        top: 0;
        z-index: 9;
        cursor: pointer;
    }
    .responsive .navbar-content:not(:first-child) {
        display: inline-block !important;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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