我想实现以下结构:
[gfhtfg..., kgjrfg..., asd, mrhgf, ]
^-------^ ^-------^ ^-------^ ^-------^
X X X X
(X = a fixed length)
我有<div>一个固定长度的,在它里面有一个水平的、逗号分隔ul的链接列表( )。
元素应具有固定宽度,因此<li>如果链接超过固定长度,将显示省略号(使用text-overflow属性)。
我知道两种使列表水平的方法。一个正在使用display: inline,另一个正在使用该float属性。
- 使用第一种方法,我无法设置固定宽度,因为 CSS 规范不允许设置内联元素的宽度。
- 第二种方法会造成混乱:O
float在元素上设置<a>,旨在限制那里的宽度,将其与逗号分开。
没有浏览器兼容性问题,我只需要支持 WebKit。
我包含了我尝试使用的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>a title</title>
<style>
body { font-family: arial; font-size: 10pt; }
div {
height: 30px;
width: 300px;
background: #eee;
border: 1px solid #ccc;
text-align: center;
}
ul { margin: 0px; padding: 0px; }
ul li:after { content: ","; }
ul li:last-child:after { content: ""; }
ul li a { text-decoration: none; color: #666; }
ul li {
margin: 0px;
padding: 0px;
list-style: none;
overflow: hidden;
text-overflow: ellipsis;
-webkit-text-overflow: ellipsis;
/* Inline elements can't have a width (and they shouldn't according to the specification */
display: inline;
width: 30px;
}
</style>
</head>
<body>
<div>
<ul>
<li><a href="#">a certain link</a></li>
<li><a href="#">link</a></li>
<li><a href="#">once again</a></li>
<li><a href="#">another one</a></li>
</ul>
</div>
</body>
</html>
谢谢你。