css屬性繼承問題

來源:互聯網
上載者:User

標籤:css屬性繼承

概念:所謂CSS的繼承是指被包在內部的標籤將擁有外部標籤的樣式性質,它是依賴於祖先-後代的關係的。繼承特性最典型的應用通常發揮在整個網頁的樣式預設,需要指定為其它樣式的部份設定在個別元素裡即可。

css規則告訴瀏覽器如何去渲染HTML頁面上特定的元素,css規則由兩大部分組成,選取器+聲明塊,如:body{color:red;}

color屬性的繼承
案例1://body{color:red;}
 <p>>父級P<strong class="class_s" id="id_s">子級strong</strong>父級P</p>
 <ul>
        父級ul
         <li>子級li1</li>
         <li>子級li2</li>
        父級ul結束
     </ul>

html文檔樹的理解,請看圖: 文檔樹.png

list-style屬性的繼承
案例2:ul{list-style:none} li{list-style:none}


優先順序(權值越大優先順序越高)來確定css規則繼承指定的值:
1、聲明的權值比繼承過來的權值高;
案例4:ul{color:pink} li{color:lightblue}

2、多種樣式混合應用時的優先順序如下:
內聯樣式 > id選取器 > class選取器和偽類別選取器 > 元素選取器和偽類元素選取器 > 萬用字元選取器 > 外聯樣式
案例5: strong{color:black}
 .class_s{color:wheat}
  #id_s{color:blue}

 

3、在文檔中出現的順序,越在後面出現的,優先順序越高
案例5:  strong{color:black} strong{color: lawngreen}

4、人為定義CSS繼承優先順序(important規則)通過在一條規則的分號前插入!important這樣一個短語來標記一條重要規則
 其優先順序最高
案例6:strong{color:black !important}


並非所有的屬性是繼承的,例如boder,width,display等屬性就不能繼承
案例7: p{border:1px solid;width:200px}
 <p>父級P<strong>子級strong</strong>父級P</p>
<script>
     var p=document.getElementsByTagName("p")[0];
        var num=0;
        function bul(){
            if(num==0){
                p.style.display="none";
                num=1;
            }
            else{
                p.style.display="block";
                num=0;
            }

        }
    </script>
 

下面總結了一些關於樣式表的屬性是否繼承:
1、字型(Font)
繼承性——有:font、color、font-family、font-size、font-size-adjust、font-stretch 、font-style 、font-style 、text-underline-position 、font-variant 、 text-transform line-height、letter-spacing 、word-spacing               
     
繼承性——無:text-decoration 、text-shadow


2、文本(Text)
繼承性——有:text-indent 、text-align 、layout-flow 、writing-mode 、white-space 、word-wrap 、text-kashida-space 、layout-grid 、layout-grid-char 、layout-grid-char-spacing 、layout-grid-line 、layout-grid-mode 、layout-grid-type

繼承性——無:text-overflow 、vertical-align 、direction、unicode-bidi 、word-break 、line-break 、text-autospace、text-justify 、ruby-align 、ruby-overhang 、ruby-position 、ime-mode 、


3、背景(Background)
繼承性都為無:
background、background-origin 、background-clip 、background-size 、background-attachment 、background-color、background-image 、background-position 、background-positionX 、background-positionY 、background-repeat 、layer-background-color 、layer-background-image 


4、定位(Position)
繼承性都為無:
position 、z-index 、top、right 、bottom 、left


5、尺寸(Dimensions)
繼承性都為無:
height 、max-height 、min-height 、width、max-width 、min-width


6、布局(Layout)
繼承性都為無:
clear 、float、clip 、overflow、overflow-x、overflow-y、display、visibility


7、外補丁(Margins)
繼承性都為無:
margin 、margin-top 、margin-right 、margin-bottom 、margin-left


8、輪廓(Outlines)
繼承性都為無:
outline、outline-color 、outline-style 、outline-style 、outline-width


9、邊框(border)
繼承性都為無:
border、border-color 、border-style 、border-image 、border-radius 、box-shadow、border-width 、border-top 、border-top-color 、border-top-style 、border-top-width 、border-right 、border-right-color 、border-right-style 、border-right-width 、border-bottom 、border-bottom-color 、border-bottom-style 、border-bottom-width 、border-left 、border-left-style 、border-left-width


10、內補丁(Padding)
繼承性都為無:
padding 、padding-top 、padding-right 、padding-bottom 、padding-left

 
11、列表(Lists)
繼承性——有:list-style 、list-style-image 、list-style-position 、list-style-type

繼承性——無:marker-offset


12、表格(Tabble)
繼承性都為有:
border-collapse 、border-spacing 、caption-side 、empty-cells 、table-layout 、speak-header


13、其他
繼承性都為無:
cursor、zoom

css屬性繼承問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.