溢位换行未应用于文本

时间:2019-03-05 08:41:56

标签: html css angular

问题

我目前有一个使用Angular构建的侧边菜单,它在我的屏幕上(4k分辨率)运行良好。问题是在较小的屏幕上,菜单中的文本不会自动换行,因此会导致容器div溢出。我已经尝试将overflow-wrap: break-word;应用于文本,但这不起作用。

任何帮助将不胜感激。

您可以在下面看到问题:

Text overflowing div


代码

组件的HTML:

<div class="sidebar animated fadeIn">
  <div class="header">
    <div style="display:block;margin-left:auto;margin-right:auto;width:100%;">
      <img src="../../../assets/images/logo.svg" style="width:100%" />
    </div>
  </div>
  <div class="spacer"></div>
  <div class="menu-item" *ngFor="let item of items" (click)="goTo(item.path)">
    <i class="material-icons">{{item.icon}}</i>
    <h3 class="text">{{item.name}}</h3>
  </div>
</div>

组件的样式:

.sidebar {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 2;
  width: 10%;
  background: rgba(191, 158, 69, 1);
  background: -moz-linear-gradient(top, rgba(191, 158, 69, 1) 0%, rgba(250, 239, 210, 1) 100%);
  background: -webkit-gradient(
    left top,
    left bottom,
    color-stop(0%, rgba(191, 158, 69, 1)),
    color-stop(100%, rgba(250, 239, 210, 1))
  );
  background: -webkit-linear-gradient(bottom, rgba(191, 158, 69, 1) 0%, rgba(250, 239, 210, 1) 100%);
  background: -o-linear-gradient(bottom, rgba(191, 158, 69, 1) 0%, rgba(250, 239, 210, 1) 100%);
  background: -ms-linear-gradient(bottom, rgba(191, 158, 69, 1) 0%, rgba(250, 239, 210, 1) 100%);
  background: linear-gradient(to top, rgba(191, 158, 69, 1) 0%, rgba(250, 239, 210, 1) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#bf9e45', endColorstr='#faefd2', GradientType=0 );
  box-shadow: 5px 0 15px rgba(0, 0, 0, 0.5);
}

.header {
  width: 100%;
}

.spacer {
  margin-top: 3em;
}

.menu-item {
  border-top: 1px solid darkgray;
  padding-left: 2em;
  height: 3em;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.menu-item .text {
  margin-left: auto;
  margin-right: auto;
  overflow-wrap: break-word;
}

1 个答案:

答案 0 :(得分:2)

添加此属性word-break: break-all;

.menu-item .text {
  margin-left: auto;
  margin-right: auto;
  /*overflow-wrap: break-word;*/
  word-break: break-all;
}