消除display: inline-block 元素间的换行符空格间隙问题。

消除display: inline-block 元素间的换行符空格间隙的方法

想把一些元素在一行显示,最简单的方法是display:inline-block;变成行内块元素
但是同行显示的inline-block元素之间会有一个换行符,一般占据4px位置(例如Chrome浏览器 字体默认大小16px,行内块元素间隔4px,字体大小越大间隔越大)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style>
			.wrap{
				width: 500px;
				height: 500px;
				border: 1px solid red;
				font-size:16px;
			}
			.wrap div{
				display: inline-block;
			}
			.div1{
				width: 100px;
				height: 100px;
				background-color: #f00;
			}
			.div2{
				width: 100px;
				height: 100px;
				background-color: #0f0;
			}
			.div3{
				width: 100px;
				height: 100px;
				background-color: #00f;
			}
		</style>
	</head>
	<body>
		<div class="wrap">
			<div class="div1">div1</div>
			<div class="div2">div2</div>
			<div class="div3">div3</div>
			<div class="div4"></div>
		</div>
	</body>
</html>

效果:行内块间隔问题

解决行内块间隔的方法:

1.直接去掉换行符或注释换行

<div class="wrap">
	<div class="div1">div1</div><div class="div2">div2</div>
	<div class="div3">div3</div>
	<div class="div4"></div>
</div>
		<div class="wrap">
			<div class="div1">div1</div><!--
			--><div class="div2">div2</div>
			<div class="div3">div3</div>
			<div class="div4"></div>
		</div>

解决方法1
缺点:代码的可读性变差。

2.使用浮动float
缺点:浮动元素会脱离文档流,但是也可以使用清楚浮动解决,清楚浮动下篇再提

3.letter-spacing消除 inline-block 元素间的换行符空格间隙

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style>
			.wrap{
				width: 500px;
				height: 100px;
				border: 1px solid red;
				 /*letter-spacing消除 inline-block 元素间的换行符空格间隙问题*/
				letter-spacing: -4px;
			}
			.wrap div{
				display: inline-block;
			}
			.div1{
				width: 100px;
				height: 100px;
				background-color: #f00;
			}
			.div2{
				width: 100px;
				height: 100px;
				background-color: #0f0;
			}
			.div3{
				width: 100px;
				height: 100px;
				background-color: #00f;
			}
		</style>
	</head>
	<body>
		<div class="wrap">
			<div class="div1">div1</div>
			<div class="div2">div2</div>
			<div class="div3">div3</div>
		</div>
	</body>
</html>

缺点:字体间隔会缩进,如果元素内有字体元素,最好不用这种方法
效果:
在这里插入图片描述
4.为父元素中设置font-size: 0,在子元素上重置正确的font-size

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style>
			.wrap{
				width: 500px;
				height: 100px;
				border: 1px solid red;
				font-size: 0;		
			}
			.wrap div{
				display: inline-block;
			}
			.div1{
				width: 100px;
				height: 100px;
				background-color: #f00;
				font-size: 16px;
			}
			.div2{
				width: 100px;
				height: 100px;
				background-color: #0f0;
				font-size: 16px;
			}
			.div3{
				width: 100px;
				height: 100px;
				background-color: #00f;
				font-size: 16px;
			}
		</style>
	</head>
	<body>
		<div class="wrap">
			<div class="div1">div1</div>
			<div class="div2">div2</div>
			<div class="div3">div3</div>
		</div>
	</body>
</html>

缺点:麻烦,代码量多,容易影响到其他样式

5.设置父元素,display:table

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style>
			.wrap{
				width: 500px;
				height: 100px;
				border: 1px solid red;
				display: table;
			}
			.wrap div{
				display: inline-block;
			}
			.div1{
				width: 100px;
				height: 100px;
				background-color: #f00;
			}
			.div2{
				width: 100px;
				height: 100px;
				background-color: #0f0;
			}
			.div3{
				width: 100px;
				height: 100px;
				background-color: #00f;
			}
		</style>
	</head>
	<body>
		<div class="wrap">
			<div class="div1">div1</div>
			<div class="div2">div2</div>
			<div class="div3">div3</div>
		</div>
	</body>
</html>

缺点:影响到父级元素样式

消除display: inline-block 元素间的换行符空格间隙的方法应该还有很多,目前总结我见过的几种
每种方法都有优缺点,使用时应该根据需求选择相应的方法

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值