2018-07-16

1.定位

1.相对定位

相对定位就是元素在页面上正常的位置
相对定位一般不使用right,bottom

    <style>
    div{
        width:100px;
        height:100px;
        background: red;
        position: relative;
        left:200px;
        top:200px;
      
    }
</style>
 </head>
 <body>
<!-- 定位 -->
<!-- 
    相对定位
    绝对定位
 -->
 <div>

 </div>
</body>
1.png

2.绝对定位

绝对定位的元素移动的位置,是离它最近的给了定位的父元素

     <style>
    .parent{
        width:200px;
        height:200px;
        background-color:red;
        position: relative;
    }
   
    .child{
        width:50px;height:50px;background:green;
        position:absolute;
        right:0;
        bottom:0;
    }
</style>
  </head>
  <body>
<div class="parent">
    <div class="child">

    </div>
</div>
</body>
2.png

3.元素垂直水平居中

    <style>
    *{margin:0;padding:0}
    .parent{
        position: relative;
        width:300px;
        height:300px;
        background:red;
    }
    .child{
        position:absolute;
        width:50px;
        height:50px;
        background:green;
        left:50%;
        top:50%;
        margin-left:-25px;
        margin-top: -25px;
    }
    </style>
     </head>
     <body>
<div class="parent">
    <div class="child">

    </div>
</div>
   </body>
3.png

4.百分比赋值

     <style>
    *{margin:0;padding:0}
    html,body{
        width:100%;
        height:100%;
    }
    /* 子元素left,top值给百分比,是相对于父元素的width,height而言的 */
    img{
        position:absolute;
        left:50%;
        top:50%;
        width:618px;
        height:128px;
        margin-left:-307px;
        margin-top: -64px;
    }
    body{
        position: relative;
        background:url("images/bg.png") no-repeat center center;
        background-size: cover;
    }
    </style>
      </head>
      <body>
<img src="images/phone.png" alt="">
     </body>
4.png

5.search

     <style>
    *{margin:0;padding:0}
    .search{
        margin:100px;width:240px;
        height:40px;position: relative;
    }
    button{
        position:absolute;top:50%;margin-top: -11px;

        right:5px;width:23px;height:22px;
        background: url("images/icon4.png");border:none;
    }
    input{
        padding-left:20px;border:none;
        border-radius: 30px;outline: none;
        width:220px;height:40px;background:#eee;
    }
</style>
  </head>
  <body>
<div class="search">
    <input type="text" placeholder="搜索">
    <button></button>
</div>
</body>
5.png

6.fixed

    <style>
    div{
        width:20px;
        height:50px;
        background:red;
        position:fixed;
        right:10px;
        bottom:130px;
    }
</style>
     </head>
    <body>
<div></div>
      </body>

此图永远处于屏幕这个位置,不会随滚轮滚动而改变


6.png

7. z-index

z-index设置给了absolute定位元素的堆叠顺序

     <style>
    
    .parent{
        width:300px;height:300px;background: red;
        position: relative;
    }
    .one{
        width:100px;
        height:100px;
        background:green;
        position:absolute;
        z-index:100;
    }
    .two{
        width:200px;
        height:50px;position:absolute;
        background:blue;
        z-index: 10;
    }
    
</style>
     </head>
  <body>
<!-- z-index -->
<div class="parent">
    <div class="one"></div>
    <div class="two"></div>
</div>
     </body>
7.png
     <style>
    
    .parent{
        width:300px;height:300px;background: red;
        position: relative;
    }
    .one{
        width:100px;
        height:100px;
        background:green;
        position:absolute;
        z-index:100;
    }
    .two{
        width:200px;
        height:50px;position:absolute;
        background:blue;
        z-index: 101;
    }
    
</style>
</head>
 <body>
<!-- z-index -->
<div class="parent">
    <div class="one"></div>
    <div class="two"></div>
</div>
</body>
8.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,697评论 1 92
  • 1.获取一堆name名相同的div,点击弹出显示第几个div (本题主要考察闭包的用法,是在面试中常被问的题目,不...
    Wang997阅读 4,371评论 0 2
  • 学会使用CSS选择器熟记CSS样式和外观属性熟练掌握CSS各种选择器熟练掌握CSS各种选择器熟练掌握CSS三种显示...
    七彩小鹿阅读 11,445评论 2 66
  • 四种组合数据类型的含义、声明、增删改查,遍历 四种组合类型: 列表list: [append/insert/ext...
    奈斯嗷阅读 1,733评论 0 0
  • 喜欢一个人是憋不住的 喜欢是不自然的想念 喜欢是想起时的微笑 喜欢是就是想对他说我想你 喜欢是什么? 其实我真的不...
    两生阅读 3,352评论 0 1

友情链接更多精彩内容