亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么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了):http://img1.sycdn.imooc.com//5977cd480001783c11250803.jpg

這是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):http://img1.sycdn.imooc.com//5977cd6e0001cb7b11470890.jpg


正在回答

2 回答

可以呀,我把你代碼復制過來試下,完全可以,但你的button標簽后沒有文字,效果不是很明顯,

這里有三種方法可以實現margin為0,達到沒有空格的效果,

1、font-size設置為0,如果button后面有文字,這個方法就不太好,font-size為0后,文字根本顯示不出來,而且,不用點擊按鈕,按鈕就自動聚在一起,

2、line-height,line-height不要設置為0,不然效果出來會很丑,行高和按鈕高度一樣就可以了,我這里設置的是32px,而且可實現點擊,

3、margin 明明沒設置,但它存在,是因為一些默認樣式,去掉默認就好了,可以添加*{margin:0;padding:0;}語句,就可以了

你可以試試

0 回復 有任何疑惑可以回復我~
#1

qq_aslongas_0

糾正一下,出了點bug,line-height單獨設的時候,只能設為0,但效果真的不盡人意,
2017-08-24 回復 有任何疑惑可以回復我~
font-size:0;是設置文字大小的,margin是設置偏移位置的。文字為0自然間距也小了


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么font-size為0后margin也為0,但line-height為0后margin卻不為0?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號