How do I make my selectors more specific?

南楼画角 提交于 2019-12-11 04:52:12

问题


In a page I use a tabstrip with its own stylesheets. This tabstrip writen with divs and anchors.

I add some other divs into tabs but they inherit stylesheet from the outer tabstrip. This new divs has their own css classes.

Here is my question, are there a way to break this inheritance without changing the structure of css ?

Tabs' CSS Styles :

div.tabs {
    padding: .5em;
}
div.tabs div.tabs {
    padding: 0;
}
div.tabs div.tabs div {
    clear: left;
    height: 4em;
    padding: .5em;
    border: 1px solid #003366;
}

New added divs use this classes :

.graphTextItem{ font-family:sans-serif; font-size:12px; border: solid 1px #78ACFF; text-align:center; width:150px; }
.graphImageItem{ border-left: solid 1px #78ACFF; border-right: solid 1px #78ACFF; text-align:center; height:70px; }

回答1:


You could always try using different elements for each nested level instead of all divs:

<div>
   <ul>
      <li></li>
   </ul>
</div>

In the above example you can style the div, ul and li anyway you want and you can target them individually to apply style rules. Inheritance won't be a problem.




回答2:


Override each element you need to not inherit in your most specific classes.

e.g. in .graphTextItem, override height and padding.




回答3:


Not really. Inheritance is part of CSS. If you want a specific value then specify it.




回答4:


By removing div from this stylesheet solved my problem :

div.tabs div.tabs {
    clear: left;
    height: 4em;
    padding: .5em;
    border: 1px solid #003366;
}

But I still wonder whether there is a way ?



来源:https://stackoverflow.com/questions/747308/how-do-i-make-my-selectors-more-specific

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!