html - 如何添加边框css
问题描述
HTML:
<ul>
<li><a href="">someloooooooooooooooooooooong text </a></li>
<li><a href="">someloooooooooooooooooooooong text </a></li>
</ul>
我怎样才能添加这样的边框?
我的CSS:
ul {
display: block;
width: 255px;
border: 1px solid #007ab6;
}
ul a {
width: 225px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
ul a:hover {
overflow: visible;
width: unset;
display: inline-block;
border: 1px solid;
background: #ebf7fd;
}
解决方案
我使用伪元素在悬停时添加边框和背景颜色。您也可以根据需要添加任意数量的列表。无需单独处理每个列表。
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
display: block;
width: 255px;
border: 1px solid #007ab6;
list-style: none;
background: #fff;
margin: 10px;
}
ul li {
white-space: nowrap;
overflow: hidden;
width: 100%;
text-overflow: ellipsis;
display: inline-block;
position: relative;
vertical-align: middle;
}
ul li:before {
content: '';
position: absolute;
top: -1px;
left: -1px;
right: -1px;
bottom: -1px;
background: #ffffff;
border: 1px solid;
z-index: -3;
box-sizing: border-box;
}
ul li:after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #fff;
z-index: -2;
}
ul li:hover {
overflow: visible;
width: auto;
}
ul li a {
position: relative;
z-index: 1;
text-decoration: none;
background: #fff;
padding: 10px;
display: inline-block;
}
<ul>
<li><a href="">someloooooooooooooooooooooong text </a></li>
<li><a href="">someloooooooooooooooooooooong text </a></li>
<li><a href="">someloooooooooooooooooooooong text </a></li>
<li><a href="">someloooooooooooooooooooooong text </a></li>
</ul>
推荐阅读
- java - String#replaceFirst 似乎无法随机工作
- python - 将变量添加到 xpath
- python - 尝试使用带有用户输入的字典(python)
- css - 使用三点跨度触发 DropDown
- php - 如何呈现动态选择(ChoiceType)?
- java - 如何获得可识别的匹配模式数组
- laravel - 自定义 Bagisto 并添加功能
- ansible - Ansible JSON 解析错误 - 失败!=> {"msg": "模板化字符串时出现模板错误:预期的名称或数字
- c - jiffies 如何在无滴答内核中增加?
- python - 如何使用变量来识别 SQLAlchemy 表中可能的列?