首页 > 解决方案 > 如何在不破坏 CSS 对齐的情况下修复文本换行?

问题描述

我有下面的 html 和 css 来根据屏幕截图显示一些数据。如果我添加white-space: nowrap;Then 文本移出框(第一个屏幕截图)。如果我不使用,那么它会包裹但会干扰框对齐(屏幕截图中的第二行)。

我可以设置什么属性来解决这个问题而不增加盒子宽度?我也尝试过显示属性,但到目前为止还没有运气。

<html>      
  <head>
    <meta charset="UTF-8"/>
 <style>
.sideBarList li {
  width: 12%;
  height: 50px;
  text-align: center;
  line-height: -10px;
  border-radius: 10px;
  background: skyblue;
  margin: 0 10px;
          white-space: nowrap;
  display: inline-block;
  color: black;
  position: relative; 
  text-align: center;
  font-family: Arial;
  font-size: 11px;
}

.sideBarList li::before{
  content: '';
  position: absolute;
  top: 25px;
  left: -8em;
  width: 8em;
  height: .2em;
  background: skyblue;
  z-index: -1;
}

   </style>

   </head>
  <body>

  <ul class="sideBarList">
        <li class="li">Hi There</li>  
        <li class="li">Hi There</li> 
        <li class="li"> ABC DEF GHI TYTYT YTYYT IIIOO</li> 
 </ul>
  </body>
</html>

标签: htmlcss

解决方案


display:inline-flex也有效。请检查。

.sideBarList li {
    width: 12%;
    height: 50px;
    text-align: center;
    line-height: -10px;
    border-radius: 10px;
    background: skyblue;
    margin: 0 10px;
    display: -webkit-inline-box;
    display: -ms-inline-flexbox;
    display: inline-flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    vertical-align:top;
    color: black;
    position: relative;
    text-align: center;
    font-family: Arial;
    font-size: 11px;
}

.sideBarList li::before {
    content: '';
    position: absolute;
    top: 25px;
    left: -8em;
    width: 8em;
    height: .2em;
    background: skyblue;
    z-index: -1;
}
<ul class="sideBarList">
  <li class="li">Hi There</li>
  <li class="li">Hi There</li>
  <li class="li"> ABC DEF GHI TYTYT YTYYT IIIOO</li>
</ul>


推荐阅读