【问题标题】:Getting text-overflow in a flex-box to work [duplicate]让弹性框中的文本溢出工作[重复]
【发布时间】:2017-01-03 16:45:16
【问题描述】:

我有以下“顶部栏”,其中包含用户数据。它还将包含他的名字。代码如下:

html, body, ul, li, a, input { box-sizing: border-box; outline: 0px solid transparent; }
.title-bar {
  border-bottom: 1px solid white;
  height: 128px;
  width: 100%;
  background-color: #999;
  float: left;
}
.user-info {
  width: 128px;
  height: 100%;
  align-items: center;
  border-left: 1px solid white;
  background-color: inherit;
  display: flex;
  justify-content: space-between;
  flex-flow: column nowrap;
  float: right;
  text-align: center;
}
.user-info span {
  overflow: hidden;
  text-overflow: '.';
}
.user-info .circle {
  border: 2px solid #369;
  border-radius: 50%;
  display: inline-block;
  flex: 0 0 auto;
  height: 32px;
  margin: 8px 8px;
  overflow: hidden;
  transition: border 0.15s ease-out;
  width: 32px;
}
.user-info .circle:hover { border-width: 4px; }
.user-info .container {
  border-top: 1px solid white;
  display: flex;
  justify-content: center;
  margin-top: 6px;width: 100%;
}
<div class="title-bar">
  <div class="user-info">
    <a href="#" class="circle"></a>
    <span>Johnny B. Testname</span>
    <div class="container">
      <a href="#" class="circle"></a>
      <a href="#" id="log-off" class="circle first" title="Log Off">
      </a>
    </div>
  </div>
</div>

如您所见,“Johnny B. Testname”在底部被截断。这是因为我在跨度上设置了overflow: hidden;(否则它会推动其他我不想要的弹性项目)。

我一直在寻找“动态更改字体大小”的解决方案,但我认为这看起来不太好(而且我找不到任何可行的方法)。我找到了text-overflow;,如您所见,我已将其设置为 `text-overflow: '.';

唯一的问题是:它没有达到需要文本溢出的地步。我已经尝试设置flex-shrink: 1;(这并不重要,因为这是默认设置)、display: inline;(我绝望了)和其他 CSS 样式,但它们都没有解决问题。我不想设置heightwidth,因为我仍然希望span 是动态的。

也许我使用错误的文本溢出,但我认为我使用的是 MDN specs。我也为这个问题发了fiddle

谢谢。

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    DEMO

    .user-info span {
      overflow: hidden;
      text-overflow: ellipsis; /* for getting the end dots */
      display: inline-block; /* for making span a block (inline-block) so that it accepts max-width */
      max-width: 90%; /* specify the width */
      white-space: nowrap; /* for keeping text in single line */
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2016-11-10
      • 2022-07-19
      • 2021-12-17
      • 2017-02-11
      • 2020-04-20
      • 1970-01-01
      • 2019-06-19
      • 2017-11-23
      相关资源
      最近更新 更多