html - 对所有可调整大小的子元素强制使用 flex 容器的最大宽度

标签 html css flexbox

给定一个具有 2 列的固定大小的 flex 容器:

  • 左动态基于内容的宽度列;
  • 一个普通的可调整大小的(双向)文本区域。

  • 我要控制textarea宽度,当用户调整大小(在两个方向)时。

    如何强制执行容器的 max-width大小为 textarea (没有 javascript 和/或禁用水平调整大小)就像 textarea是容器的直接 child 吗?

    我发现的唯一可行的仅 CSS 解决方案是 display: contents.right-content哪一栏unfortunately is not supported by all browsers .

    .container {
      max-width: 100px;
      background-color: red;
      display: flex;
    }
    
    .left-content {
      background-color: yellow;
    }
    
    .right-content {
      display: contents;
    }
    <div class="container">
      <div class="left-content">foo</div>
      <div class="right-content">
         <textarea>bar</textarea>
      </div>
    </div>

    最佳答案

    简单地做如下:

    .container {
      max-width: 100px;
      background-color: red;
      display: flex;
    }
    
    .left-content {
      background-color: yellow;
    }
    .right-content {
      min-width:0; /* this will prevent the element from growing more than the flex container
                      related: https://stackoverflow.com/q/36247140/8620333*/
    }
    
    textarea {
      max-width:100%; /* this will prevent the text area from growing more than its parent*/
      display:block;  /* this will remove the whitespace from the bottom*/
      box-sizing:border-box; /* don't forget this*/
    
      opacity:0.8; /* to illustrate */
    }
    <div class="container">
      <div class="left-content">foo</div>
      <div class="right-content">
         <textarea>bar</textarea>
      </div>
    </div>

    关于html - 对所有可调整大小的子元素强制使用 flex 容器的最大宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60628265/

    相关文章:

    javascript - 如何在我的代码编辑器应用程序中将颜色应用于保留字

    css - 有没有更好的方法可以在柔性显示中的元素之间保持相同的间隙空间?

    php - 如何使用 PHP 将数组插入到 SQL 表的单个字段中

    html - 如何处理同一页面中的不同样式表(WYSIWYG Editor 和网页one)?

    css - 在 CSS 媒体查询中实现逻辑 OR 的最有效方式

    javascript - Font Awesome 图标选择器在图标旁边显示字体信息文本

    css - 为 child 覆盖 css flex align-content

    css - 等宽进度条标签和垂直居中

    php - 在我的 PHP 代码中解析错误 : Syntax error, 文件意外结束

    html - 如何在文本旁边 float 图像,中间有间隙