css - 将样式应用于元素中的所有子元素

标签 css

我有一个如下的html

<!DOCTYPE html>
<html>
    <head>
        <style>
            p{font-size:14pt;color:red;}
        </style>
    </head>
    <body>
        <div>
            <p> this is a p without class defined</p>
        </div>
            <div class = "temp">
            <p> this is a p tag in another div</p>
        </div>
        <div class = "test">
            <p> this is a Original Mail</p>
            <p class="hello"> this is a p tag </p>
            <p> this is a p without class defined</p>
            <div> this is a div tag 
                <p> this is a p tag within the div</p>
            </div>
        </div>
    </body>
</html>

我想将样式应用到带有类测试的 div 中的所有 p 标签。

我试过像 div.test > p{font-size:14pt;color:red;} 但是随后该样式未应用于具有类名测试的 div 的子 div 中的 p 标记。 请帮我解决这个问题。

最佳答案

JSFiddle:http://jsfiddle.net/8K2yL/

你想要的选择器是:

div.test p {
    font-size:14pt;
    color:red;
}

这将选择 div.test 标签内任意位置的所有 p 标签。

选择器 div.test > p 只会选择 p 标签,它们是 div.test 的直接子标签。

关于css - 将样式应用于元素中的所有子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23364673/

相关文章:

Javascript - 尝试创建动态组合框

html - DIV 容器高度受浏览器窗口限制

javascript - Tab 覆盖 div

html - HTML/CSS 中的按钮(列表)溢出

html - 如何将背景图像保持到页面末尾

css - Twitter Bootstrap 居中按钮文本

html - 如何对齐 aside 内的 div 与 aside 的关系?

javascript - 如何像浏览器在 html 元素内部那样制作移动响应式 View ?

javascript - .prepend() .append() 正在删除原来的内容

html - 减少 CSS 网格 UL/LI 中的间隙