首页 > web前端 > html教程 > CSS absolute与relative不得不说的故事

CSS absolute与relative不得不说的故事

WBOY
发布: 2016-07-21 14:53:03
原创
977 人浏览过

写在开篇:

absolute说:“relative,我这辈子都不想看见你!”

为什么呢?它们明明那么相亲相爱,形影不离,这之中到底发生了什么不为人知的故事,竟然让absolute如此讨厌relative?

要想找到问题的答案,请跟我来。。。

 

relative对absolute的限制之一

absolute,拥有top、right、bottom、left四项技能,从此天高任鸟飞,海阔任鱼跃;本来想去哪儿就去哪儿,自由自在,生活是那么地美好。

直到有一天,在出去玩的路上,遇到一个relative,跳出来大喊一声;“此山是我开,此树是我栽,要从此路过,留下买路财!”

然后,我们可爱哒absolute小朋友就乖乖地屈服了。

但是无良的relative收到好处居然还不放行,死活不让absolute过去。。。额,这个故事就是这样子的。。。相信大家都懂的。。。

额,还是写个demo吧,看这里:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 3</span>     <span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 4</span>         <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="utf-8"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 5</span>         <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>relative对absolute的限制1<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 6</span>         <span style="color: #0000ff;"><span style="color: #800000;">style</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 7</span> <span style="background-color: #f5f5f5; color: #800000;">            .box </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 8</span> <span style="background-color: #f5f5f5; color: #ff0000;">                width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">500px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 9</span> <span style="background-color: #f5f5f5; color: #ff0000;">                height</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">250px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">10</span> <span style="background-color: #f5f5f5; color: #ff0000;">                background-color</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> pink</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">11</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">30px auto 50px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">12</span> <span style="background-color: #f5f5f5; color: #ff0000;">                line-height</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 250px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">13</span> <span style="background-color: #f5f5f5; color: #ff0000;">                text-align</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">center</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">14</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">15</span> 
<span style="color: #008080;">16</span> <span style="background-color: #f5f5f5; color: #800000;">            .box p </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">17</span> <span style="background-color: #f5f5f5; color: #ff0000;">                display</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> inline-block</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">18</span> <span style="background-color: #f5f5f5; color: #ff0000;">                vertical-align</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> middle</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">19</span> <span style="background-color: #f5f5f5; color: #ff0000;">                width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">300px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">20</span> <span style="background-color: #f5f5f5; color: #ff0000;">                font-size</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 16px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">21</span> <span style="background-color: #f5f5f5; color: #ff0000;">                line-height</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 1.5</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">22</span> <span style="background-color: #f5f5f5; color: #ff0000;">                text-align</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> left</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">23</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">24</span> 
<span style="color: #008080;">25</span> <span style="background-color: #f5f5f5; color: #800000;">            .box2 p </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">26</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin-left</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 30px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">27</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">28</span> 
<span style="color: #008080;">29</span> <span style="background-color: #f5f5f5; color: #800000;">            .box img </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">30</span> <span style="background-color: #f5f5f5; color: #ff0000;">                position</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> absolute</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">31</span> <span style="background-color: #f5f5f5; color: #ff0000;">                left</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">0</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">32</span> <span style="background-color: #f5f5f5; color: #ff0000;">                top</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">0</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">33</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">34</span> 
<span style="color: #008080;">35</span> <span style="background-color: #f5f5f5; color: #800000;">            .box2 </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">36</span> <span style="background-color: #f5f5f5; color: #ff0000;">                position</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> relative</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">37</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">38</span> 
<span style="color: #008080;">39</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">style</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">40</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">41</span>     <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">42</span>         <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box box1"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">43</span>             <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;">44</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>今天,absolute小朋友出去玩啦,开启left:0; top:0; 这个组合技能,一切顺利,到达了天边。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">45</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">46</span> 
<span style="color: #008080;">47</span>         <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box box2"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">48</span>             <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;">49</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>今天,absolute小朋友又出去玩啦,同样开启left:0; top:0; 这个组合技能,但是天公不作美,出门就碰到relative这个坏家伙,于是止步于relative的地方。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">50</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">51</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">52</span> <span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
View Code

 

relative对absolute的限制之二

上回说到,absolute小朋友用top、right、bottom、left四项技能出去玩的时候被relative半路给截住了。

这次absolute小伙伴吸取了教训,不用那四项技能了,用margin负值技能,一样能跑出去玩。

很好,虽然房子周围有overflow:hidden的魔法结界,但是我们的absolute小朋友直接无视之,从容通过,大家鼓掌!!!

但是,但是,但是那个无良的relative又来啦。

还好,还好,还好这次的是margin负值技能,absolute小朋友成功突破了relative的限制,跑出去啦。。。

额,好像有什么不对。。。

我跑出去那部分身体怎么不见啦?

demo在这里:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 3</span>     <span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 4</span>         <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="utf-8"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 5</span>         <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>relative对absolute的限制2<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 6</span>         <span style="color: #0000ff;"><span style="color: #800000;">style</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 7</span> <span style="background-color: #f5f5f5; color: #800000;">            .box </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 8</span> <span style="background-color: #f5f5f5; color: #ff0000;">                width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">500px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 9</span> <span style="background-color: #f5f5f5; color: #ff0000;">                height</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">250px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">10</span> <span style="background-color: #f5f5f5; color: #ff0000;">                background-color</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> pink</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">11</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">50px auto 50px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">12</span> <span style="background-color: #f5f5f5; color: #ff0000;">                overflow</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> hidden</span><span style="background-color: #f5f5f5; color: #000000;">;</span>                
<span style="color: #008080;">13</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">14</span> 
<span style="color: #008080;">15</span> <span style="background-color: #f5f5f5; color: #800000;">            .box p </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">16</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 20px 30px 20px 120px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">17</span> <span style="background-color: #f5f5f5; color: #ff0000;">                text-align</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> left</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">18</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">19</span> 
<span style="color: #008080;">20</span> <span style="background-color: #f5f5f5; color: #800000;">            .box img </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">21</span> <span style="background-color: #f5f5f5; color: #ff0000;">                position</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> absolute</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">22</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin-left</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">-30px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">23</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin-top</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> -45px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">24</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">25</span> 
<span style="color: #008080;">26</span> <span style="background-color: #f5f5f5; color: #800000;">            .box2 </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">27</span> <span style="background-color: #f5f5f5; color: #ff0000;">                position</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> relative</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">28</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">29</span> 
<span style="color: #008080;">30</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">style</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">31</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">32</span>     <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">33</span>         <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box box1"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">34</span>             <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;">35</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>今天,absolute小朋友出去玩啦。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">36</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>鉴于上次用top、right、bottom、left技能的时候遇到了relative,导致出不去的尴尬,今天用的是margin负值技能。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">37</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>虽然房子周围设置了overflow:hidden,但这位强大的小朋友依然跑出去啦。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">38</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">39</span> 
<span style="color: #008080;">40</span>         <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box box2"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">41</span>             <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;">42</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>今天,absolute小朋友又出去玩啦,同样用的是margin负值技能。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">43</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>房子周围也是设置了overflow:hidden属性的,但是这位强大的小朋友依然跑出去啦。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">44</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>咦,什么情况,我房子外边的那部分身体呢?<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">45</span>             <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>我擦嘞,relative你什么时候来的?<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">46</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">47</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">48</span> <span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
View Code

 

经过这两次事件,absolute小朋友就开始讨厌relative小伙伴了,用一句耳熟能详的话来说就是,“我再也不想看见你啦”!

 

请给absolute自由

absolute小朋友天生就会飞,使用top、right、bottom、left想去哪儿就去哪儿。

absolute小朋友还会margin负值精确定位,想怎么玩就怎么玩。

absolute小朋友那么可爱,不信你看:

这么Q,这么萌,为什么要被限制住呢,宝宝向往天空和自由啊!啊!啊!啊!啊!

absolute说:“relative,我再也不想看见你了!”

 

但是,理想很丰满,现实很骨感。。。

absolute是不可能得偿所愿的,在定位的时候,或多或少都会使用relative来限制absolute,毕竟absolute实在太会飞了,不限制不行的啊。

不过,限制归限制,这只是relative和absolute两个人的恩怨,千万不要影响到其它花花草草。

所以,我们在使用relative+absolute定位的时候,要遵循relative影响最小化原则

来个例子,如下:

<span style="color: #008080;">  1</span> <span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">  2</span> <span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">  3</span>     <span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">  4</span>         <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="utf-8"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">  5</span>         <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>relative影响最小化<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">  6</span>         <span style="color: #0000ff;"><span style="color: #800000;">style</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">  7</span> <span style="background-color: #f5f5f5; color: #800000;">            * </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">  8</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">0</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">  9</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 10</span> 
<span style="color: #008080;"> 11</span> <span style="background-color: #f5f5f5; color: #800000;">            .wraper </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 12</span> <span style="background-color: #f5f5f5; color: #ff0000;">                width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">800px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 13</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">50px auto</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 14</span> <span style="background-color: #f5f5f5; color: #ff0000;">                background-color</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> #ccc</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 15</span> <span style="background-color: #f5f5f5; color: #ff0000;">                border</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 3px solid green</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 16</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 17</span> 
<span style="color: #008080;"> 18</span> <span style="background-color: #f5f5f5; color: #800000;">            .box </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 19</span> <span style="background-color: #f5f5f5; color: #ff0000;">                width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">500px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 20</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">50px auto</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 21</span> <span style="background-color: #f5f5f5; color: #ff0000;">                background-color</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> orange</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 22</span> <span style="background-color: #f5f5f5; color: #ff0000;">                border</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 3px solid black</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 23</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 24</span> 
<span style="color: #008080;"> 25</span> <span style="background-color: #f5f5f5; color: #800000;">            img </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 26</span> <span style="background-color: #f5f5f5; color: #ff0000;">                border</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">1px solid blue</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 27</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">10px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 28</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 29</span> 
<span style="color: #008080;"> 30</span> <span style="background-color: #f5f5f5; color: #800000;">            p </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 31</span> <span style="background-color: #f5f5f5; color: #ff0000;">                padding-left</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">10px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 32</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">10px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 33</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 34</span> 
<span style="color: #008080;"> 35</span> <span style="background-color: #f5f5f5; color: #800000;">            .absolute </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 36</span> <span style="background-color: #f5f5f5; color: #ff0000;">                position</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> absolute</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 37</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 38</span> 
<span style="color: #008080;"> 39</span> <span style="background-color: #f5f5f5; color: #800000;">            .box2 .absolute </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 40</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin-left</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> -3px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 41</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin-top</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> -3px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 42</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 43</span> 
<span style="color: #008080;"> 44</span> <span style="background-color: #f5f5f5; color: #800000;">            .box3 .absolute </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 45</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin-top</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">-3px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 46</span> <span style="background-color: #f5f5f5; color: #ff0000;">                margin-left</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">450px</span><span style="background-color: #f5f5f5; color: #000000;">;</span> 
<span style="color: #008080;"> 47</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 48</span> 
<span style="color: #008080;"> 49</span> <span style="background-color: #f5f5f5; color: #800000;">            .box4 .relative </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 50</span> <span style="background-color: #f5f5f5; color: #ff0000;">                position</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> relative</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 51</span> <span style="background-color: #f5f5f5; color: #ff0000;">                border</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">2px solid red</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 52</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 53</span> 
<span style="color: #008080;"> 54</span> <span style="background-color: #f5f5f5; color: #800000;">            .box4 .absolute </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 55</span> <span style="background-color: #f5f5f5; color: #ff0000;">                top</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">-10px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 56</span> <span style="background-color: #f5f5f5; color: #ff0000;">                right</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">-10px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 57</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;"> 58</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">style</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 59</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 60</span>     <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 61</span>         <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="wraper"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 62</span>             <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box box1"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 63</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://image.tianjimedia.com/uploadImages/2012/237/4134F4528419.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:50px;height:30px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 64</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 65</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 66</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 67</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>这是没有任何定位,正常流的样子。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 68</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>后续。。。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 69</span>             <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 70</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #008000;"><!--</span><span style="color: #008000;">关闭wraper</span><span style="color: #008000;">--></span>
<span style="color: #008080;"> 71</span>         <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="wraper"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 72</span>             <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box box2"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 73</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="absolute"</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">="http://image.tianjimedia.com/uploadImages/2012/237/4134F4528419.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:50px;height:30px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 74</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 75</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 76</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 77</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>如果我们要将图像定位到黑色边框的左上角,应该怎么办?<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 78</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>很简单,绝对定位,不加任何偏移量,用margin微调,完成!<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 79</span>             <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 80</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #008000;"><!--</span><span style="color: #008000;">关闭wraper</span><span style="color: #008000;">--></span>
<span style="color: #008080;"> 81</span>         <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="wraper"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 82</span>             <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box box3"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 83</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="absolute"</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">="http://image.tianjimedia.com/uploadImages/2012/237/4134F4528419.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:50px;height:30px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 84</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 85</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 86</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 87</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>如果我们要将图像定位到黑色边框的右上角,应该怎么办?<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 88</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;">第一种方法,像刚才那样,直接绝对定位,再用margin调整;
</span><span style="color: #008080;"> 89</span>                 这种方法需要消耗一点脑细胞,算一算到底应该位移多少像素。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 90</span>             <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 91</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #008000;"><!--</span><span style="color: #008000;">关闭wraper</span><span style="color: #008000;">--></span>
<span style="color: #008080;"> 92</span>         <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="wraper"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 93</span>             <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box box4"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 94</span>                 <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="relative"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 95</span>                     <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="absolute"</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">="http://image.tianjimedia.com/uploadImages/2012/237/4134F4528419.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:50px;height:30px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 96</span>                 <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 97</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 98</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;"> 99</span>                 <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="A picture"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width:100px;height:150px"</span> <span style="color: #0000ff;">/></span>
<span style="color: #008080;">100</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>第二种方法,就是大家喜闻乐见的relative+absolute定位啦。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">101</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>不过,看在absolute那么讨厌relative的面子上,relative就只欺负它一个就好了,千千万万不要影响到其它花花草草。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">102</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>具体来说,给这个需要定位的图像外加一层div,设置relative,让这个relative只影响需要绝对定位的元素。<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">103</span>                 <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>relative影响最小化!<span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">104</span>             <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">105</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #008000;"><!--</span><span style="color: #008000;">关闭wraper</span><span style="color: #008000;">--></span>
<span style="color: #008080;">106</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">107</span> <span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
View Code

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板