为什么font-size为0后margin也为0,但line-height为0后margin却不为0?
在这个问题下:
在按钮1~4后添加文字后,为什么trigger按钮上方出现了空格
看到了这个答案:
那不是间距 是 由于 文符带来的基线冗余,其实就是行高,解决办法就是font-size:0或者line-height:0;
改了font-size之后的确消除了间距(使margin为0了)
但是改了line-height之后却没用
这是font-size的代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>浮动去空格</title>
<style>
button { margin: 0; }
p { clear: both; }
</style>
</head>
<body style="font-size:0;">
<button>按钮1</button><button>按钮2</button><button>按钮3</button><button>按钮4</button><p><input type="button" id="trigger" value="点击按钮浮动"></p>
<script>
var trigger = document.getElementById("trigger"),
buttons = document.getElementsByTagName("button");
var length = buttons.length;
if (trigger && length > 0) {
trigger.onclick = function() {
for (var index = 0; index < length; index += 1) {
buttons[index].style["cssFloat" in trigger.style? "cssFloat": "styleFloat"] = "left";
}
};
}
</script>
</body>
</html>
效果(的确消除了间距,使margin为0了):
这是line-height的代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>浮动去空格</title>
<style>
button { margin: 0; }
p { clear: both; }
</style>
</head>
<body style="line-height:0;">
<button>按钮1</button><button>按钮2</button><button>按钮3</button><button>按钮4</button><p><input type="button" id="trigger" value="点击按钮浮动"></p>
<script>
var trigger = document.getElementById("trigger"),
buttons = document.getElementsByTagName("button");
var length = buttons.length;
if (trigger && length > 0) {
trigger.onclick = function() {
for (var index = 0; index < length; index += 1) {
buttons[index].style["cssFloat" in trigger.style? "cssFloat": "styleFloat"] = "left";
}
};
}
</script>
</body>
</html>
效果(没有消除间距,margin不为0):