Home > other >  HTML allign last navbar item to the right
HTML allign last navbar item to the right

Time:10-18

I have a problem. I created the following navbar:

* {
    box-sizing: border-box;
    font-family: -apple-system, BlinkMacSystemFont, "segoe ui", roboto, oxygen, ubuntu, cantarell, "fira sans", "droid sans", "helvetica neue", Arial, sans-serif;
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    margin: 0;
    padding: 0;
}

body {
    background: linear-gradient( rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url(src/assets/images/background2.jpg);
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    background-color:#464646;
}


a {
    text-decoration: none;
}

li {
    list-style: none;
}

.navbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px;
    background-color: #547430;
    color: #fff;
}

.nav-links a {
    color: #fff;
}


/* LOGO */

.logo {
    font-size: 2vw;
    font-family: Rockwell;
    text-align: center;
    position: absolute;
    width: 30%;
    left: 50%;
    margin-left: -15%;
}


/* NAVBAR MENU */

.menu {
    display: flex;
    gap: 1em;
    font-size: 18px;
    z-index: 9;
}

.menu li:hover {
    background-color: #7ead47;
    border-radius: 5px;
    transition: 0.3s ease;
}

.menu li {
    padding: 5px 14px;
    font-weight: bold;
}


/* DROPDOWN MENU */

.services {
    position: relative;
}

.dropdown {
    background-color: rgb(1, 139, 139);
    padding: 1em 0;
    position: absolute;
    /*WITH RESPECT TO PARENT*/
    display: none;
    border-radius: 8px;
    top: 35px;
}

.dropdown li li {
    margin-top: 10px;
}

.dropdown li {
    padding: 0.5em 1em;
    width: 8em;
    text-align: center;
}

.dropdown li:hover {
    background-color: #4c9e9e;
}

.account {
    position: absolute;
    right: 0;
}

.services:hover .dropdown {
    display: block;
}


/*RESPONSIVE NAVBAR MENU STARTS*/


/* CHECKBOX HACK */

input[type=checkbox] {
    display: none;
}


/*HAMBURGER MENU*/

.hamburger {
    display: none;
    font-size: 24px;
    user-select: none;
}


/* APPLYING MEDIA QUERIES */

@media (max-width: 1000px) {
    .menu {
        display: block;
        position: absolute;
        background-color: #547430;
        margin-top: 5px;
        text-align: left;
        height: 100%;
        width: 100%;
        padding: 16px 0;
        transition: all 0.3s ease;
        left: -100%;
    }
    .logo {
        font-size: 5vw;
    }
    .menu li:hover {
        display: inline-block;
        background-color: #7ead47;
        transition: 0.3s ease;
    }
    .menu li li {
        margin-top: 12px;
    }
    input[type=checkbox]:checked~.menu {
        left: 0%;
    }
    .hamburger {
        display: block;
    }
    .dropdown {
        left: 50%;
        top: 30px;
        transform: translateX(35%);
    }
    .dropdown li:hover {
        background-color: #4c9e9e;
    }
}






/*RESPONSIVE NAVBAR MENU STARTS*/
/* CHECKBOX HACK */
input[type=checkbox]{
  display: none;
}
/*HAMBURGER MENU*/
.hamburger {
  display: none;
  font-size: 24px;
  user-select: none;
}
/* APPLYING MEDIA QUERIES */
@media (max-width: 1000px) {

  .menu {
    display: block;
    position: absolute;
    background-color: #547430;
    margin-top: 5px;
    text-align: left;
    height: 100%;
    width: 100%;
    padding: 16px 0;
    transition: all 0.3s ease;
    left: -100%;
  }

  .logo {
    font-size: 5vw;
  }

  .menu li:hover {
    display: inline-block;
    background-color: #7ead47;
    transition: 0.3s ease;
  }

  .menu li   li {
    margin-top: 12px;
  }

  input[type=checkbox]:checked ~ .menu{
    left: 0%;
  }

  .hamburger {
    display: block;
  }

  .dropdown {
    left: 50%;
    top: 30px;
    transform: translateX(35%);
  }

  .dropdown li:hover {
    background-color: #4c9e9e;
  }

}
<nav class="navbar">
  <div class="logo">Audio Diary</div>
  <ul class="nav-links">

    <!-- USING CHECKBOX HACK -->
    <input type="checkbox" id="checkbox_toggle" />
    <label for="checkbox_toggle" class="hamburger">&#9776;</label>

    <!-- NAVIGATION MENUS -->
    <div class="menu">
      <li><a href="/">Home</a></li>
      <li><a href="/">Explore</a></li>
      <li><a href="/">Info</a></li>
      <li class="account" id="login"><a href="/login">Login</a></li>
      <li class="services">
        <a href="/">Services</a>
        <ul class="dropdown">
          <li><a href="/">Dropdown 1 </a></li>
          <li><a href="/">Dropdown 2</a></li>
          <li><a href="/">Dropdown 2</a></li>
          <li><a href="/">Dropdown 3</a></li>
          <li><a href="/">Dropdown 4</a></li>
        </ul>
      </li>
    </div>
  </ul>
</nav>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

But now I want the items in the navbar with the class: account to be alligned on the right. I already tried

float:right;

and

margin-left: auto

in the .account class in the css, but that doesn't result in any changes. What am I doing wrong and how can I achieve this?

CodePudding user response:

Use text-align: right for account class.

CodePudding user response:

Use position: absolute with right: 0 to align to the left

* {
  box-sizing: border-box;
  font-family: -apple-system, BlinkMacSystemFont, "segoe ui", roboto, oxygen, ubuntu, cantarell, "fira sans", "droid sans", "helvetica neue", Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  margin: 0;
  padding: 0;
}

body {
  background: linear-gradient( rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(src/assets/images/background2.jpg);
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  background-color: #464646;
}

a {
  text-decoration: none;
}

li {
  list-style: none;
}

.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
  background-color: #547430;
  color: #fff;
}

.nav-links a {
  color: #fff;
}


/* LOGO */

.logo {
  font-size: 2vw;
  font-family: Rockwell;
  text-align: center;
  position: absolute;
  width: 30%;
  left: 50%;
  margin-left: -15%;
}


/* NAVBAR MENU */

.menu {
  display: flex;
  gap: 1em;
  font-size: 18px;
  z-index: 9;
}

.menu li:hover {
  background-color: #7ead47;
  border-radius: 5px;
  transition: 0.3s ease;
}

.menu li {
  padding: 5px 14px;
  font-weight: bold;
}


/* DROPDOWN MENU */

.account {
  position: relative;
}

.dropdown {
  background-color: rgb(1, 139, 139);
  padding: 1em 0;
  position: absolute;
  /*WITH RESPECT TO PARENT*/
  display: none;
  border-radius: 8px;
  top: 35px;
}

.dropdown li li {
  margin-top: 10px;
}

.dropdown li {
  padding: 0.5em 1em;
  width: 8em;
  text-align: center;
}

.dropdown li:hover {
  background-color: #4c9e9e;
}

.account {
  position: absolute;
  right: 0;
}

.account:hover .dropdown {
  display: block;
}


/*RESPONSIVE NAVBAR MENU STARTS*/


/* CHECKBOX HACK */

input[type=checkbox] {
  display: none;
}


/*HAMBURGER MENU*/

.hamburger {
  display: none;
  font-size: 24px;
  user-select: none;
}


/* APPLYING MEDIA QUERIES */

@media (max-width: 1000px) {
  .menu {
    display: block;
    position: absolute;
    background-color: #547430;
    margin-top: 5px;
    text-align: left;
    height: 100%;
    width: 100%;
    padding: 16px 0;
    transition: all 0.3s ease;
    left: -100%;
  }
  .logo {
    font-size: 5vw;
  }
  .menu li:hover {
    display: inline-block;
    background-color: #7ead47;
    transition: 0.3s ease;
  }
  .menu li li {
    margin-top: 12px;
  }
  input[type=checkbox]:checked~.menu {
    left: 0%;
  }
  .hamburger {
    display: block;
  }
  .dropdown {
    left: 50%;
    top: 30px;
    transform: translateX(35%);
  }
  .dropdown li:hover {
    background-color: #4c9e9e;
  }
}
<nav class="navbar">
  <div class="logo">Audio Diary</div>
  <ul class="nav-links">

    <!-- USING CHECKBOX HACK -->
    <input type="checkbox" id="checkbox_toggle" />
    <label for="checkbox_toggle" class="hamburger">&#9776;</label>

    <!-- NAVIGATION MENUS -->
    <div class="menu">
      <li><a href="/">Home</a></li>
      <li><a href="/">Explore</a></li>
      <li><a href="/">Info</a></li>
      <li class="account" id="login"><a href="/login">Login</a></li>
    </div>
  </ul>
</nav>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Update:

As the dropdown disappears when go to it . Well the reason for this to disappear is that there is some space between dropdown and btn on hover . So when you try to go to dropdown the space remove the hover event on btn and dropdown disappears . This behavior will not seen when go to dropdown fastly .

Solution :

  • Either trigger dropdown when clicked on btn and close it when clicked again or outside

  • Or by removing the space between btn and dropdown

I have shown the 2nd solution and reduced .dropdown {top: 35px;} to .dropdown {top: 30px;}

* {
  box-sizing: border-box;
  font-family: -apple-system, BlinkMacSystemFont, "segoe ui", roboto, oxygen, ubuntu, cantarell, "fira sans", "droid sans", "helvetica neue", Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  margin: 0;
  padding: 0;
}

body {
  background: linear-gradient( rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(src/assets/images/background2.jpg);
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  background-color: #464646;
}

a {
  text-decoration: none;
}

li {
  list-style: none;
}

.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
  background-color: #547430;
  color: #fff;
}

.nav-links a {
  color: #fff;
}


/* LOGO */

.logo {
  font-size: 2vw;
  font-family: Rockwell;
  text-align: center;
  position: absolute;
  width: 30%;
  left: 50%;
  margin-left: -15%;
}


/* NAVBAR MENU */

.menu {
  display: flex;
  gap: 1em;
  font-size: 18px;
  z-index: 9;
}

.menu li:hover {
  background-color: #7ead47;
  border-radius: 5px;
  transition: 0.3s ease;
}

.menu li {
  padding: 5px 14px;
  font-weight: bold;
}


/* DROPDOWN MENU */

.services {
  position: relative;
}

.dropdown {
  background-color: rgb(1, 139, 139);
  padding: 1em 0;
  position: absolute;
  /*WITH RESPECT TO PARENT*/
  display: none;
  border-radius: 8px;
  top: 30px;
}

.dropdown li li {
  margin-top: 10px;
}

.dropdown li {
  padding: 0.5em 1em;
  width: 8em;
  text-align: center;
}

.dropdown li:hover {
  background-color: #4c9e9e;
}

.account {
  position: absolute;
  right: 0;
}

.services:hover .dropdown {
  display: block;
}


/*RESPONSIVE NAVBAR MENU STARTS*/


/* CHECKBOX HACK */

input[type=checkbox] {
  display: none;
}


/*HAMBURGER MENU*/

.hamburger {
  display: none;
  font-size: 24px;
  user-select: none;
}


/* APPLYING MEDIA QUERIES */

@media (max-width: 1000px) {
  .menu {
    display: block;
    position: absolute;
    background-color: #547430;
    margin-top: 5px;
    text-align: left;
    height: 100%;
    width: 100%;
    padding: 16px 0;
    transition: all 0.3s ease;
    left: -100%;
  }
  .logo {
    font-size: 5vw;
  }
  .menu li:hover {
    display: inline-block;
    background-color: #7ead47;
    transition: 0.3s ease;
  }
  .menu li li {
    margin-top: 12px;
  }
  input[type=checkbox]:checked~.menu {
    left: 0%;
  }
  .hamburger {
    display: block;
  }
  .dropdown {
    left: 50%;
    top: 30px;
    transform: translateX(35%);
  }
  .dropdown li:hover {
    background-color: #4c9e9e;
  }
}


/*RESPONSIVE NAVBAR MENU STARTS*/


/* CHECKBOX HACK */

input[type=checkbox] {
  display: none;
}


/*HAMBURGER MENU*/

.hamburger {
  display: none;
  font-size: 24px;
  user-select: none;
}


/* APPLYING MEDIA QUERIES */

@media (max-width: 1000px) {
  .menu {
    display: block;
    position: absolute;
    background-color: #547430;
    margin-top: 5px;
    text-align: left;
    height: 100%;
    width: 100%;
    padding: 16px 0;
    transition: all 0.3s ease;
    left: -100%;
  }
  .logo {
    font-size: 5vw;
  }
  .menu li:hover {
    display: inline-block;
    background-color: #7ead47;
    transition: 0.3s ease;
  }
  .menu li li {
    margin-top: 12px;
  }
  input[type=checkbox]:checked~.menu {
    left: 0%;
  }
  .hamburger {
    display: block;
  }
  .dropdown {
    left: 50%;
    top: 30px;
    transform: translateX(35%);
  }
  .dropdown li:hover {
    background-color: #4c9e9e;
  }
}
<nav class="navbar">
  <div class="logo">Audio Diary</div>
  <ul class="nav-links">

    <!-- USING CHECKBOX HACK -->
    <input type="checkbox" id="checkbox_toggle" />
    <label for="checkbox_toggle" class="hamburger">&#9776;</label>

    <!-- NAVIGATION MENUS -->
    <div class="menu">
      <li><a href="/">Home</a></li>
      <li><a href="/">Explore</a></li>
      <li><a href="/">Info</a></li>
      <li class="account" id="login"><a href="/login">Login</a></li>
      <li class="services">
        <a href="/">Services</a>
        <ul class="dropdown">
          <li><a href="/">Dropdown 1 </a></li>
          <li><a href="/">Dropdown 2</a></li>
          <li><a href="/">Dropdown 2</a></li>
          <li><a href="/">Dropdown 3</a></li>
          <li><a href="/">Dropdown 4</a></li>
        </ul>
      </li>
    </div>
  </ul>
</nav>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related