Title rewritten as: flexbox menu and table with title - is it possible to truncate the title based on table width?
P粉642920522
P粉642920522 2023-09-06 17:30:06
0
1
446

I want to achieve something like this by using flexbox CSS. The menu is fixed width, but the table can grow automatically, and the header should not exceed the table width, even if it is long. In other words, the table should be able to stretch the column containers, but the headers shouldn't.

+----+--------+
|菜单| 标题   |
|    +--------+
|    | 表格   |
+----+--------+

It can be easily implemented with just one flex column.

<html>
<style>
    td {
        font-size: 60px;
    }
    .container {
        font-size: 60px;
        background-color: yellow;
        display: flex;
        flex-direction: column;
    }
    .item1 {
        background-color: red;
        white-space: nowrap;
        overflow: hidden;
    }
    .item2 {
        background-color: blue;
    }
</style>
<body>
<div class="container">
  <div class="item1">Item 1 bla bla blaaas asd das dsa das das aaaaaaaaa</div>
  <div class="item2">
    <table>
      <tr>
        <td>data</td>
        <td>data</td>
        <td>data</td>
        <td>data</td>
        <td>data</td>
        <td>data</td>
        <td>data</td>
        <td>data</td>
        <td>data</td>
        <td>data</td>
        <td>data</td>
      </tr>
    </table>
  </div>
</div>
</body>
</html>

But when adding the menu, everything is not normal, the title is not truncated.

<html>
<style>
    .main {
        display: flex;
    }
    .menu {
        background-color: #222222;
        width: 200px;
    }
    td {
        font-size: 60px;
    }
    .container {
        font-size: 60px;
        background-color: yellow;
        display: flex;
        flex-direction: column;
        width: 100%;
    }
    .item1 {
        background-color: red;
        white-space: nowrap;
        overflow: hidden;
    }
    .item2 {
        background-color: blue;
    }
</style>
<body>
<div class="main">
  <div class="menu">
    菜单
  </div>
  <div class="container">
    <div class="item1">Item 1 bla bla blaaas asd das dsa das das aaaaaaaaa</div>
    <div class="item2">
      <table>
        <tr>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
          <td>data</td>
        </tr>
      </table>
    </div>
  </div>
</div>
</body>
</html>
P粉642920522
P粉642920522

reply all(1)
P粉512526720

So you want the element containing the title to change in size with its sibling elements, but without affecting its own child elements.

The only way I can think of is to wrap the title text in an appropriate element - in this case <span> - and then use position: absolute to remove it from Take it out of the flow and use position: relative on the parent element of the title. Then you need to set some height to the title.

Example

I replaced the width with flex: 0 0 200px. This will keep the menu from changing size, always staying at 200px.

.main {
  display: flex;
}

.menu {
  background-color: #222222;
  flex: 0 0 200px; /*改变了*/
}

td {
  font-size: 60px;
}

.container {
  font-size: 60px;
  background-color: yellow;
  display: flex;
  flex-direction: column;
  width: maxwidth; /*将宽度改为maxwidth*/
}

.item1 {
  background-color: red;
  white-space: nowrap;
  overflow: hidden;
  position: relative; /*添加了*/
  height: 1.2em; /*添加了*/
}

.item1 span {
  position: absolute; /*添加了*/
}

.item2 {
  background-color: blue;
}
<div class="main">
  <div class="menu">
    菜单
  </div>

  <div class="container">
    <div class="item1"><span>The quick brown fox jumped over the lazy dog</span></div>
    <div class="item2">
      <table>
        <tr>
          <td>数据</td>
          <td>数据</td>
          <td>数据</td>
        </tr>
      </table>
    </div>
  </div>
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template