代码之家  ›  专栏  ›  技术社区  ›  kadina

如何确保div中的元素不应该水平穿过div?

  •  0
  • kadina  · 技术社区  · 4 年前

    我们正在尝试创建聊天机器人应用程序。用户输入文本和“发送”按钮的输入位于div内。div宽度为100%。这在笔记本电脑和所有手机上都很有效,除了Iphone14的发送按钮被切断。下面是代码。

    .chat-container {
      background: #fff;
      height: 100%;
      overflow: hidden;
      padding: 0;
      border-right: 1px solid #d8dada;
      border-left: 1px solid #d8dada;
    }
    
    .chat-container>div:last-of-type {
      position: absolute;
      bottom: 0;
      width: 100%;
      display: flex;
      align-items: center;
      padding-right: 10px;
    }
    
    body>div>div>div:nth-child(2)>span {
      background: #dadada;
      padding: 10px;
      font-size: 21px;
      border-radius: 50%;
    }
    
    body>div>div>div.message-data-right.macro {
      margin: auto;
      margin-left: 1%;
    }
    
    .chat-header {
      background-color: #ffffff;
      height: 3.5rem;
      line-height: 3.5rem;
      color: #000000;
      border-bottom: 1px solid #000000;
      position: relative;
    }
    
    .chat-header-content {
      align-content: center;
      padding-top: 10px;
      padding-bottom: 10px;
    }
    
    .header-img {
      height: 24px;
      width: 106px;
      transform: scale(0.85);
      vertical-align: middle;
      content: url('./../images/vz_logo.svg');
    }
    
    .gray-button {
      background-color: #5b5b5b;
      border: none;
      color: white;
      padding: 10px 24px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 15px;
      font-family: Roboto-Medium;
      border-radius: 4px;
    }
    
    .chat-ul {
      width: 100%;
      list-style-type: none;
      padding: 18px 18px 3px 18px;
      position: absolute;
      bottom: 62px;
      display: flex;
      flex-direction: column;
      top: 3.5rem;
      overflow-y: auto;
      scrollbar-width: thin;
      scrollbar-color: #909296 #dee0e2;
      background-color: #f6f6f6;
      margin-bottom: 0%;
      border-bottom: 1px solid #c1c1c1;
    }
    
    .entered-text {
      border-width: 1px;
      border-radius: 5px;
      padding: 10px;
      background: #f6f6f6;
      width: 100%;
      border-color: #c1c1c1;
    }
    
    .text {
      width: 100%;
      display: flex;
      flex-direction: column;
    }
    
    .text>p:first-of-type {
      width: 100%;
      margin-top: 0;
      margin-bottom: auto;
      line-height: 13px;
      font-size: 13px;
    }
    
    .text>p {
      width: 100%;
      margin-top: 0;
      margin-bottom: auto;
      line-height: 13px;
      font-size: 13px;
    }
    
    .text>p:last-of-type {
      width: 100%;
      text-align: right;
      margin-bottom: -2px;
      margin-top: auto;
    }
    
    .text-right {
      float: right;
      font-family: Arial;
      position: relative;
    }
    
    .send-message {
      width: 100%;
      border-radius: 0px;
      padding: 10px;
      display: flex;
    }
    
    input:focus {
      outline: none;
    }
    
    button,
    button:focus,
    button:active {
      outline: none;
    }
    <div class="chat-container">
      <header class="chat-header">
        <img class="header-img" />
        <button type="button" class="icon-close" onClick="closeChatWindow()"></button>
      </header>
    
      <ul class="chat-ul"></ul>
      <div>
        <div class="send-message">
          <div class="text text-right">
            <input class="entered-text" />
          </div>
        </div>
        <button id="send" class="gray-button" type="button" onClick="onMessageSend()"> Send </button>
      </div>
    </div>

    我们的测试团队提出了一个错误,说发送按钮在IPhone14上被切断。我不知道如何重现这个问题,因为我没有Iphone14。我有Android手机,它的代码运行良好。在个人电脑上,我测试了不同的浏览器都工作良好。我使用开发人员工具下的切换设备工具栏来检查不同设备的外观,并使用它来响应宽度和高度的变化。我无法再现这个问题。以下是它在Iphone14上被复制的图片。

    enter image description here

    在图像的末尾,“发送”灰色按钮被切断。谁能告诉我怎么解决这个问题吗。

    3 回复  |  直到 4 年前
        1
  •  1
  •   isherwood    4 年前

    你不需要iPhone就能看到这个。对我来说这是显而易见的。如果愿意,可以使用dev工具中的仿真器。

    1. 拆下浮子( .text-right )从布局中
    2. 去掉输入端的100%宽度 .send-message
    3. 将flex类向上移动一级,以包含输入和按钮

    我还在按钮上加了一些左边距。

    在我的职业生涯中,我已经修复了很多这样的情况,而且解决方案通常包括去除不必要的样式以简化。你可能会通过你的整个布局,这样做。

    .gray-button {
      background-color: #5b5b5b;
      border: none;
      color: white;
      padding: 10px 24px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 15px;
      font-family: Roboto-Medium;
      border-radius: 4px;
      margin-left: 10px;
    }
    
    .entered-text {
      border-width: 1px;
      border-radius: 5px;
      padding: 10px;
      background: #f6f6f6;
      border-color: #c1c1c1;
    }
    
    .text {
      width: 100%;
      display: flex;
      flex-direction: column;
    }
    
    .send-message {
      border-radius: 0px;
      padding: 10px;
      display: flex;
    }
    
    input:focus {
      outline: none;
    }
    
    button,
    button:focus,
    button:active {
      outline: none;
    }
    <div class="chat-container">
      <div class="send-message">
          <div class="text">
            <input class="entered-text" />
          </div>
        <button id="send" class="gray-button" type="button" onClick="onMessageSend()"> Send </button>
      </div>
    </div>
        2
  •  1
  •   Rounin    4 年前

    解决这个问题最直接的方法是使用CSS calc() 给予的功能 input.entered-text 灵活的 width 这将始终适应 宽度 <button> :

    例如 如果你给 button#send width: 100px ), 然后 输入。输入-文本 可以容纳固定宽度的宽度( width: calc(100% - 100px) )

    例子:

    #send {
      display: inline-block;
      width: 100px;
      margin-left: 12px;
      box-sizing: border-box;
    }
    
    .entered-text {
      display: inline-block;
      width: calc(100% - 12px - 100px);
      box-sizing: border-box;
    }
    
        3
  •  0
  •   Daniel Caminero    4 年前

    我不是专家,但你应该 <br> 在办公室里

    编辑: 换行符通常是不必要的,所以我要说的是,在找到解决方案之前,只使用这个临时的