2015-04-23 100 views
-1

我想在<div class="jumbotron">中包含一个大的<div>,如下所示,但它不应该与父母的CSS风格固有。如何不从父母继承CSS样式?

<div class="container"> 
    <div class="jumbotron"> 
HTML chunk here that should not inherent from parents 
    </div> 
</div> 

Here是正确渲染和here当它已被列入引导模板,其中的字体大小,更是搅乱了大的HTML块。

问题

是否有可能包括HTML块与它固有的从父母任何CSS样式?

+1

为什么你把内容放在* jumbotron里面*如果你不*要*让它继承样式?用jumbotron *包装东西的全部目的是*以某种方式对其进行设计...... – Jeroen

+1

请编辑您的Fiddles以仅使用更好的格式化CSS显示与您的问题相关的代码。 – Shaggy

+5

我认为这个链接应该回答你的问题: http://stackoverflow.com/questions/958170/how-do-i-prevent-css-inheritance ...总之:你必须覆盖你设置的任何属性父母。作为@Shaggy,提到了 –

回答

1

一些方法一起工作或防止CSS继承:

  1. 最显而易见的方法是删除了超大屏幕的CSS和自己编写。

  2. 其次,你可以尝试改变CSS来更具体。例如使用高级css选择器IE:.jumbotron > .childClass。或者像+ :not() :first-child :last-child和其他)的东西。取决于你的用例。请参阅advanced selectors

或者如果您不想修改或更改父类的CSS。那么另一种选择是用较高的父母覆盖它。例如...

<div class="jumboTronParent"> 
    <div class="container"> 
    <div class="jumbotron"> 
     <div class="myChildClass"></div> 
    </div> 
    </div> 
</div> 

.jumboTronParent .jumbotron > .myChildClass { 
    font-size:1em; 
    // applies font style to just first level children with this class 
} 

.jumboTronParent .jumbotron .myChildClass { 
    font-size:1em; 
    // applies font style to all children with class 
}