css - 仅为特定 div 内的元素设置样式

标签 css

我只想将我的自定义样式应用到特定 div 内的元素中。我的意思是整个页面上有许多具有相同类的元素,我只想将这些自定义 CSS 应用于 div 内的元素,数据属性如 div data-some-feature=...不适用于其余

     <div class="row" data-language>
      <section class="posts by-tags show-grid" data-language>
           <div class='right a'>a</div>
             <div class="row">';
               <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 m-right">
                 <div class="a">
                   <h6>apple</h6>
                   <p>some text...</p>
                 </div>
               </div>
             </div>
    </section>
      </div>

  <div class='row'>
    <section .....>
         ......
     </section>
   </div>

<style>
   div[data-dictionary-language].posts {
     //some style
     }
</style>

我只想使用 div[data-dictionary-language] 一次,然后应用我的自定义 CSS,如下所示:

   <style>
     div[data-dictionary-language]{
      .tags{
         //some style only apply to all elements are inside the specific div
        }
      .posts{
        //some style only apply to all elements are inside the specific div
       }
    }
   </style>

最佳答案

你快到了,像这样尝试:

/*     ___________ select the div with data-language attribute 
       |           ______ note the space here
       |          |    ________ select child of div[data-language] with class posts
 ______|________  | __|__                                                          */
div[data-language] .posts {
    background-color: orange;
}
<div class="row" data-language>
      <section class="posts by-tags show-grid" data-language>
           <div class='right a'>a</div>
             <div class="row">';
               <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 m-right">
                 <div class="a">
                   <h6>apple</h6>
                   <p>some text...</p>
                 </div>
               </div>
             </div>
    </section>
      </div>

  <div class='row'>
    <section .....>
         ......
     </section>
   </div>

关于css - 仅为特定 div 内的元素设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56555656/

相关文章:

css - 如何在 <a> 标签中使用 z-index?

javascript - 跨浏览器鼠标滚轮,通过一个滚轮滚动 100% 浏览器高度

css - pandoc/markdown 中的重要空白通常在哪里受到监管?

html - 网站内容在 Moz 上显示,但在 Chrome 上不显示

html - 根据子元素的文本自动调整芯片组件(div)的宽度

css - 以可读的方式呈现长链接列表 HTML

javascript - 同时设置多个元素的样式

javascript - 在 IE 中使用空媒体查询调用 matchMedia 失败,参数无效

php - Href 链接不适用于移动 View 。半页 href 链接有效

css - Bootstrap 站点结构 - 重新定位/流动导航