将DIV宽度限制为父DIV宽度(没有显式宽度声明)

时间:2018-01-12 21:30:23

标签: html css css3 flexbox

Rquirements:

基于flexbox 纯HTML / CSS没有Jquery / JS 没有明确声明DIV宽度,就像内容

所需要的那样

在下面的代码中,我希望页脚用省略号截断(以便不解释容器div),或者更好地换行到下一行。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .truncate {
            white-space: nowrap;
            overflow-x: hidden;
            text-overflow: ellipsis;
        }
    </style>
</head>
<div class="row" style="display:flex; justify-content: flex-start; padding: px; border: 2px solid red;">


    <div class='olb-container' style=' width: fit-content; display: flex; flex-direction: column; justify-content: flex-start; margin: 5px; border: 2px solid green;'>
        <div class='olb-header' style='display: flex; justify-content: center; overflow-x: hidden; border: 1px solid black; padding: 0 3px 2.5px 3px; font-size: 125%; font-weight: 900;'>HEADER</div>
        <div class='olb-center' style='display: flex; flex-direction: row; border: 1px solid black;'>
            <div class='olb-yes-col' style='display: flex; flex-direction: column; align-items: stretch; border: 1px solid black; '>
                <div class='olb-yes-title' style='border-bottom: 1px solid black; padding: 2.5px 3px 3px 3px;'>YES</div>
                <div class='olb-yes-checks' style='padding: 2.5px 3px 3px 3px;'>
                    <div style='display: flex; flex-direction: column; align-items: center'>
                        <input class='olb-yes' type='checkbox' data-text='aaa' value=true>
                        <input class='olb-yes' type='checkbox' data-text='bbb' value=true>
                    </div>
                </div>
            </div>
            <div class='olb-items-col' style='display: flex; flex-direction: column; align-items: stretch; border: 1px solid black;'>
                <div class='olb-items-title' style='border-bottom: 1px solid black; ; padding: 2.5px 3px 3px 3px;'>LABEL</div>
                <div class='olb-items-text' style='display: flex; flex-direction: column; align-items: center; padding: 2px; padding: 2.5px 3px 3px 3px;'>
                    <div>aaaa</div>
                    <div>bbb</div>
                </div>
            </div>
            <div class='olb-no-col' style='display: flex; flex-direction: column; align-items: stretch; border: 1px solid black;'>
                <div class='olb-no-title' style='border-bottom: 1px solid black; padding: 2.5px 3px 3px 3px;'>NO</div>
                <div class='olb-no-checks' style='padding: 2.5px 3px 3px 3px;'>
                    <div style='display: flex; flex-direction: column; align-items: center'>
                        <input class='olb-no' type='checkbox' data-text='aaa' value=true>
                        <input class='olb-no' type='checkbox' data-text='bbb' value=true>
                    </div>
                </div>
            </div>
        </div>
        <div class='olb-footer' style='display: flex; align-content: flex-start; overflow: hidden; border: 1px solid black; padding: 2.5px 3px 0 3px; font-size: 60%; text-decoration: italics;'>
            <div style="">FOOTERafd sdf fsddsfkjsdgfdskjgkjhgfa</div>
        </div>

    </div>
    </body>

</html>

1 个答案:

答案 0 :(得分:2)

解决方案是使用绝对位置使您的内容脱离流程,并且您不需要指定宽度,因为通过指定top / left / bottom / right,您将获取已定义的所有空间:< / p>

&#13;
&#13;
.truncate {
  white-space: nowrap;
  overflow-x: hidden;
  text-overflow: ellipsis;
}

.olb-footer {
  position: relative;
}

.olb-footer:before {
  content: "A"; /* Add at least one hidden letter to create the space for a line */
  visibility: hidden;
}

.content {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
&#13;
<div class="row" style="display:flex; justify-content: flex-start; padding: px; border: 2px solid red;">


  <div class='olb-container' style=' width: fit-content; display: flex; flex-direction: column; justify-content: flex-start; margin: 5px; border: 2px solid green;'>
    <div class='olb-header' style='display: flex; justify-content: center; overflow-x: hidden; border: 1px solid black; padding: 0 3px 2.5px 3px; font-size: 125%; font-weight: 900;'>HEADER</div>
    <div class='olb-center' style='display: flex; flex-direction: row; border: 1px solid black;'>
      <div class='olb-yes-col' style='display: flex; flex-direction: column; align-items: stretch; border: 1px solid black; '>
        <div class='olb-yes-title' style='border-bottom: 1px solid black; padding: 2.5px 3px 3px 3px;'>YES</div>
        <div class='olb-yes-checks' style='padding: 2.5px 3px 3px 3px;'>
          <div style='display: flex; flex-direction: column; align-items: center'>
            <input class='olb-yes' type='checkbox' data-text='aaa' value=true>
            <input class='olb-yes' type='checkbox' data-text='bbb' value=true>
          </div>
        </div>
      </div>
      <div class='olb-items-col' style='display: flex; flex-direction: column; align-items: stretch; border: 1px solid black;'>
        <div class='olb-items-title' style='border-bottom: 1px solid black; ; padding: 2.5px 3px 3px 3px;'>LABEL</div>
        <div class='olb-items-text' style='display: flex; flex-direction: column; align-items: center; padding: 2px; padding: 2.5px 3px 3px 3px;'>
          <div>aaaa</div>
          <div>bbb</div>
        </div>
      </div>
      <div class='olb-no-col' style='display: flex; flex-direction: column; align-items: stretch; border: 1px solid black;'>
        <div class='olb-no-title' style='border-bottom: 1px solid black; padding: 2.5px 3px 3px 3px;'>NO</div>
        <div class='olb-no-checks' style='padding: 2.5px 3px 3px 3px;'>
          <div style='display: flex; flex-direction: column; align-items: center'>
            <input class='olb-no' type='checkbox' data-text='aaa' value=true>
            <input class='olb-no' type='checkbox' data-text='bbb' value=true>
          </div>
        </div>
      </div>
    </div>
    <div class='olb-footer' style='display: flex; align-content: flex-start; overflow: hidden; border: 1px solid black; padding: 2.5px 3px 0 3px; font-size: 60%; text-decoration: italics;'>
      <div class="content">FOOTERafd sdf fsddsfkjsdgfdskjgkj hgfa</div>
    </div>

  </div>
&#13;
&#13;
&#13;