【问题标题】:CSS: Truncate button-text and keep icon in a flex-boxCSS:截断按钮文本并将图标保留在弹性框中
【发布时间】:2020-05-18 09:38:41
【问题描述】:

我正在尝试让按钮显示一些被截断的文本和一个图标。即使文本开始被截断,我也希望该图标存在。

所以问题是当文本被截断时 - 图标也被删除。 我可以通过将图标作为单独的按钮来解决这个问题 - 但这会使键盘导航变得奇怪。

请参阅下面的示例:

.bar {
  display: flex;
}

.item2 {
  border: none;
  background-color: white;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
<nav class="bar">
  <span class="item1">
    TEST
  </span>
  <button type="button" class="item2">
    <span class="item2_text">This is a veeeery long text that we want to overflow so that
    everything is on one line and does not break our content line in two or overflow</span>
    <span class="item2_icon">
      <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="18px" height="10px" viewBox="0 0 451.847 451.847" style="enable-background:new 0 0 451.847 451.847;" xml:space="preserve">
        <g>
          <path d="M225.923,354.706c-8.098,0-16.195-3.092-22.369-9.263L9.27,151.157c-12.359-12.359-12.359-32.397,0-44.751
	c12.354-12.354,32.388-12.354,44.748,0l171.905,171.915l171.906-171.909c12.359-12.354,32.391-12.354,44.744,0
	c12.365,12.354,12.365,32.392,0,44.751L248.292,345.449C242.115,351.621,234.018,354.706,225.923,354.706z" />
        </g>
      </svg>
    </span>
  </button>
  <span class="menu">
    Stuff
  </span>
</nav>

我试图将 text-overflow: ellipsis 移动到 item2_text 但它不起作用。 谁能帮帮我?

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    如果您想采用动态方法,请不要给文本固定宽度,请使用 calc 属性。 首先,将省略号代码移动到跨度而不是将其放在按钮本身上并添加inline-block 属性以使省略号起作用。 然后将项目设置为 100% 并减去图标的宽度(以及一些额外的像素)。

    .bar {
      display: flex;
    }
    
    .item2 {
      border: none;
      background-color: white;
      overflow: hidden;
    }
    
    .item2_text {
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      display: inline-block;
      width: calc(100% - 25px);
    }
    <nav class="bar">
      <span class="item1">
        TEST
      </span>
      <button type="button" class="item2">
        <span class="item2_text">This is a veeeery long text that we want to overflow so that
        everything is on one line and does not break our content line in two or overflow</span>
        <span class="item2_icon">
          <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="18px" height="10px" viewBox="0 0 451.847 451.847" style="enable-background:new 0 0 451.847 451.847;" xml:space="preserve">
            <g>
              <path d="M225.923,354.706c-8.098,0-16.195-3.092-22.369-9.263L9.27,151.157c-12.359-12.359-12.359-32.397,0-44.751
    	c12.354-12.354,32.388-12.354,44.748,0l171.905,171.915l171.906-171.909c12.359-12.354,32.391-12.354,44.744,0
    	c12.365,12.354,12.365,32.392,0,44.751L248.292,345.449C242.115,351.621,234.018,354.706,225.923,354.706z" />
            </g>
          </svg>
        </span>
      </button>
      <span class="menu">
        Stuff
      </span>
    </nav>

    【讨论】:

    • 没错,这是一种防止过早选择答案的预防形式
    猜你喜欢
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    相关资源
    最近更新 更多