简体   繁体   English

CSS-阻止文本在跨度中溢出

[英]CSS - Stop text overflowing in span

I am trying to implement 2 lobbies in a menu bar for a mobile app and as long as the text for each lobby doesn't exceed the width of the container everything looks fine: 我正在尝试在移动应用程序的菜单栏中实现2个大厅,并且只要每个大厅的文本不超过容器的宽度,一切都很好:

在此处输入图片说明

However, if the text gets too long it all goes horribly wrong: 但是,如果文本太长,则将导致严重错误:

在此处输入图片说明

The height of the container needs to stay fixed at 40px so I gues the best solution is truncate the text. 容器的高度需要固定为40px,所以我认为最好的解决方法是截断文本。 However, there can be 2 or 3 lobbies here so I can't set a specific width on the span, if I use a div instead the layout gets borked. 但是,这里可能有2或3个大厅,因此如果我使用div而不是布局,则无法在跨度上设置特定宽度。 The icon and text needs to sit in the middle of the lobby, so using floats makes a mess of things. 图标和文本需要位于大厅的中间,因此使用浮动控件会造成混乱。 Anyone got any ideas? 任何人有任何想法吗?

HTML: HTML:

<div class="view lobby-menu-view">
<ul class="lobby-menu-list">
    <li data-lobby-type="lobby1" class="item">
        <div class="borderStyle"></div>
        <a href="#lobby/lobby1">
            <span class="lobby1 icon-home icon"></span>
            <span>Lobby 1</span>
        </a>
    </li>
    <li data-lobby-type="lobby2" class="item active">
        <div class="borderStyle"></div>
        <a href="#lobby/lobby2">
            <span class="lobby2 icon-home icon"></span>
            <span>Lobby 2</span>
        </a>
    </li>
</ul>

CSS: CSS:

div.lobby-menu-view {
z-index: -1;
position: relative;
margin: 0;
padding: 0;
width: 100%;
-webkit-box-shadow: 0px 2px 2px 0px rgba(97,59,53,0.7);

ul.lobby-menu-list {
      margin-bottom: 0;;
      text-align: center;
      list-style-type: none;
      margin: 0;
      width: 100%;
      display: table;
      li {
          text-align: center;
          height:40px;
          max-width: 220px;
          float: none;
          line-height: 40px;
          display: inline-block;
          .borderStyle {
               position: absolute;
               height: 50%;
               width: inherit;
               top: 25%;
           }
      }
      a {
          display: block;
          width: 100%;
          height: 100%;
          text-decoration: none;
          position: relative;
          color: #615452;
          font-size: 14px;
          span {
              position: relative;
              vertical-align: middle;
          }
          span.icon {
              display: inline-block;
              background-size: contain;
              width: 20px;
              height: 20px;
          }
      }
      li.active {
          padding: 0px;
          a {
              border-bottom-width: 6px;
              color:red;
              border-bottom-color:red;
              border-bottom-style: solid;
              box-sizing: border-box;
          }
      }

Use word-break: break-all : 使用word-break: break-all

.lobby-menu-list {
    word-break: break-all;
}

Demo 演示版

 .lobby-menu-view { max-width: 400px; } .lobby-menu-view li { width: 50%; float: left; } .lobby-menu-list { word-break: break-all; } 
 <div class="view lobby-menu-view"> <ul class="lobby-menu-list"> <li data-lobby-type="lobby1" class="item"> <div class="borderStyle"></div> <a href="#lobby/lobby1"> <span class="lobby1 icon-home icon"></span> <span>Lobby 1</span> </a> </li> <li data-lobby-type="lobby2" class="item active"> <div class="borderStyle"></div> <a href="#lobby/lobby2"> <span class="lobby2 icon-home icon"></span> <span>Lobbergergrgergergergergergegergergergergergergre</span> </a> </li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM