鍍金池/ 問答/Android  HTML/ css absolute定位問題

css absolute定位問題

我有一個(gè)div要絕對(duì)定位在左上方,請(qǐng)問下面兩種寫法區(qū)別在哪里?

div{
    position: absolute;
    left:0;
    top:0;
}


div{
    position: absolute;
    left:0;
    top:0;
    right:0;
    bottom:0;
}
回答
編輯回答
病癮

mozilla的文檔這樣寫

When both top and bottom are specified, as long as height is
unspecified, auto, or 100%, both top and bottom distances will be
respected. Otherwise, if height is constrained in any way, the top
property takes precedence and the bottom property is ignored.

也就是,你的后一種寫法可能導(dǎo)致div的大小拉大為全屏,如果div大小已經(jīng)被限制住,right和bottom會(huì)被忽略。

2017年10月3日 17:41
編輯回答
六扇門

如果非全屏用第一種,全屏用第二種

2018年7月10日 16:27
編輯回答
愿如初

第一種寫法只是定位。
第二種寫法會(huì)撐滿距離自己最近的有定位屬性的父級(jí)節(jié)點(diǎn)。

2018年8月6日 10:37
編輯回答
大濕胸

第一種只是元素的定位; 第二種寫法既有元素定位也設(shè)置了元素的大小,將元素?fù)伍_到設(shè)置了定位為非static父節(jié)點(diǎn)的大小

2017年2月7日 09:38