[英]How to display <a href> long text with fix width
我有固定寬度的水平外來html -list,每個列表中的文本時間都更長,我需要將文本溢出到下一行,但沒有滾動條。
ul.horizontal-funtions-list{ list-style-type: none; width: 100%; padding-left: 0; padding-top: 10px; padding-bottom: 10px; margin: 0; overflow-x:auto; white-space: nowrap; background-color: blueviolet; } ul.horizontal-funtions-list li{ display: inline-block; zoom:1; width: 100px; margin-left: 20px; padding: 20px; background-color: greenyellow; } ul.horizontal-funtions-list li a{ display: block; width: 80px; color:gray; }
<ul class="horizontal-funtions-list"> <li><a href="#Function A">I am long function name</a></li> <li><a href="#home">Home</a></li> </ul>
我試過了display:在ul.horizontal-funtions-list li a中阻止,但無法正常工作
嘗試
ul.horizontal-funtions-list li a {
display: block;
color:gray;
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
/* OPTIONAL - FIXED height
overflow: hidden;
height: 50px;
*/
}
請注意width
已刪除。
這是演示輸出的代碼段。
ul.horizontal-funtions-list{ list-style-type: none; width: 100%; padding-left: 0; padding-top: 10px; padding-bottom: 10px; margin: 0; overflow-x:auto; white-space: nowrap; background-color: blueviolet; } ul.horizontal-funtions-list li{ display: inline-block; zoom:1; width: 100px; margin-left: 20px; padding: 20px; background-color: greenyellow; } ul.horizontal-funtions-list li a{ display: block; color:gray; white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ overflow: hidden; height: 50px; }
<ul class="horizontal-funtions-list"> <li><a href="#Function A">I am long function name</a></li> <li><a href="#home">Home</a></li> </ul>
ul.horizontal-funtions-list{ list-style-type: none; width: 100%; padding-left: 0; padding-top: 10px; padding-bottom: 10px; margin: 0; overflow-x:auto; white-space: nowrap; background-color: blueviolet; } ul.horizontal-funtions-list li{ display: inline-block; zoom:1; width: 100px; margin-left: 20px; padding: 20px; background-color: greenyellow; } ul.horizontal-funtions-list li a{ display: block; width: 80px; color:gray; /*new style*/ white-space: pre-wrap; }
<ul class="horizontal-funtions-list"> <li><a href="#Function A">I am long function name</a></li> <li><a href="#home">Home</a></li> </ul>
只為<a>
標簽添加此CSS代碼white-space: pre-wrap;
試試這個代碼。
white-space: nowrap
。 overflow-x: auto
。 width: 100px
(也不要在其內部的width: 80px
的錨點上)。 我主要刪除您擁有的樣式的地方,並設置適當的padding
和margin
以獲取正確的間距。
ul.horizontal-funtions-list { margin: 0; padding: 0 20px 20px 0; background-color: blueviolet; list-style-type: none; } ul.horizontal-funtions-list li { display: inline-block; margin: 20px 0 0 20px; padding: 20px; background-color: greenyellow; } ul.horizontal-funtions-list li a { color: gray; }
<ul class="horizontal-funtions-list"> <li><a href="#Function A">I am long function name</a></li> <li><a href="#home">Home</a></li> <li><a href="#Function B">Another long function name</a></li> <li><a href="#Function C">Yet another</a></li> <li><a href="#Function D">We can keep going</a></li> </ul> <p>Space in between.</p> <ul class="horizontal-funtions-list"> <li><a href="#Function A">Shorter list</a></li> <li><a href="#home">Doesn't wrap</a></li> </ul>
ul.horizontal-funtions-list { list-style-type: none; width: 100%; padding-left: 0; padding-top: 10px; padding-bottom: 10px; margin: 0; overflow-x: auto; /* white-space: nowrap; */ background-color: blueviolet; } ul.horizontal-funtions-list li { display: inline-block; zoom: 1; /* width: 100px; */ margin-left: 20px; padding: 15px; background-color: greenyellow; } ul.horizontal-funtions-list li a { display: block; max-width: 100px; color: gray; }
<ul class="horizontal-funtions-list"> <li><a href="#Function A">I am long function name</a></li> <li><a href="#home">I am long function name2</a></li> </ul>
從鏈接中刪除width屬性,並添加white-space: pre-wrap;
而已。
ul.horizontal-funtions-list li a{
/* width: 80px; */
display: block;
white-space: pre-wrap;
color:gray;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.