• css中margin-left与left的区别


    研究下拉菜单和弹出菜单时比较所得:
    1.直接在css中设置left生效的前提是必须设置父容器position:absolute或relative,如果不设置则会显示为最近一个定位的父对象左边相关的位置:
    测试代码:

    1. <div style="position:relative;top:100px;left:100px;border:1px solid blue;100px;height:100px;">  
    2. <div id="outer" style="position:relative;margin-top:25px;margin-left:25px;50px;height:50px;border:1px solid red;">  
    3.     <div id="inner" style="position:absolute;left:15px;top:15px;25px;height:25px;border:1px solid green;"></div>  
    4. </div>  
    5. </div>  
    	<div style="position:relative;top:100px;left:100px;border:1px solid blue;100px;height:100px;">
    	<div id="outer" style="position:relative;margin-top:25px;margin-left:25px;50px;height:50px;border:1px solid red;">
    		<div id="inner" style="position:absolute;left:15px;top:15px;25px;height:25px;border:1px solid green;"></div>
    	</div>
    	</div>

    测试效果:
    不设置红线框的position,设置蓝线框的position,绿线框的位置:


    设置红线框的position,设置蓝线框的position,绿线框的位置:


    2.设置margin-left则只会出现在父对象的左边的相对位置,不考虑是否设置了position。
    测试代码:

    1. <div style="position:relative;top:100px;left:100px;border:1px solid blue;100px;height:100px;">  
    2. <div id="outer" style="position:relative;margin-top:25px;margin-left:25px;50px;height:50px;border:1px solid red;">  
    3.     <div id="inner" style="position:absolute;margin-left:15px;margin-top:15px;25px;height:25px;border:1px solid green;"></div>  
    4. </div>  
    5. </div>  
    	<div style="position:relative;top:100px;left:100px;border:1px solid blue;100px;height:100px;">
    	<div id="outer" style="position:relative;margin-top:25px;margin-left:25px;50px;height:50px;border:1px solid red;">
    		<div id="inner" style="position:absolute;margin-left:15px;margin-top:15px;25px;height:25px;border:1px solid green;"></div>
    	</div>
    	</div>

    不设置红线框的position,设置蓝线框的position,绿线框的位置:


    设置红线框的position,设置蓝线框的position,绿线框的位置:


    3.设置margin-right无法起到将element定位到右边相距XX位置,但设置right可以。
    测试代码:
    <div style="position:relative;98px;height:63px;background-image:url('images/pic.png');margin-left:100px;margin-top:300px;">
    <img src="images/delete.gif" style="position:absolute;top:-10px;right:-5px;"/>
    </div>


    测试效果:
    设置margin-right

    设置right

  • 相关阅读:
    winston写日志(译)
    H5打字机特效
    Flutter 手指放大 平移 旋转 Widget
    51nod1432【贪心】
    死锁的例子
    C# SpinLock用法。
    鼓音效
    rm-rf
    cdoj 1334 郭大侠与Rabi-Ribi Label:贪心+数据结构
    1092 回文字符串(51nod)
  • 原文地址:https://www.cnblogs.com/kuangwong/p/6083233.html
Copyright © 2020-2023  润新知