zoukankan      html  css  js  c++  java
  • css中marginleft与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>  

    测试效果:
    不设置红线框的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>  

    不设置红线框的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

  • 相关阅读:
    bootstrapValidator重新校验/全选回显
    mybatis遍历map参数查询
    location.href传json字符串
    springmvc异步处理
    intellIJ IDEA学习笔记3
    intellIJ IDEA学习笔记2
    intellIJ IDEA学习笔记
    maven国内镜像
    Docker版本Jenkins的使用
    Docker容器网络
  • 原文地址:https://www.cnblogs.com/liuxuze/p/2946557.html
Copyright © 2011-2022 走看看