在可伸缩的表格中包装文本

时间:2015-06-21 17:04:04

标签: html css css3 flexbox word-wrap

我在一个居中的div的两边有两张桌子。我希望左侧表格中的内容在右侧对齐,右侧表格中的内容在左侧对齐。我设法通过在左侧表格中创建一个额外的列并在第一个单元格上设置width: 100%并在第二个单元格上设置white-space: nowrap来实现此目的。

问题是,由于nowrap属性,当表格太小时内容会溢出。我试着设置word-wrap: break-word是徒劳的。如何强制内容适合表格宽度?

到目前为止,我的代码是:JSFIDDLE



#container {
    height: 180px;
    display: flex;
    flex-flow: row;
    align-items: stretch;
    border: 1px dotted gray;
}

table {
    flex: 1;
    display: flex;
    margin: 10px;
    overflow-y: scroll;
    overflow-x: hidden;
    border: 1px dashed blue;
}

tbody {
    display: block;
    margin: auto 0;
}

td {
    padding: 5px;
    border: 1px solid red;
}

#left td:nth-child(1){
   width: 100%;
}

#left td:nth-child(2) {
    white-space: nowrap;
}

#list, #center {
    width: 100px;
    margin: 10px;
    border: 1px dotted gray;
}

<div id="container">
    <div id="list"></div>
    <table id="left">
        <tbody>
            <tr>
                <td></td>
                <td>Lorem ipsum dolor sit amet</td>
            </tr>
            <tr>
                <td></td>
                <td>Valar morghulis</td>
            </tr>
            <tr>
                <td></td>
                <td>One fish, two fish, red fish, blue fish</td>
            </tr>
        </tbody>
    </table>
    <div id="center"></div>
    <table id="right">
        <tbody>
            <tr><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit</td></tr>
            <tr><td>The quick brown fox jumps over the lazy dog</td></tr>
            <tr><td>Would you kindly help me out</td></tr>
            <tr><td>Supercalifragilisticexpialidocious</td></tr>
            <tr><td>Alpha</td></tr>
            <tr><td>Bravo</td></tr>
            <tr><td>Charlie</td></tr>
            <tr><td>Delta</td></tr>
            <tr><td>Echo</td></tr>
            <tr><td>Foxtrot</td></tr>
            <tr><td>Golf</td></tr>
        </tbody>
    </table>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

这个怎么样: DEMO

删除额外的列,并将margin-left:auto添加到左tbody

删除nowrap

#left tbody{
    margin-left:auto;
}

答案 1 :(得分:0)

有多种方法可以沿着主轴对齐Flex容器内的flex项目。

  • justify-content

      

    justify-content属性沿着flex items对齐main axis   Flex容器当前行的auto margins。这个完成了   在任何灵活的长度和任何flex-direction之后   解决。

    #left {
      justify-content: flex-end;
    }
    

    #container {
      height: 180px;
      display: flex;
      border: 1px dotted gray;
    }
    table {
      flex: 1;
      display: flex;
      margin: 10px;
      overflow-y: scroll;
      overflow-x: hidden;
      border: 1px dashed blue;
    }
    tbody {
      display: block;
      margin: auto 0;
    }
    td {
      padding: 5px;
      border: 1px solid red;
    }
    #left {
      justify-content: flex-end;
    }
    <div id="container">
      <table id="left">
        <tbody>
          <tr>
            <td>Lorem ipsum dolor sit amet</td>
          </tr>
          <tr>
            <td>Valar morghulis</td>
          </tr>
          <tr>
            <td>One fish, two fish, red fish, blue fish</td>
          </tr>
        </tbody>
      </table>
      <table id="right">
        <tbody>
          <tr><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit</td></tr>
          <tr><td>The quick brown fox jumps over the lazy dog</td></tr>
          <tr><td>Would you kindly help me out</td></tr>
          <tr><td>Supercalifragilisticexpialidocious</td></tr>
          <tr><td>Alpha</td></tr>
          <tr><td>Bravo</td></tr>
          <tr><td>Charlie</td></tr>
          <tr><td>Delta</td></tr>
          <tr><td>Echo</td></tr>
          <tr><td>Foxtrot</td></tr>
          <tr><td>Golf</td></tr>
        </tbody>
      </table>
    </div>

  • flex-direction

      

    flex items属性指定main axis的方式   通过设置弯曲的方向放置在柔性容器中   容器的main-start。这决定了flex的方向   物品布置在。

    如果您使用row-reverse(或column-reverse)代替row(或column),则会更换main-endAuto margins方向。

    #left {
      flex-direction: row-reverse;
    }
    

    #container {
      height: 180px;
      display: flex;
      border: 1px dotted gray;
    }
    table {
      flex: 1;
      display: flex;
      margin: 10px;
      overflow-y: scroll;
      overflow-x: hidden;
      border: 1px dashed blue;
    }
    tbody {
      display: block;
      margin: auto 0;
    }
    td {
      padding: 5px;
      border: 1px solid red;
    }
    #left {
      flex-direction: row-reverse;
    }
    <div id="container">
      <table id="left">
        <tbody>
          <tr>
            <td>Lorem ipsum dolor sit amet</td>
          </tr>
          <tr>
            <td>Valar morghulis</td>
          </tr>
          <tr>
            <td>One fish, two fish, red fish, blue fish</td>
          </tr>
        </tbody>
      </table>
      <table id="right">
        <tbody>
          <tr><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit</td></tr>
          <tr><td>The quick brown fox jumps over the lazy dog</td></tr>
          <tr><td>Would you kindly help me out</td></tr>
          <tr><td>Supercalifragilisticexpialidocious</td></tr>
          <tr><td>Alpha</td></tr>
          <tr><td>Bravo</td></tr>
          <tr><td>Charlie</td></tr>
          <tr><td>Delta</td></tr>
          <tr><td>Echo</td></tr>
          <tr><td>Foxtrot</td></tr>
          <tr><td>Golf</td></tr>
        </tbody>
      </table>
    </div>

  • justify-content

      

    在通过align-selfflex grow factor进行对齐之前,   任何正的自由空间都会分配到汽车边距   尺寸。

    #left > tbody {
      margin-left: auto;
    }
    

    #container {
      height: 180px;
      display: flex;
      border: 1px dotted gray;
    }
    table {
      flex: 1;
      display: flex;
      margin: 10px;
      overflow-y: scroll;
      overflow-x: hidden;
      border: 1px dashed blue;
    }
    tbody {
      display: block;
      margin: auto 0;
    }
    td {
      padding: 5px;
      border: 1px solid red;
    }
    #left > tbody {
      margin-left: auto;
    }
    <div id="container">
      <table id="left">
        <tbody>
          <tr>
            <td>Lorem ipsum dolor sit amet</td>
          </tr>
          <tr>
            <td>Valar morghulis</td>
          </tr>
          <tr>
            <td>One fish, two fish, red fish, blue fish</td>
          </tr>
        </tbody>
      </table>
      <table id="right">
        <tbody>
          <tr><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit</td></tr>
          <tr><td>The quick brown fox jumps over the lazy dog</td></tr>
          <tr><td>Would you kindly help me out</td></tr>
          <tr><td>Supercalifragilisticexpialidocious</td></tr>
          <tr><td>Alpha</td></tr>
          <tr><td>Bravo</td></tr>
          <tr><td>Charlie</td></tr>
          <tr><td>Delta</td></tr>
          <tr><td>Echo</td></tr>
          <tr><td>Foxtrot</td></tr>
          <tr><td>Golf</td></tr>
        </tbody>
      </table>
    </div>

  • 插入具有正flex item

    的伪元素
      

    flex grow factor 决定了flex items将会有多少   相对于flex中https://robots.thoughtbot.com/dont-repeat-your-ruby-constants-in-javascript的其余部分增长   当正空闲空间被分配时的容器。

    #left::before {
      content: '';
      flex-grow: 1;
    }
    

    #container {
      height: 180px;
      display: flex;
      border: 1px dotted gray;
    }
    table {
      flex: 1;
      display: flex;
      margin: 10px;
      overflow-y: scroll;
      overflow-x: hidden;
      border: 1px dashed blue;
    }
    tbody {
      display: block;
      margin: auto 0;
    }
    td {
      padding: 5px;
      border: 1px solid red;
    }
    #left::before {
      content: '';
      flex-grow: 1;
    }
    <div id="container">
      <table id="left">
        <tbody>
          <tr>
            <td>Lorem ipsum dolor sit amet</td>
          </tr>
          <tr>
            <td>Valar morghulis</td>
          </tr>
          <tr>
            <td>One fish, two fish, red fish, blue fish</td>
          </tr>
        </tbody>
      </table>
      <table id="right">
        <tbody>
          <tr><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit</td></tr>
          <tr><td>The quick brown fox jumps over the lazy dog</td></tr>
          <tr><td>Would you kindly help me out</td></tr>
          <tr><td>Supercalifragilisticexpialidocious</td></tr>
          <tr><td>Alpha</td></tr>
          <tr><td>Bravo</td></tr>
          <tr><td>Charlie</td></tr>
          <tr><td>Delta</td></tr>
          <tr><td>Echo</td></tr>
          <tr><td>Foxtrot</td></tr>
          <tr><td>Golf</td></tr>
        </tbody>
      </table>
    </div>