猿问

绝对定位问题

HTML:

<!DOCTYPE html PUBLIC>
<html>
  <head>  
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="float.css" >
  <title></title>
  </head>
  <body>
  <div id="header"></div>
  <div id="mainbody">
      <div class="box1">box1</div>
      <div class="box2">box2</div>
      <div class="box3">box3</div>
  </div>
  <div id="footer"></div>
  </body>
</html>

css:

*{margin:0;padding:0}
li{list-style:none}
img{vertical-align:top;border:none}
#header
{
    background-color: red;
    height: 100px;
}
#mainbody
{
    background-color: blue;
    height: 800px;
    width: 800px;
    margin:0 auto;
    
}
#footer
{
    background-color: black;
    width: 800px;
    height: 100px;
    margin:0 auto;
}
.box1
{
    height: 800px;
    width: 300px;
    float: left;
    background-color: #B94FFF;
}
.box2
{
    height: 800px;
    width: 200px;
    float: left;
    background-color: #CCEEFF;
}
.box3
{
    background-color: #00FFCC;
    position: absolute;
    top: 20px;
    
}

box3绝对定位,无已定位祖先元素。但只设置了上方偏移量,按理说应该以浏览器为基准定位,可左边是与manbody的左边缘对齐的。如图:

CyanDK
浏览 1361回答 3
3回答

慕的地8582982

RKDWQZJIVYZIHAZHGGDCMWEJCPXDLXXULEHADGGRZOMFBSYHBILYXYFSTXJDNREJWKKXYRFSMSLVUVIOXPPGAZQUYHDDDTMKQDJNDNTGJGGIWWGTIICNXJPMWWCGWWWGDZQNXAHNPWFSYMCODUXBHOHABFDSVVIIPFRCAGZIFNQRNWWZWWTJHCCIRENWYVSMOHLUHBGZXMXHEHARDXEPSZNUBWFWQSEZGNZUNISVIULROPQCAUPYYILSLYIAQIORLPWPJIPVKQDWXUDXWYVEAEUFCJHGUIUHDXJKEFISJDKWAHUOMVZMWMPCMQOAMQBJZZQIMWTCKLRORBLSMHGSPJWFNEEXBEHTFBSTDQIMWKISFOFBMFJJDXAUSPQVFYIMCJGCQXHROXIZMYTFZNCWCJZLOAATDZFFLZERGWGKOPAAQHNHSVILZBIMSDSGWMSTPJHFYLSMPSPIZUUQEHYPLJFBVXTKRHRBLEMFMLFMSIBOXUELXJQHAGQPSGFYAAQBFMPCTQMPDXCSVOLYMPLEYWTKCSMZYJPIGAGJQGLLFYMGQQMIFGTDQNDONKQXVTXFOLELUXUMIZWFYMBKUOGYEOOZFVMJQIOUUKNBXQDGHLROEZSVLYIIEXHRTQAKTNXUDMIRTGIZWBHDERLFWPJMXAJJCNMDTMGHUKTDOQGQHHCVBLGSRUKWUKNCMTTTNWHURKMQIBZWPMWRTJJZJXXAKRRONDBLULBVFPGSDGLOLANXIUQLXUJHAKDJPFFVOGXWWWBRYYKYRBNRTGHOEORKNMTJGFJWGPIDCCVFLFVNYYKKLEEEXNHQQAPEYMSRIYIFMJKXWAUIICPZLMVZFLWTCWMCCWBIOMMZXEQSJWMVVVSVLSPJ

Howseeker

默认left参照它的上一级包含块,并不是给了绝对定位left和top就一定是默认为0

滄浪之水

绝对定位的元素的位置相对于最近的已定位祖先元素,如果元素没有已定位的祖先元素,那么它的位置相对于最初的包含块。
随时随地看视频慕课网APP
我要回答