zoukankan      html  css  js  c++  java
  • CSS中的z-index属性如何使用

    z-index属性介绍

    • 只有设置了定位我们才会使用到该z-index属性,如:固定定位相对定位绝对定位
    • 定位元素默认的z-index属性值是0
    • 如果2个定位的元素都没有设置z-index属性,后者会覆盖到前者。
    • 如果2个定位的元素都设置了z-index属性,并且数值一样大还是后者会覆盖到前者。
    • z-index属性的属性值大的就会覆盖小,就是设置元素的层级。

    z-index属性实践

    • 用实践证明这句话,如果2个定位的元素都没有设置z-index属性,后者会覆盖到前者。

    • 代码块

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>z-index属性</title>
      <style>  
        div{
           200px;
          height: 200px;
        } 
         .div1{
           background-color: red;
           position: relative;
           top: 50px;
           left: 50px;
         }
         .div2{
           background-color: slateblue;
           position: relative;
           left: 100px;
         }
        
      </style>
    </head>
    
    <body>
        <div class="div1"></div>
        <div class="div2"></div>
    </body>
    
    </html>
    
    • 结果图

    • 用实践来证明这句话,如果2个定位的元素都设置了z-index属性,并且数值一样大还是后者会覆盖到前者。
    • 代码块

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>z-index属性</title>
      <style>  
        div{
           200px;
          height: 200px;
        } 
         .div1{
           background-color: red;
           position: relative;
           top: 50px;
           left: 50px;
           z-index: 2;
         }
         .div2{
           background-color: slateblue;
           position: relative;
           left: 100px;
           z-index: 2;
         }
        
      </style>
    </head>
    
    <body>
        <div class="div1"></div>
        <div class="div2"></div>
    </body>
    
    </html>
    
    • 结果图

    • 用实践来证明这句话,z-index属性的属性值大的就会覆盖小,就是设置元素的层级。

    • 代码块

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>z-index属性</title>
      <style>  
        div{
           200px;
          height: 200px;
        } 
         .div1{
           background-color: red;
           position: relative;
           top: 50px;
           left: 50px;
           z-index: 3;
         }
         .div2{
           background-color: slateblue;
           position: relative;
           left: 100px;
           z-index: 2;
         }
        
      </style>
    </head>
    
    <body>
        <div class="div1"></div>
        <div class="div2"></div>
    </body>
    
    </html>
    
    • 结果图

  • 相关阅读:
    vs2010 + .net3.5 MSCharts使用介绍与例子
    TFS服务连接TF31002 出错
    SharePoint CAML 通过时间查询
    SharePoint2010项目总结汇总
    jquery 获取和设置 select下拉框的值
    How to Create Multilingual Webpart in SharePoint 2010 (C# 方式)
    sharepoint母版页固定宽度与纵向滚动条靠右边(修改版)
    JavaScript进行GET和POST请求
    端口简介大全
    程序员学习能力提升三要素
  • 原文地址:https://www.cnblogs.com/lq0001/p/12116422.html
Copyright © 2011-2022 走看看