html - 将不同的 css 应用于同一类的有效方法

标签 html css

所以,我有一个 div 的集合,如下所示用于 My Page

 <div class="My_Page"> 
    <div class="one"> 
        <div class="two"> 
           <a href="#"> Hi </a>
        </div>
    </div>
 </div>

然后我有另一个页面具有相同的类 = onetwo

 <div class="Your_Page"> 
    <div class="one"> 
        <div class="two"> 
           <a href="#"> Hi </a>
        </div>
    </div>
 </div>

但是,我正在尝试根据它们所在的页面将不同的 css 应用于 class= onetwo

例如:

  1. 我的页面: .My_Page .one{width:100%;}

  2. 你的页面: .Your_Page .one{width:50%}

我有一个包含这两个代码的 css 文件。

对于任何一个页面,这两个 css 标记都被加载,我觉得必须有更有效的方法来根据它所在的父级 div 应用不同的 css

还是我做得对?

谢谢

最佳答案

.page{ ....  //common css for both page one and two}
.page .one{ width:100% .... //common for both  } 
.page .two{ .... //common for both  } 

.My_page .two{ width:50%;}


<div class="page My_page">
   <div class="one"> 
        <div class="two"> 
           <a href="#"> Hi </a>
        </div>
    </div>
</div>

关于html - 将不同的 css 应用于同一类的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30133362/

相关文章:

javascript - jQuery 用里面的其他元素替换元素的 html/text

javascript - 有没有办法让元素在 mousedown 事件激活之前消失?

html - 我的 table 怎么了?

html - 巨大的隐形 div 覆盖了整个网页

javascript - 无效的字符串长度错误

html - 圆 Angular div 中的文本垂直居中

javascript - 位置固定和背面可见性

jquery - aria-labelledby 未被 NVDA Chrome 读取

css - 背景大小属性的浏览器问题

css - 如何将标题放置在页面顶部并按源代码顺序位于内容之后?