2013-03-22 31 views
8

我經常使用background: inherit;。像這樣,許多其他CSS屬性接受繼承作爲值。什麼是繼承在CSS中的意思?

inherit是什麼意思?它是如何工作的?

+0

繼承它從父類 – 2013-03-22 07:07:30

+4

你應該首先嚐試谷歌搜索。這是一個很好的鏈接:https://developer.mozilla.org/en-US/docs/CSS/inherit – xbonez 2013-03-22 07:07:59

+0

我已經接受了一個答案,發生了什麼? – 2013-03-24 02:08:37

回答

10

inherit意味着樣式將從元素的父項繼承。例如:

jsFiddle

HTML

<div class="blue"> 
    <div class="inherit">I also have a blue background!</div> 
</div> 

CSS

.blue { background: blue; } 
.inherit { background: inherit; } 

使用inheritbackground通常不會做你多好不過了,通常產生相同作爲默認值透明背景會。上面的例子並沒有真正添加任何東西,它將藍色背景置於藍色背景之上,inherit的主要用途是用於某些默認值,如colorfont-size

4

它做什麼呢繼承一般..這是繼承父元素的屬性

說,我們有以下的html

<div> 
    <h1>text<h1> 
</div> 

,我們有CSS像

div 
    { 
    padding : "10px" 
} 
h1 
{ 
    padding : inherit //10px 
} 

since h1 is child of div so will use the value of padding property for div 

也看到這個線程What does the inherit keyword in CSS do?

+0

我被困在樹屋,這個問題有幫助,順便說一句,謝謝 – Sidhartha 2015-03-18 05:58:09