1

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>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Ethan
  • 118
  • 1
  • 13
  • 4
    There is no attached code snippet. Look into [CSS viewport units](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Values_and_units#relative_length_units). – Sean Sep 30 '22 at 17:55
  • why not have it `relative` to the `html` element? – sal3jfc Sep 30 '22 at 17:55
  • if you want it left then its `left:0`, push it out abit `left:10px` etc, if you want it on the right the use `right:0`, or out a bit `right:10px` etc, `left: 90%-page-size` doesn't make much sense – Lawrence Cherone Sep 30 '22 at 17:57
  • @LawrenceCherone maybe OP meant `90vw` – GrafiCode Sep 30 '22 at 18:01
  • Is the size of the hamburger related to the size of the viewport? (I'm trying to understand exactly what 'the same spot' means in the context). – A Haworth Sep 30 '22 at 18:03
  • 1
    @GrafiCode then should use `right: 10vw` – Lawrence Cherone Sep 30 '22 at 18:07
  • @A Hawort I meant position on the page – Ethan Sep 30 '22 at 18:13
  • @GrafiCode I have already tried setting the position to 90vw. It gets crop off the page on mobile sizizes – Ethan Sep 30 '22 at 18:14
  • @Ethan you want it to scale too? Please update your question if so. – Sean Sep 30 '22 at 18:15
  • 'position on the page' - so you want to use units relative to the page (or do you?) Still not clear I'm afraid - do you want it positioned a fixed distance from the side of the user's phone, for example, or do you want it fixed at n% of the page's width? And please show us how you are sizing the hamburger - is that relative to the viewport size or fixed px for example? – A Haworth Sep 30 '22 at 18:50
  • Does this answer your question? [Sizing and positioning elements based on window width](https://stackoverflow.com/questions/10075524/sizing-div-based-on-window-width) – isherwood Sep 30 '22 at 19:58
  • @AHaworth yes I wanted to be fixed at n% of the screen's width. – Ethan Oct 01 '22 at 00:55

1 Answers1

0

Have you tried

position: fixed;
right: 10px;
Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63