css 的繼承性


css 的繼承性是什么?

在面向對象語言都會存在繼承的概念,在面向對象語言中,繼承的特點:繼承了父類的屬性和方法。
那么我們現在主要研究css,css就是在設置屬性的。不會牽扯到方法的層面。

css的繼承:就是給父級設置一些屬性,子級繼承了父級的該屬性,這就是我們的css中的繼承。
官方解釋,繼承是一種規則,它允許樣式不僅應用於特定的html標簽元素,而且應用於其后代元素。
  • 代碼 實例
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>繼承性</title>
  <style type="text/css">
    div{
        color: deeppink;
        font-size: 40px;
        background: aqua;
        width: 400px;
        height: 400px;
    }
  </style>
</head>
<body>
  <div>
      小白兔<span>白又白</span>兩只<span>耳朵</span>豎起來
  </div>
</body>
</html>
  • 執行效果

父元素的屬性那些可以被子元素繼承,哪些不能呢?

一、無繼承性的屬性

1、display:規定元素應該生成的框的類型

2、文本屬性:

​ vertical-align:垂直文本對齊

​ text-decoration:規定添加到文本的裝飾

​ text-shadow:文本陰影效果

​ white-space:空白符的處理

​ unicode-bidi:設置文本的方向

3、盒子模型的屬性:width、height、margin 、margin-top、margin-right、margin-bottom、margin-left、border、 border-style、border-top-style、border-right-style、border-bottom-style、border-left-style、border-width、border-top-width、border-right-right、border-bottom-width、border-left-width、border-color、border-top-color、border-right-color、border-bottom-color、border-left-color、border-top、border-right、border-bottom、border-left、padding、padding-top、padding-right、padding-bottom、padding-left

4、背景屬性:background、background-color、background-image、background-repeat、background-position、background-attachment

5、定位屬性:float、clear、position、top、right、bottom、left、min-width、min-height、max-width、max-height、overflow、clip、z-index

6、生成內容屬性:content、counter-reset、counter-increment

7、輪廓樣式屬性:outline-style、outline-width、outline-color、outline

8、頁面樣式屬性:size、page-break-before、page-break-after

9、聲音樣式屬性:pause-before、pause-after、pause、cue-before、cue-after、cue、play-during

二、有繼承性的屬性

1、字體系列屬性

​ font:組合字體

​ font-family:規定元素的字體系列

​ font-weight:設置字體的粗細

​ font-size:設置字體的尺寸

​ font-style:定義字體的風格

​ font-variant:設置小型大寫字母的字體顯示文本,這意味着所有的小寫字母均會被轉換為大寫,但是所有使用小型大寫 字體的字母與其余文本相比,其字體尺寸更小。

​ font-stretch:對當前的 font-family 進行伸縮變形。所有主流瀏覽器都不支持。

​ font-size-adjust:為某個元素規定一個 aspect 值,這樣就可以保持首選字體的 x-height。

2、文本系列屬性

​ text-indent:文本縮進

​ text-align:文本水平對齊

​ line-height:行高

​ word-spacing:增加或減少單詞間的空白(即字間隔)

​ letter-spacing:增加或減少字符間的空白(字符間距)

​ text-transform:控制文本大小寫

​ direction:規定文本的書寫方向

​ color:文本顏色 a元素除外

3、元素可見性:visibility

4、表格布局屬性:caption-side、border-collapse、border-spacing、empty-cells、table-layout

5、列表布局屬性:list-style-type、list-style-image、list-style-position、list-style

6、生成內容屬性:quotes

7、光標屬性:cursor

8、頁面樣式屬性:page、page-break-inside、windows、orphans

9、聲音樣式屬性:speak、speak-punctuation、speak-numeral、speak-header、speech-rate、volume、voice-family、 pitch、pitch-range、stress、richness、、azimuth、elevation

三、所有元素可以繼承的屬性

1、元素可見性:visibility

2、光標屬性:cursor

四、內聯元素可以繼承的屬性

1、字體系列屬性

2、除text-indent、text-align之外的文本系列屬性

五、塊級元素可以繼承的屬性

1、text-indent、text-align

參考博客

作 者: 郭楷豐
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角 推薦一下。您的鼓勵是博主的最大動力!
自 勉:生活,需要追求;夢想,需要堅持;生命,需要珍惜;但人生的路上,更需要堅強。 帶着感恩的心啟程,學會愛,愛父母,愛自己,愛朋友,愛他人。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM