skip to Main Content

I’m new to CSS, JavaScript, and HTML so be patient with me. I created a button that duplicates my div but the button won’t go to the top right of my page. It just stays attached for the div for some reason – image attached for clarification.

Button and were I'm trying to get it to go

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.nameone {
  box-shadow: 0px 0px 6px -1px rgba(0, 0, 0, 0.4);
  position: relative;
  padding: 20px;
  padding-left: 25px;
  padding-right: 25px;
  border-radius: 5%;
}

.buttonformatting {
  background-color: red;
  height: 25px;
  width: 25px;
}

.text {
  border: none;
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewpoint" content="width=device-width,initial-scale=1">
  <title>TrialV3</title>
  <link href="Style.css" rel="stylesheet" type="text/css">
</head>

<body>

  <div class="nameone">
    <input class="text" type="text" onkeypress="this.style.width = ((this.value.length + 1)*8 ) + 'px';">
  </div>

  <script>
    var i = 0;
    var original = document.querySelector('.nameone');

    function duplicate() {
      var clone = original.cloneNode(true);
      clone.class = ".nameone" + ++i;
      original.parentNode.appendChild(clone);
    }
  </script>

  <button id="buttonone" onclick=duplicate() class="buttonformatting">
    </button>

</body>

<button id="buttonone" onclick=duplicate() class="buttonformatting">
    </button>

</html>

I have tried placing outside of my so it doesn’t get affected by that centering. I also tried putting inside of the body. However I arrange it, it seems like the button doesn’t want to leave the div’s side.

2

Answers


  1. Why would it go to the top right unless you specify it?
    position: absolute; top: 20px; right: 20px also consider position: fixed

    body {
      margin: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    
    .nameone {
      box-shadow: 0px 0px 6px -1px rgba(0, 0, 0, 0.4);
      position: relative;
      padding: 20px;
      padding-left: 25px;
      padding-right: 25px;
      border-radius: 5%;
    }
    
    .buttonformatting {
      background-color: red;
      height: 25px;
      width: 25px;
      position: absolute;
      top: 20px;
      right: 20px;
    }
    
    .text {
      border: none;
      text-align: center;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <meta name="viewpoint" content="width=device-width,initial-scale=1">
      <title>TrialV3</title>
      <link href="Style.css" rel="stylesheet" type="text/css">
    </head>
    
    <body>
    
      <div class="nameone">
        <input class="text" type="text" onkeypress="this.style.width = ((this.value.length + 1)*8 ) + 'px';">
      </div>
    
      <script>
        var i = 0;
        var original = document.querySelector('.nameone');
    
        function duplicate() {
          var clone = original.cloneNode(true);
          clone.class = ".nameone" + ++i;
          original.parentNode.appendChild(clone);
        }
      </script>
    
      <button id="buttonone" onclick=duplicate() class="buttonformatting">
        </button>
    
    </body>
    
    </html>
    Login or Signup to reply.
  2. <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewpoint" content="width=device-width,initial-scale=1">
        <title>TrialV3x</title>
        <link href="Style.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    
      <div class="nameone">
        <input class="text" type="text" onkeypress="this.style.width = ((this.value.length + 1)*8 ) + 'px';">
      </div>
      <button id="buttonone"></button>
    
    
    <script>
      const
        original = document.querySelector('.nameone')
      , btDuplic = document.querySelector('#buttonone')
        ;
      original.counter = 1
        ; 
      btDuplic.addEventListener('click', () =>
        {
        let clone       = original.cloneNode(true);
        clone.className = "nameone "  + original.counter; // className
        original.parentNode.append(clone);
        });
    </script>
    </body>
    </html>
    

    CSS Code:

    body {
      margin          : 0;
      display         : flex;
      justify-content : center;
      align-items     : center;
      height          : 100vh;
      }
    .nameone {
      box-shadow    : 0px 0px 6px -1px rgba(0,0,0,0.4);
      padding       : 20px;
      padding-left  : 25px;
      padding-right : 25px;
      border-radius : 5%;
      }
    #buttonone {
      position   : absolute;  /* prefer use absolute to avoid scroll pb */
      top        : 20px;
      right      : 20px;
      background : red;
      height     : 25px;
      width      : 25px;
      }
    .text {
      border     : none;
      text-align : center;
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search