在`<li>`元素

时间:2016-11-12 05:20:16

标签: html css css3

我创建了一个表单,我有一个侧面导航栏。在这种情况下,所有<li>元素的放置方式使得两个<li>落在一行中。现在,问题是,只要<li>元素的文本增加,它就会影响其他<li>元素。这就是我尝试过的方式:

body {
  overflow-x: hidden;
}
    
    /* Toggle Styles */
    
#wrapper {
  padding-left: 0;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}
    
#wrapper.toggled {
  padding-left: 250px;
}
    
#sidebar-wrapper {
  z-index: 1000;
  position: fixed;
  left: 250px;
  width: 0;
  height: 100%;
  margin-left: -250px;
  overflow-y: auto;
  background: #000;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}
    
#wrapper.toggled #sidebar-wrapper {
  width: 250px;
}
    
#page-content-wrapper {
  width: 100%;
  position: absolute;
  padding: 15px;
}
    
#wrapper.toggled #page-content-wrapper {
  position: absolute;
  margin-right: -250px;
}
    
  /* Sidebar Styles */
    
.sidebar-nav {
  position: absolute;
  top: 0;
  width: 120px;
  margin: 0;
  padding: 0;
  list-style: none;
  text-indent: -2em;
}
    
.sidebar-nav li {
  text-indent: 20px;
  line-height: 40px;
}
    
.sidebar-nav li a {
  display: block;
  text-decoration: none;
  color: #999999;
}
    
.sidebar-nav li a:hover {
  text-decoration: none;
  color: #fff;
  background: rgba(255,255,255,0.2);
}
    
.sidebar-nav li a:active,
.sidebar-nav li a:focus {
  text-decoration: none;
}
    
.sidebar-nav > .sidebar-brand {
  height: 65px;
  font-size: 18px;
  line-height: 60px;
}
    
.sidebar-nav > .sidebar-brand a {
  color: #999999;
}
    
.sidebar-nav > .sidebar-brand a:hover {
  color: #fff;
  background: none;
}
    
.sidebar-nav_wrapper    {
  float:left;
  margin-left:5px;
} 
<div id="sidebar-wrapper">
  <div class="sidebar-nav_wrapper">
    <h4 class="sidebar-brand">Select Field Type</h4>
    <ul class="sidebar-nav" style="margin-top: 40px;">
    	
      <li><a href="#" data-toggle="modal" data-target="#SingleLine_Modal">Single Line</a></li>
      <li><a href="#" data-toggle="modal" data-target="#PickList_Modal">Pick List</a></li>
      <li><a href="#" data-toggle="modal" data-target="#MultiSelect_Modal">Multi Select</a></li>
      <li><a href="#" data-toggle="modal" data-target="#MultiLine_Modal">Multi Line</a></li>
      <li><a href="#" data-toggle="modal" data-target="#Email_Modal">Email</a></li>
      <li><a href="#" data-toggle="modal" data-target="#Phone_Modal">Phone Number</a></li>
      <li><a href="#" data-toggle="modal" data-target="#Mobile_Modal">Mobile Number</a></li>
    </ul>
  </div>
  <div class="sidebar-nav_wrapper">
    <ul class="sidebar-nav" style="margin-top: 40px; margin-left: -20px;">
      <li><a href="#" data-toggle="modal" data-target="#Date_Modal">Date</a></li>
      <li><a href="#" data-toggle="modal" data-target="#Time_Modal">Time</a></li>
      <li><a href="#" data-toggle="modal" data-target="#DateTime_Modal">Date-Time</a></li>
      <li><a href="#" data-toggle="modal" data-target="#CheckBox_Modal">Checkbox</a></li>
      <li><a href="#" data-toggle="modal" data-target="#Radio_Modal">Radio</a></li>
      <li><a href="#" data-toggle="modal" data-target="#Decimal_Modal">Decimal</a></li>
      <li><a href="#" data-toggle="modal" data-target="#Number_Modal">Number</a></li>
      <li><a href="#" data-toggle="modal" data-target="#AutoNumber_Modal">Auto Number</a></li>
  </div>
</div>

我想要的是,每当<li>的文字大小增加时,外面只显示几个字母,..成功。我提供了我的CSS和HTML。我不知道要纠正什么,在哪里纠正。那么,有人可以帮助我实现我想要的吗?

2 个答案:

答案 0 :(得分:1)

您可以使用white-space: nowrap;阻止文字换行到第二行,并text-overflow: ellipsis;显示...而不是溢出的文字部分

答案 1 :(得分:0)

您可以使用此代码

&#13;
&#13;
<!DOCTYPE html>
<html lang="en-US">

<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
<title>Location Retriever</title>
<style>
body {
    overflow-x: hidden;
 }

/* Toggle Styles */

#wrapper {
    padding-left: 0;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#wrapper.toggled {
    padding-left: 250px;
}

#sidebar-wrapper {
    z-index: 1000;
    position: fixed;
    left: 250px;
    height: 100%;
    margin-left: -250px;
    overflow-y: auto;
    background: #000;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#wrapper.toggled #sidebar-wrapper {
    width: 250px;
}

#page-content-wrapper {
    width: 100%;
    position: absolute;
    padding: 15px;
}

#wrapper.toggled #page-content-wrapper {
    position: absolute;
    margin-right: -250px;
}

/* Sidebar Styles */

.sidebar-nav {
    position: absolute;
    top: 0;
    width: 120px;
    margin: 0;
    padding: 0;
    list-style: none;
    text-indent: -2em;
}

.sidebar-nav li {
    text-indent: 20px;
    line-height: 40px;
}

.sidebar-nav li a {
    display: block;
    text-decoration: none;
    color: #999999;
}

.sidebar-nav li a:hover {
    text-decoration: none;
    color: #fff;
    background: rgba(255,255,255,0.2);
}

.sidebar-nav li a:active,
.sidebar-nav li a:focus {
    text-decoration: none;
}

.sidebar-nav > .sidebar-brand {
    height: 65px;
    font-size: 18px;
    line-height: 60px;
}

.sidebar-nav > .sidebar-brand a {
    color: #999999;
}

.sidebar-nav > .sidebar-brand a:hover {
    color: #fff;
    background: none;
}

.sidebar-nav_wrapper    {
    float:left;
    margin-left:5px;
} 
li{

}
</style>
</head>

<body>
<div id="sidebar-wrapper">
        <div class="sidebar-nav_wrapper">
             <h4 class="sidebar-brand">Select Field Type</h4>
          <ul class="sidebar-nav" style="margin-top: 40px;">

             <li><a href="#" data-toggle="modal" data-target="#SingleLine_Modal">Single Line</a></li>
             <li><a href="#" data-toggle="modal" data-target="#PickList_Modal">Pick Listxcvxvccvxcv</a></li>
             <li><a href="#" data-toggle="modal" data-target="#MultiSelect_Modal">Multi Select</a></li>
             <li><a href="#" data-toggle="modal" data-target="#MultiLine_Modal">Multi Linesdfsdfsdfsdfsdfsdfsdf</a></li>
             <li><a href="#" data-toggle="modal" data-target="#Email_Modal">Emailsdfsdf</a></li>
             <li><a href="#" data-toggle="modal" data-target="#Phone_Modal">Phone Numberdfsdf</a></li>
             <li><a href="#" data-toggle="modal" data-target="#Mobile_Modal">Mobile Numbersdfsdf</a></li>
        </ul>
        </div>
        <div class="sidebar-nav_wrapper">
          <ul class="sidebar-nav" style="margin-top: 40px; margin-left: -20px;">
             <li><a href="#" data-toggle="modal" data-target="#Date_Modal">Datexcvxcvxcvxcv</a></li>
             <li><a href="#" data-toggle="modal" data-target="#Time_Modal">Time</a></li>
             <li><a href="#" data-toggle="modal" data-target="#DateTime_Modal">Date-Time</a></li>
             <li><a href="#" data-toggle="modal" data-target="#CheckBox_Modal">Checkbox</a></li>
             <li><a href="#" data-toggle="modal" data-target="#Radio_Modal">Radio</a></li>
             <li><a href="#" data-toggle="modal" data-target="#Decimal_Modal">Decimal</a></li>
             <li><a href="#" data-toggle="modal" data-target="#Number_Modal">Number</a></li>
             <li><a href="#" data-toggle="modal" data-target="#AutoNumber_Modal">Auto Number</a></li>
			 </ul>
        </div>
</div>
<script>
var listItems=$("li a");
for(var i=0;i<listItems.length;i++){
if (listItems[i].innerText.length>12){
listItems[i].style.whiteSpace="nowrap";	
var firstPart=listItems[i].innerText.substring(0,12);
var secPart=listItems[i].innerText.substring(12,listItems[i].innerText.length);
listItems[i].innerText=listItems[i].innerText.slice(0,12);
listItems[i].innerHTML+="<span onclick='clickSpan(this)' title="+secPart+">...</span>";

var text=listItems[i].innerHTML;
console.log("getting text "+text);
}
}

function clickSpan(span){
	span.innerText=span.title;
}

</script>
</body>
</html>
&#13;
&#13;
&#13;

你必须增加导航栏的宽度。这就是我所做的。

  • 我根据列表元素中文本的长度引入了span元素。
  • 如果长度大于12,则在锚标记之后和字符串的第一部分之后创建span标记。
  • 锚标记中的文字将是&#34; ...&#34;。如果我们想要查看额外的文字,您必须点击&#34; ...&#34;部分和文本的额外部分将与前一个文本一起查看。
  • 您可以调整额外文字的代码以显示&#34; onhover&#34;或任何其他事件。
  • 您还可以更改文本的字数限制,之后必须显示范围