如何在不破坏CSS对齐的情况下修复文本换行?

时间:2018-06-13 09:23:02

标签: html css css3

我在html和css下面按屏幕截图显示一些数据。如果我添加white-space: nowrap;然后文本移出框(第一个屏幕截图)。如果我不使用它,它会包装但会扰乱盒子对齐(屏幕截图中的第2行)。

我可以设置任何属性来修复它而不增加框宽度? 我也尝试过显示属性,但到目前为止没有运气。

<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>

4 个答案:

答案 0 :(得分:1)

您正在使用inline-block,您需要使用vertical-align:top来维持对齐,因为默认对齐方式为baseline

.sideBarList li {
  width: 12%;
  height: 50px;
  text-align: center;
  line-height: -10px;
  border-radius: 10px;
  background: skyblue;
  margin: 0 10px;
  display: inline-block;
  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>

答案 1 :(得分:1)

只将.sideBarList li更新为此

.sideBarList li {
   width: 12%;
  height: 50px;
  text-align: center;
  line-height: -10px;
  border-radius: 10px;
  background: skyblue;
  margin: 0 10px;
  display: inline-block;
  vertical-align: top;
  color: black;
  position: relative;
  text-align: center;
  font-family: Arial;
  font-size: 11px;
}

答案 2 :(得分:1)

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

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

使用此风格

ul {
   display: flex;
}

 
.sideBarList li {
  width: 12%;
  height: 50px;
  text-align: center;
  line-height: -10px;
  border-radius: 10px;
  background: skyblue;
  margin: 0 10px;
  white-space: normal;
  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;
}
ul {
   display: flex;
}
<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>