<ul>元素超出其各自的<div>界限

时间:2019-08-21 06:36:55

标签: html css

当我调整div的大小时,div的一个子元素超出范围。

我尝试过在可调整大小和调整大小div中添加填充。

所有实际代码都包含在这里: https://github.com/LudwigVonChesterfield/Totally-Accurate-Political-Simulator

不能与上述代码分开工作的代码段:

        <div id='div_player_chat' class='resizable' style='height: 200px; width: 400px; top: 60%; left: 60%'>
            <div class='resizers'>
                <div class='resizer top-left'></div>
                <div class='resizer top-right'></div>
                <div class='resizer bottom-left'></div>
                <div class='resizer bottom-right'></div>

                <div class="chat-wrapper">
                    <form id="player_chat-form">
                        <input id="player_chat" autocomplete="off" title="chat"/>
                        <button id="player_say">Say</button>
                    </form>
                </div>
                <ul id="player_events"></ul>
            </div>
        </div>

.resizable {
  background: black;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  overflow: hidden;
}

.resizable .resizers {
  width: 100%;
  height: 100%;
  border: 3px solid #4286f4;
  box-sizing: border-box;
}

.resizers {
  padding: 10px 10px 10px 10px;
}


.chat-wrapper {
  padding: 10px 10px 10px 10px;
  height: 35px;
  background: dodgerblue;
}

.chat-wrapper form {
  display: flex;
  height: 30px;
}

.chat-wrapper form input {
  font-size: 1.0rem;
  margin: 0 10px 0 0;
  padding: 0 5px;
  flex-grow: 1;
}

.chat-wrapper form button {
  width: 80px;
}

#player_events {
  width: 100%;
  height: 90%;
  flex-grow: 1;
  list-style: none;
  margin: 0;
  padding: 0;
  border-bottom: 1px solid lightgrey;
  overflow-y: scroll;
}

#player_events li {
  padding: 10px 10px 10px 10px;
  color: white;
}

我希望<ul>的大小会相应地调整为<div>,并且永远不会超出其范围。

但是结果是: https://cdn.discordapp.com/attachments/401030271417188355/613621706745053184/unknown.png

https://cdn.discordapp.com/attachments/489092762466254848/613618996977336320/2019-08-21_09-22-31.mp4

1 个答案:

答案 0 :(得分:1)

我建议您使用flex。我从height中删除了ul规范,取而代之的是赋予它flex-grow属性,它将占用剩余的空间。希望这对您有帮助

.resizable {
  background: black;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  overflow: hidden;
}

.resizable .resizers {
  width: 100%;
  height: 100%;
  border: 3px solid #4286f4;
  box-sizing: border-box;
}

.resizers {
  padding: 10px 10px 10px 10px;
}

.d-flex {
  display: flex;
  flex-direction: column;
}

.chat-wrapper {
  padding: 10px 10px 10px 10px;
  height: 35px;
  background: dodgerblue;
}

.chat-wrapper form {
  display: flex;
  height: 30px;
}

.chat-wrapper form input {
  font-size: 1.0rem;
  margin: 0 10px 0 0;
  padding: 0 5px;
  flex-grow: 1;
}

.chat-wrapper form button {
  width: 80px;
}

#player_events {
  width: 100%;
  /*height: 90%;*/
  flex-grow: 1;
  list-style: none;
  margin: 0;
  padding: 0;
  border-bottom: 1px solid lightgrey;
  overflow-y: scroll;
}

#player_events li {
  padding: 10px 10px 10px 10px;
  color: white;
}
<div id='div_player_chat' class='resizable' style='height: 200px; width: 400px; top: 60%; left: 60%'>
  <div class='resizers d-flex'>
    <div class='resizer top-left'></div>
    <div class='resizer top-right'></div>
    <div class='resizer bottom-left'></div>
    <div class='resizer bottom-right'></div>

    <div class="chat-wrapper">
      <form id="player_chat-form">
        <input id="player_chat" autocomplete="off" title="chat" />
        <button id="player_say">Say</button>
      </form>
    </div>
    <ul id="player_events"></ul>
  </div>
</div>