Nick Wright
Nick Wright

Reputation: 125

Button positioning in completely wrong place to where it should

I've got a webpage with 2 different sections. Each are the height of the viewport. One has a button 'work' in the center. When this is clicked, that disappears and some links appear where it was. The same sort of thing applies for the next section.

I'm trying to add a reset function to remove the links and add the buttons back. I originally tried to make one button reset all sections but, after that didn't work, I'm now trying to make an individual button for each section.

I've done this but my problem is that the second section's reset button appears in the same place as the first section's one. Both should appear at the bottom right section of their respective sections.

function openSites(categoryType) {
  if (categoryType == "work") {
    var sites = document.getElementById("workSites");
    var button = document.getElementById("workButton");
  } else if (categoryType == "other") {
    var sites = document.getElementById("otherSites");
    var button = document.getElementById("otherButton");
  }
  sites.classList.add("show");
  sites.classList.remove("hide");
  button.classList.add("hide");
}

function reset(categoryType) {
  if (categoryType == "work") {
    var sites = document.getElementById("workSites");
    var button = document.getElementById("workButton");
  } else if (categoryType == "other") {
    var sites = document.getElementById("otherSites");
    var button = document.getElementById("otherButton");
  }
  sites.classList.remove("show");
  sites.classList.add("hide");
  button.classList.remove("hide");
}

function betterReset() {
  for (category in document.getElementsByClassName("category-container")) {
    document.write(category);
  }
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.page {
  display: inline-block;
  overflow: hidden;
  width: 100%;
  height: 100vh;
}

#page-1 {
  display: block;
  background-color: #3faae4;
}

#page-2 {
  display: block;
  background-color: #ffffff;
}

.pointer {
  cursor: pointer;
}

#work {
  height: 100%;
  width: 100%;
}

#other {
  height: 100%;
  width: 100%;
}

#workSites {
  height: 100%;
  width: 100%;
}

#otherSites {
  height: 100%;
  width: 100%;
}

.sites {
  list-style-type: none;
  height: 100%;
}

.site {
  padding: 50px 0px;
  flex-grow: 1;
  text-align: center;
}

.center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.category-container {
  height: 100%;
}

.category-button {
  border: solid 0.5px;
  padding: 60px;
}

.buttonClose {
  position: absolute;
  border: solid 0.5px;
  border-radius: 5px;
  right: 3px;
  bottom: 0px;
  width: 70px;
  height: 35px;
}

.show {
  display: block;
}

.hide {
  display: none;
}
<!DOCTYPE html>
<html>
<head>
  <title>Nick's site</title>
  <link rel="stylesheet" type="text/css" href="./styles3.css">
  <meta name="viewport" content="width= device-width, inital-scale=1">
</head>
<body>
  <div id="page-1" class="page">
    <div id="work">
      <div id="workButton" class="category-container center">
        <a class="category-button pointer" onclick="openSites('work')">Work</a>
      </div>
      <div id="workSites" class="hide">
        <ul class="sites center">
          <li class="site"><a class="pointer" href="#">Printfactory</a></li>
          <li class="site"><a class="pointer" href="#">Henry's Site</a></li>
        </ul>
        <button onclick="reset('work')" class="buttonClose pointer" style="z-index: 2;">
         Reset
       </button>
      </div>
    </div>
  </div>
  <div id="page-2" class="page">
    <div id="other">
      <div id="otherButton" class="category-container center">
        <a class="category-button pointer" onclick="openSites('other')">Other</a>
      </div>
      <div id="otherSites" class="hide">
        <ul class="sites center">
          <li class="site"><a class="pointer" href="#">#</a></li>
          <li class="site"><a class="pointer" href="#">#</a></li>
          <li class="site"><a class="pointer" href="#">#</a></li>
        </ul>
        <button onclick="reset('other')" class="buttonClose pointer" style="z-index: 2;">
          Reset2
        </button>
      </div>
    </div>
  </div>
</body>
</html>

Upvotes: 1

Views: 632

Answers (1)

Gerardo BLANCO
Gerardo BLANCO

Reputation: 5648

You are giving a position:absolute tu your reset buttons. This make them take the values of right and bottom relative to next parent with position:relative.In this case being the body tag.

To fix this, add position:relative to your parent divs.

#workSites,
#otherSites {
  position: relative;
}

Hope this helps :>

function openSites(categoryType) {
                if (categoryType == "work") {
                    var sites = document.getElementById("workSites");
                    var button = document.getElementById("workButton");
                } else if (categoryType == "other") {
                    var sites = document.getElementById("otherSites");
                    var button = document.getElementById("otherButton");
                }
                sites.classList.add("show");
                sites.classList.remove("hide");
                button.classList.add("hide");

            }
            function reset(categoryType) {
                if (categoryType == "work") {
                    var sites = document.getElementById("workSites");
                    var button = document.getElementById("workButton");
                } else if (categoryType == "other") {
                    var sites = document.getElementById("otherSites");
                    var button = document.getElementById("otherButton");
                }
                sites.classList.remove("show");
                sites.classList.add("hide");
                button.classList.remove("hide");
            }
            function betterReset() {
                for (category in document.getElementsByClassName("category-container")){
                    document.write(category);
                }
            }
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.page {
    display: inline-block;
    overflow: hidden;
    width: 100%;
    height: 100vh;
}
#page-1 {
    display: block;
    background-color: #3faae4;
}
#page-2 {
    display: block;
    background-color: #ffffff;
}
.pointer {
    cursor: pointer;
}
#work {
    height: 100%;
    width: 100%;
}
#other {
    height: 100%;
    width: 100%;
}
#workSites {
    height: 100%;
    width: 100%;
}
#otherSites {
    height: 100%;
    width: 100%;
}
.sites {
    list-style-type: none;
    height: 100%;
}
.site {
    padding: 50px 0px;
    flex-grow: 1;
    text-align: center;
}
.center {
    display: flex;
    align-items: center;
    justify-content: center;
}
.category-container {
    height: 100%;
}
.category-button {
    border: solid 0.5px;
    padding: 60px;
}
.buttonClose {
    position: absolute;
    border: solid 0.5px;
    border-radius: 5px;
    right: 3px;
    bottom: 0px;
    width: 70px;
    height: 35px;
}
.show {
    display: block;
}
.hide {
    display: none;
}

#workSites,
#otherSites {
  position: relative;
}
<!DOCTYPE html>
<html>
    <head>
        <title>Nick's site</title>
        <link rel="stylesheet" type="text/css" href="./styles3.css">
        <meta name="viewport" content="width= device-width, inital-scale=1">
    </head>
    <body>
        <div id="page-1" class="page">
            <div id="work">
                <div id="workButton" class="category-container center">
                    <a class="category-button pointer" onclick="openSites('work')">Work</a>
                </div>
                <div id="workSites" class="hide">
                    <ul class="sites center">
                        <li class="site"><a class="pointer" href="#">Printfactory</a></li>
                        <li class="site"><a class="pointer" href="#">Henry's Site</a></li>
                    </ul>
                    <button onclick="reset('work')" class="buttonClose pointer" style="z-index: 2;">
                        Reset
                    </button>
                </div>
            </div>
        </div>
        <div id="page-2" class="page">
            <div id="other">
                <div id="otherButton" class="category-container center">
                    <a class="category-button pointer" onclick="openSites('other')">Other</a>
                </div>
                <div id="otherSites" class="hide">
                    <ul class="sites center">
                        <li class="site"><a class="pointer" href="#">#</a></li>
                        <li class="site"><a class="pointer" href="#">#</a></li>
                        <li class="site"><a class="pointer" href="#">#</a></li>
                    </ul>
                    <button onclick="reset('other')" class="buttonClose pointer" style="z-index: 2;">
                        Reset2
                    </button>
                </div>
            </div>
        </div>
    </body>
</html>

Upvotes: 4

Related Questions