萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 可繼承屬性清單:子元素自動獲得的樣式

CSS 部分屬性具有繼承性:子元素未顯式設定時,自動使用父元素的計算值。

主要可繼承屬性分類

不可繼承屬性 (佈局/盒模型/背景/邊框等)

  • display, position, float, width, height, margin, padding, border, background, overflow, z-index, top/right/bottom/left, flex/grid 相關...

強制控制繼承

/* 強制繼承 (含非繼承屬性) */
.child { border: inherit; }

/* 重置為初始值 */
.child { color: initial; }

/* 智能重置 */
.child { all: unset; }  /* 繼承屬性→inherit,非繼承→initial */

/* 重置為瀏覽器預設 */
.child { all: revert; }

實務提醒

  • 瀏覽器 UA 樣式表可能以預設值覆寫你的繼承值 (如 input 字體),需顯式 inherit
  • 善用繼承減少重複宣告:容器設 font/color,內容區自動套用
  • line-height 用無單位數值 (如 1.5) 時,子元素繼承「倍數」而非計算值,隨字體縮放自適應
copyright © 萬盛學電腦網 all rights reserved
類別屬性
字體/文字font-family, font-size, font-weight, font-style, font-variant, line-height, color, letter-spacing, word-spacing, text-align, text-indent, text-transform, white-space, direction, unicode-bidi
列表list-style, list-style-type, list-style-position, list-style-image
游標/可見性cursor, visibility
語音 (CSS Speech)speak, speak-punctuation, speak-numeral, speech-rate, volume, voice-family, pitch, pitch-range, stress, richness, azimuth, elevation