Li 与 Li 之间有看不见的空白间隔是什么原因引起?
浏览器的默认⾏为是把inline元素间的空⽩字符(空格换⾏tab)渲染成⼀个空格,当然空格就占用一个字符的宽度。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
<style>
ul{
width: 400px;
height: 100px;
list-style-type: none;
}
li{
width: 100px;
height: 100px;
display: inline-block;
}
ul>:nth-child(1){
background-color: rgb(250, 149, 166);
}
ul>:nth-child(2){
background-color: rgb(239, 133, 34);
}
ul>:nth-child(3){
background-color: rgb(48, 214, 131);
}
</style>
</html>
有什么解决办法?
1.更换HTML布局结构
<!-- 讲li写在同一行,中间不要有间隔 -->
<ul>
<li></li><li></li><li></li>
</ul>
不使用换行,也就是将所有的li都在一行展示
2.将ul内font-size:0
ul{
width: 400px;
height: 100px;
list-style-type: none;
/* 如果li里面有文字,需要单独设置文字大小 */
font-size: 0;
}
将ul内font-size:0
,但是单独li设置⽂字⼤⼩
3.设置字符间距(letter-spacing)
ul{
width: 400px;
height: 100px;
list-style-type: none;
/* 字符间隔-8px */
letter-spacing: -8px;
}
li{
width: 100px;
height: 100px;
display: inline-block;
/* 字符间隔默认 */
letter-spacing: normal;
}
将ul的字符间距letter-spacing:-8px
,但是单独li设置字符间距letter-spacing:normal
4.为li设置左浮动
li{
width: 100px;
height: 100px;
float: left;
}
将display: inline-block
替换成float: left
,得到如下效果