Search code examples
javascripthtmlcssdomcss-position

How to keep an HTML button element always in the same spot relative to the page size?


I have a button element on my webpage which I'm using as a mobile hamburger menu. I'm trying to keep it in the same spot relative to the page size with CSS.

Requirements

  • Needs to be positioned relative to page i.e.: position: absolute; left: 90%-page-size;
  • Cannot spill off the page on mobile

Is this possible with CSS? I'm not opposed to using a little JavaScript.

A code snippet is attached below. The element I'm targeting is a <button></button> element with a class of hamburger-menu.

body {
  overflow-x: hidden;
  font-size: large;
  margin: 0;
}

h1,
h2,
h3,
h4 {
  font-family: 'Montserrat', sans-serif;
}

.nav-bar {
  z-index: 98;
  background-color: rgba(204, 204, 204, 0.8);
  padding: 15px;
}

.nav-img {
  height: 100px;
}

.nav-options {
  text-align: right;
}

.nav-option {
  border: none;
  background-color: rgba(204, 204, 204, 0.1);
  height: 100px;
  width: 150px;
  font-size: large;
  cursor: pointer;
  transition: all 0.5s ease-out;
  position: relative;
  bottom: 15px;
}

.nav-option:hover {
  background-color: rgba(204, 204, 204, 0.1);
  color: white;
}

p,
ul,
ol,
li,
select {
  font-family: 'Poppins', sans-serif;
}

.line {
  width: 50px;
  background-color: white;
  z-index: 99;
  height: 0.5px;
}

.hamburger-menu {
  background-color: transparent;
  border: none;
  display: inline-block;
}

.mobile-nav {
  display: none;
}

.mobile-menu {
  margin: 50px;
  padding: 0;
  z-index: 98;
  position: fixed;
  right: 0%;
  bottom: -6%;
  background-color: rgba(204, 204, 204, 0.8);
  width: 100%;
  height: 110%;
  margin-left: auto;
  margin-right: auto;
  padding: 50px;
}

.mobile-options {
  position: absolute;
  list-style: none;
  top: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  flex-direction: column;
  height: 110%;
}

.mobile-option {
  width: 100%;
  height: 50px;
  font-size: large;
  letter-spacing: 2px;
  line-height: 100%;
  text-align: center;
  background-color: rgba(204, 204, 204, 0.8);
  border: none;
  padding-right: 60px;
}

.exit-btn {
  width: 50px;
  background-color: transparent;
  border: none;
  font-size: 4rem;
  color: white;
  font-family: 'Montserrat', sans-serif;
  font-weight: lighter;
  float: right;
  position: absolute;
  bottom: 75%;
  left: 75%;
}

@media screen and (max-width: 830px) {
  .desktop-nav {
    display: none;
  }
  .mobile-nav {
    display: inline-block;
  }
}
<div class="nav-bar">
  <nav class="desktop-nav">
    <a href="index.html"><img src="https://picsum.photos/100" class="nav-img"></a>
    
    <div class="nav-options">
      <button class="nav-option">About Us</button>
      <button class="nav-option">Classes</button>
      <button class="nav-option">Contact Us</button>
    </div>
  </nav>
  
  <nav class="mobile-nav">
    <a href="index.html"><img src="https://picsum.photos/100" class="nav-img"></a>
    
    <div class="nav-options">
      <button class="hamburger-menu" id="mobile-menu-enter">
          <div class="line"></div><br>
          <div class="line"></div><br>
          <div class="line"></div>
      </button>
    </div>
  </nav>
</div>


Solution

  • Have you tried

    position: fixed;
    right: 10px;