为什么我的页面需要附加滚动?

时间:2018-06-20 19:33:06

标签: html css

<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
  html,
  body {
    font-family: helvetica;
    height: 100%;
    margin: 0;
  }
  
  .chatContainer {
    width: 100%;
    height: 100vh;
  }
  
  .chatWindow {
    overflow-y: auto;
    overflow-x: hidden;
    padding: 14px;
    height: 100%;
    border: solid 1px #EEE;
  }
  
  .messages {
    //margin-top: 20px;
    display: flex;
    flex-direction: column;
  }
  
  .message {
    border-radius: 20px;
    padding: 8px 15px;
    margin-top: 10px;
    margin-bottom: 10px;
    display: inline-block;
  }
  
  .yours {
    align-items: flex-start;
  }
  
  .yours .message {
    margin-right: 25%;
    background-color: #EEE;
    position: relative;
  }
  
  .yours .message.last:before {
    content: "";
    position: absolute;
    z-index: 0;
    bottom: 0;
    left: -7px;
    height: 20px;
    width: 20px;
    background: #EEE;
    border-bottom-right-radius: 15px;
  }
  
  .yours .message.last:after {
    content: "";
    position: absolute;
    z-index: 1;
    bottom: 0;
    left: -10px;
    width: 10px;
    height: 20px;
    background: white;
    border-bottom-right-radius: 10px;
  }
  
  .mine {
    align-items: flex-end;
  }
  
  .mine .message {
    color: white;
    margin-left: 25%;
    background: rgb(0, 120, 254);
    position: relative;
  }
  
  .mine .message.last:before {
    content: "";
    position: absolute;
    z-index: 0;
    bottom: 0;
    right: -8px;
    height: 20px;
    width: 20px;
    background: rgb(0, 120, 254);
    border-bottom-left-radius: 15px;
  }
  
  .mine .message.last:after {
    content: "";
    position: absolute;
    z-index: 1;
    bottom: 0;
    right: -10px;
    width: 10px;
    height: 20px;
    background: white;
    border-bottom-left-radius: 10px;
  }
  
  .inputField {
    width: 100%;
    color: black;
    font-family: helvetica;
    border: solid 1px #EEE;
    padding: 7px;
    font-size: 16px;
    margin-top: -1px;
    z-index: 100;
  }
  
  .attatchImage {
    background-image: url("https://kieferfoto.hu/wp-content/uploads/2016/09/Flat-Camera-Icon.png");
    width: 5%;
    height: 5%;
  }
  
  .attatchImage:hover {
    opacity: 0.8;
    transition: all 0.20s linear;
  }
</style>
<div class="chatContainer">
  <div class="chatWindow" id="chatWindow">
    <div class="chat">
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
    </div>
  </div>
  <input type="text" class="inputField" placeholder="Enter message">
</div>

这似乎给了我一个不适合屏幕的页面,我希望它既可以填充当前的屏幕尺寸,也可以填充宽度和高度,但是似乎可以填充宽度而不是高度;它超出了屏幕尺寸。 这是一个演示:www.fightmymonster.rf.gd/Chat

有帮助吗?我找不到导致这种情况的原因,我尝试删除某些元素,但这只会破坏其余元素,甚至无法解决我的问题。

1 个答案:

答案 0 :(得分:0)

像这样调整您的代码:

* {
  box-sizing: border-box; /*added this*/
}

html,
body {
  font-family: helvetica;
  height: 100%;
  margin: 0;
}

.chatContainer {
  width: 100%;
  height: 100vh;
  display:flex; /*added this*/
  flex-direction:column; /*added this*/
}

.chatWindow {
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px;
  /*height: 100%; remove this*/
  flex:1;  /*add this*/
  border: solid 1px #EEE;
}

.messages {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
}

.message {
  border-radius: 20px;
  padding: 8px 15px;
  margin-top: 10px;
  margin-bottom: 10px;
  display: inline-block;
}

.yours {
  align-items: flex-start;
}

.yours .message {
  margin-right: 25%;
  background-color: #EEE;
  position: relative;
}

.yours .message.last:before {
  content: "";
  position: absolute;
  z-index: 0;
  bottom: 0;
  left: -7px;
  height: 20px;
  width: 20px;
  background: #EEE;
  border-bottom-right-radius: 15px;
}

.yours .message.last:after {
  content: "";
  position: absolute;
  z-index: 1;
  bottom: 0;
  left: -10px;
  width: 10px;
  height: 20px;
  background: white;
  border-bottom-right-radius: 10px;
}

.mine {
  align-items: flex-end;
}

.mine .message {
  color: white;
  margin-left: 25%;
  background: rgb(0, 120, 254);
  position: relative;
}

.mine .message.last:before {
  content: "";
  position: absolute;
  z-index: 0;
  bottom: 0;
  right: -8px;
  height: 20px;
  width: 20px;
  background: rgb(0, 120, 254);
  border-bottom-left-radius: 15px;
}

.mine .message.last:after {
  content: "";
  position: absolute;
  z-index: 1;
  bottom: 0;
  right: -10px;
  width: 10px;
  height: 20px;
  background: white;
  border-bottom-left-radius: 10px;
}

.inputField {
  width: 100%;
  color: black;
  font-family: helvetica;
  border: solid 1px #EEE;
  padding: 7px;
  font-size: 16px;
  margin-top: -1px;
  z-index: 100;
}

.attatchImage {
  background-image: url("https://kieferfoto.hu/wp-content/uploads/2016/09/Flat-Camera-Icon.png");
  width: 5%;
  height: 5%;
}

.attatchImage:hover {
  opacity: 0.8;
  transition: all 0.20s linear;
}
<div class="chatContainer">
  <div class="chatWindow" id="chatWindow">
    <div class="chat">
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
      <div class="mine messages">
        <div class="message">
          Great thanks!lol
        </div>
        <div class="message last">
          How about you??trgrtgrgrgrtgrtgertgrtgrt
        </div>
      </div>
      <div class="yours messages">
        <div class="message last">
          Hello, how's it going?
        </div>
      </div>
    </div>
  </div>
  <input type="text" class="inputField" placeholder="Enter message">
</div>