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

了解 CSS 繼承:一致樣式指南

Dec 21, 2024 pm 01:05 PM

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

介紹

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

您將在本文中學到什麼?

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

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

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

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

什麼是 CSS 繼承?

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

繼承的屬性

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

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

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

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

?繼承範例:

<div>



<p>Result:</p>

<p><img src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/article/000/000/000/173475751395222.jpg"  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>

<div class="code" style="position:relative; padding:0px; margin:0px;"><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/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173475751395222.jpg" 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>

登入後複製
  • Box Model Properties: width, height, margin, border, padding. Each element typically controls its own space.

  • Background: background properties, as backgrounds are often meant to be unique per element.

  • Position: position, top, left, right, bottom.

Controlling Inheritance

Using inherit: To explicitly make a property inherit from its parent:

/* 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

熱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

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

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門話題

Java教學
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1276
29
C# 教程
1256
24
靜態表單提供商的比較 靜態表單提供商的比較 Apr 16, 2025 am 11:20 AM

讓我們嘗試在這裡造成一個術語:“靜態表單提供商”。你帶上html

使Sass更快的概念證明 使Sass更快的概念證明 Apr 16, 2025 am 10:38 AM

在一個新項目開始時,Sass彙編發生在眼睛的眨眼中。感覺很棒,尤其是當它與browsersync配對時,它重新加載

每周平台新聞:HTML加載屬性,主要的ARIA規格以及從iframe轉移到Shadow dom 每周平台新聞:HTML加載屬性,主要的ARIA規格以及從iframe轉移到Shadow dom Apr 17, 2025 am 10:55 AM

在本週的平台新聞綜述中,Chrome引入了一個用於加載的新屬性,Web開發人員的可訪問性規範以及BBC Move

帶有HTML對話框元素的一些動手 帶有HTML對話框元素的一些動手 Apr 16, 2025 am 11:33 AM

這是我第一次查看HTML元素。我已經意識到了一段時間,但是尚未將其旋轉。它很酷,

紙張形式 紙張形式 Apr 16, 2025 am 11:24 AM

購買或建造是技術的經典辯論。自己構建東西可能會感覺更便宜,因為您的信用卡賬單上沒有訂單項,但是

'訂閱播客”鏈接應在哪裡? '訂閱播客”鏈接應在哪裡? Apr 16, 2025 pm 12:04 PM

有一段時間,iTunes是播客中的大狗,因此,如果您將“訂閱播客”鏈接到喜歡:

託管您自己的非JavaScript分析的選項 託管您自己的非JavaScript分析的選項 Apr 15, 2025 am 11:09 AM

有很多分析平台可幫助您跟踪網站上的訪問者和使用數據。也許最著名的是Google Analytics(廣泛使用)

See all articles