第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

在這個問題下:

在按鈕1~4后添加文字后,為什么trigger按鈕上方出現(xiàn)了空格

看到了這個答案:

那不是間距 是 由于 文符帶來的基線冗余,其實就是行高,解決辦法就是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 回答

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

這里有三種方法可以實現(xiàn)margin為0,達到?jīng)]有空格的效果,

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

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

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

你可以試試

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_aslongas_0

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


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

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

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號