搜尋
首頁web前端css教學了解 CSS 繼承:一致樣式指南

大家好,歡迎回到我的部落格! ?

介紹

讓我們深入了解 CSS 繼承 的世界。我們將探討哪些屬性會傳遞下去,如何控制此流程,以及為什麼它對您的設計很重要。本指南是為每個人(從初學者到經驗豐富的專業人士)精心設計的,可幫助您利用繼承來獲得更乾淨、更易於維護的 CSS。

您將在本文中學到什麼?

  • 繼承基礎知識:屬性繼承意味著什麼。

  • 繼承哪些屬性:深入了解繼承和非繼承屬性。

  • 控制繼承:如何使用 CSS 關鍵字和技術管理繼承。

  • 深入範例:展示繼承的實際場景,並附有更詳細的解釋。

什麼是 CSS 繼承?

CSS 繼承是指某些屬性會自動從父元素傳遞給子元素。此機制有助於在嵌套元素之間一致地應用樣式,而無需重新聲明它們。

繼承的屬性

** ✅ 常見的繼承屬性:**

  • 文字屬性:字體系列、字體大小、顏色、行高、文字對齊。這些旨在在文字內容中保持一致。

  • 可見性:可見性(但不顯示)。

  • 遊標:互動式提示的遊標。

?繼承範例:

<div>



<p>Result:</p>

<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173475751395222.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Understanding CSS Inheritance: A Guide to Consistent Styling"></p>

<p>Here, all child elements inside the div will have the Helvetica font unless overridden.</p>

<h2>
  
  
  <strong>Properties That Don't Inherit</strong>
</h2>

<h3>
  
  
  <strong>✖️ Non-Inherited Properties:</strong>
</h3>

<ul>
<li><p><strong>Box Model Properties</strong>: width, height, margin, border, padding. Each element typically controls its own space.</p></li>
<li><p><strong>Background</strong>: background properties, as backgrounds are often meant to be unique per element.</p></li>
<li><p><strong>Position</strong>: position, top, left, right, bottom.</p></li>
</ul>

<h2>
  
  
  <strong>Controlling Inheritance</strong>
</h2>

<p><strong>Using</strong> inherit: To explicitly make a property inherit from its parent:<br>
</p>

<pre class="brush:php;toolbar:false">/* If the parent has a specific color, the child will adopt it */
.child-element {
    color: inherit;
}

使用 初始 : 將屬性重設為其瀏覽器預設值:

/* Resets the font-size to the default size of the browser */
.reset-font-size {
    font-size: initial;
}

使用 unset : 將屬性恢復為其繼承值或初始值:

/* Will inherit if a parent has a color set, otherwise, it will be initial */
.unset-color {
    color: unset;
}

實際範例

  1. 簡化版式
<article>





<pre class="brush:php;toolbar:false">/* Nothing needed here; inheritance does the job */

結果:文章中的所有文字均使用 Georgia 字體和深灰色,打造統一的外觀。

Understanding CSS Inheritance: A Guide to Consistent Styling

  1. 重寫繼承
<nav>
    <ul>
        <li><a href="#home">Home</a></li>
        <li><a href="#about">About</a></li>
    </ul>
</nav>
nav {
    font-size: 16px; /* Base size for navigation */
    color: #333; /* Base color for text */
}

nav a {
    color: inherit; /* Inherits the color from nav, which is #333 */
    font-size: inherit; /* Also inherits 16px */
    text-decoration: none; /* Default is none, but doesn't inherit */
}

nav a:hover {
    color: #0056b3; /* Changes on hover, overriding inheritance */
}

結果:連結以與其父導航相同的大小和顏色開始,但在懸停時改變顏色,顯示對繼承的控制。

Understanding CSS Inheritance: A Guide to Consistent Styling

注意:為了更好地檢查結果並試驗程式碼,您可以複製貼上 Codepen.io 上的所有程式碼區塊。

  1. 版面的自訂繼承
<div>



<p>Result:</p>

<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173475751395222.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Understanding CSS Inheritance: A Guide to Consistent Styling"></p>

<p>Here, all child elements inside the div will have the Helvetica font unless overridden.</p>

<h2>
  
  
  <strong>Properties That Don't Inherit</strong>
</h2>

<h3>
  
  
  <strong>✖️ Non-Inherited Properties:</strong>
</h3>

<ul>
<li><p><strong>Box Model Properties</strong>: width, height, margin, border, padding. Each element typically controls its own space.</p></li>
<li><p><strong>Background</strong>: background properties, as backgrounds are often meant to be unique per element.</p></li>
<li><p><strong>Position</strong>: position, top, left, right, bottom.</p></li>
</ul>

<h2>
  
  
  <strong>Controlling Inheritance</strong>
</h2>

<p><strong>Using</strong> inherit: To explicitly make a property inherit from its parent:<br>
</p>

<pre class="brush:php;toolbar:false">/* If the parent has a specific color, the child will adopt it */
.child-element {
    color: inherit;
}

結果:內容 div 保持與其容器相同的內邊距和背景,確保無縫的視覺流。

Understanding CSS Inheritance: A Guide to Consistent Styling

為什麼理解繼承至關重要

  • 一致性:繼承有助於用更少的程式碼保持整個網站的樣式一致性。

  • 效能:透過利用繼承,您可以減少 CSS 規則的數量,這有助於解決載入時間和特異性問題。

  • 靈活性:了解如何控制繼承可以實現更動態的設計,其中元素可以根據需要共享或覆蓋樣式。

結論

CSS 繼承就像網頁設計的家譜,確保樣式以邏輯、有效率的方式傳遞下去。透過理解和操縱繼承,您可以製作既一致又靈活的設計。

請記住,雖然某些屬性會自然繼承,但您始終可以使用繼承、初始和取消設定等 CSS 關鍵字進行控制。

編碼愉快! ?


?大家好,我是 Eleftheria,社群經理、 開發人員、公共演講者和內容創作者。

?如果您喜歡這篇文章,請考慮分享。

所有連結 | X | 領英

以上是了解 CSS 繼承:一致樣式指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
模擬鼠標運動模擬鼠標運動Apr 22, 2025 am 11:45 AM

如果您曾經在現場演講或課程中必須顯示一個互動動畫,那麼您可能知道它並不總是那麼容易與您的幻燈片進行互動

通過Astro Action和Fuse.js為搜索提供動力通過Astro Action和Fuse.js為搜索提供動力Apr 22, 2025 am 11:41 AM

對於Astro,我們可以在構建過程中生成大部分網站,但是有一小部分服務器端代碼可以使用Fuse.js之類的搜索功能來處理搜索功能。在此演示中,我們將使用保險絲搜索一組個人“書籤”

未定義:第三個布爾值未定義:第三個布爾值Apr 22, 2025 am 11:38 AM

我想在我的一個項目中實現一條通知消息,類似於您在保存文檔時在Google文檔中看到的信息。換句話說,一個

捍衛三元聲明捍衛三元聲明Apr 22, 2025 am 11:25 AM

幾個月前,我正在使用黑客新聞(就像一個人一樣),並且遇到了一篇(現已刪除的)文章,內容涉及不使用if語句。如果您是這個想法的新手(就像我

使用網絡語音API進行多語言翻譯使用網絡語音API進行多語言翻譯Apr 22, 2025 am 11:23 AM

自科幻小說以來,我們就幻想著與我們交談的機器。今天這很普遍。即便如此,製造的技術

JetPack Gutenberg塊JetPack Gutenberg塊Apr 22, 2025 am 11:20 AM

我記得當古騰堡被釋放到核心時,因為那天我在WordCamp我們。現在已經過去了幾個月,所以我想我們越來越多的人

在VUE中創建可重複使用的分頁組件在VUE中創建可重複使用的分頁組件Apr 22, 2025 am 11:17 AM

大多數Web應用程序背後的想法是從數據庫中獲取數據,並以最佳方式將其呈現給用戶。當我們處理數據時

使用'盒子陰影”和剪輯路徑一起使用'盒子陰影”和剪輯路徑一起Apr 22, 2025 am 11:13 AM

讓我們在一個情況下做一些似乎有意義的事情的情況下逐步進行一些逐步,但是您仍然可以用CSS欺騙來完成它。在這個

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。