Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Only one div of two sharing a class with different IDs showing up

I have a problem where I’ve got two divs that derive from the same class but with different IDs. Only the one that’s defined in the HTML code first will show up (the following image is what the page looks like when leftMenu is created first).

Webpage when leftMenu is defined first

Here’s the HTML code that places the divs:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<body>
    <div class = "sideMenu" id = "leftMenu">
        leftMenu
    </div>
    
    <div class = "sideMenu" id = "rightMenu">
        rightMenu
    </div>
</body>

I assume my problem is my lack of experience specifically with cascading stylesheets so I’ve included all the code in my .css file:

* {
    background-color: #001005;
    color: #00aa00;
    font-family: consolas;
    margin: 0px;
    overflow: hidden;
    padding: 0px;
    position: relative;
} /* default attributes for all elements */

.sideMenu {
    background-color: #00200a;
    border: 2px solid #00aa00;
    border-bottom: 0px;
    border-top: 0px;
    min-width: 15vw;
    max-width: 20vw;
    overflow-y: visible;
    width: fit-content;
    height: 100vh;
}  /* shared attributes of side menus */

#leftMenu {
    left: 0vw;
    top: 0vh;
} /* attributes for the left menu */

#rightMenu {
    left: 100vw;
    top: 0vh;
    transform: translate(-100%, 0%);
} /* attributes for the right menu */

It looks like everything should be working fine, but it isn’t. Sorry if this is really basic, it’s been years since I touched HTML, CSS, or JS.

>Solution :

Your current code sets all of the positions correctly, but is still using the default position: relative which means the right nav will be positioned below the left nav (and off screen, since the left nav is 100vh)

Assuming you want your navs to be visible at any scroll position on the page, I would recommend using: position: fixed;

* {
  background-color: #001005;
  color: #00aa00;
  font-family: consolas;
  margin: 0px;
  overflow: hidden;
  padding: 0px;
  position: relative;
}

.sideMenu {
  position: fixed;
  background-color: #00200a;
  border: 2px solid #00aa00;
  border-bottom: 0px;
  border-top: 0px;
  min-width: 15vw;
  max-width: 20vw;
  overflow-y: visible;
  width: fit-content;
  height: 100vh;
}

#leftMenu {
  left: 0vw;
  top: 0vh;
}

#rightMenu {
  left: 100vw;
  top: 0vh;
  transform: translate(-100%, 0%);
}
<body>
  <div class="sideMenu" id="leftMenu">
    leftMenu
  </div>

  <div class="sideMenu" id="rightMenu">
    rightMenu
  </div>
</body>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading