html - 为什么 html 在我选择的范围之外使用选择器

标签 html css

嗨,我包含的选择器选择了不在我选择的类中的其他元素,有人可以解释为什么会发生这种情况。这里是代码....请注意我如何将 p 标签放在类之外,它仍然选择它。

<!DOCTYPE html>
<html>
<head>
<style>
.one_one  h1, p{
    display:inline-block;
    background-color:pink;
    width:100px;

}
.one_two h1, p{
    display:inline-block;
    background-color:blue;
    width:100px;
}
.one_three h1, p{
    background-color:orange;
    width:100px;
    display:inline-block;


}
</style>
</head>
<body>
<div class="webdesign_info">
            <section class="one_one">
                    <h1>A wide variety of choices</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                           sed do eiusmod tempor incididunt ut labore et dolore
                           magna aliqua ut enim ad minim veniam.</p>
            </section>

            <section class="one_two">
                    <h1>Easy to search Through</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                           sed do eiusmod tempor incididunt ut labore et dolore
                           magna aliqua ut enim ad minim veniam.</p>
            </section>

            <section class="one_three">
                    <h1>1170px wide content area</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                           sed do eiusmod tempor incididunt ut labore et dolore
                           magna aliqua ut enim ad minim veniam.</p>
            <section>




        </div>
<p>Chrisk is sm</p>
</body>
</html>

最佳答案

CSS 选择器中的逗号分隔具有相同样式的多个选择器。所以,你的 CSS

.one_three h1, p 

被浏览器读取为:

apply following styles to all (<h1>) in all of class (one_three), as well as all (<p>)

结果是 <p>由样式表中选择 p(在本例中为 .one_three h1, p)的最后一个样式设置样式

作为对您评论的回应,您可以选择 <p><h1>.one_three像这样:

.one_three h1, .one_three p 

它被浏览器读取为:

apply following styles to all (<h1>) in all of class (one_three), as well as all (<p>) in all of class (one_three)

如果你想要一个更短的类型版本,你可以使用 SCSS:

.one_three{
  h1, p{
    //Styles
  }
}

但是,在编译时,它会输出与上面发布的相同的 CSS,只是更容易阅读和跟踪开发

关于html - 为什么 html 在我选择的范围之外使用选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33375745/

相关文章:

javascript - 车牌格式化和验证

javascript - 如何使用原型(prototype)从包装器 div 获取所有外部(父)div id 值?

javascript - 添加事件监听器并在动态生成的元素上使用纯 JavaScript 获取点击值

css - SASS:在 index.js 中导入的 scss 不呈现(使用 create-react-app)

html - 如何让一个类(class)有三种不同的颜色?

c# - 网络核心 : Is it Possible to Merge TagBuilder Style Attributes with CSS Files?

python - 如何选择一个 html 元素,无论它在 selenium 中的哪个框架?

html - 如何使图像在 HTML 中移动位置?

javascript - 当在我的页面中央打开一个 div 时,无法滚动的正确方法是什么?

javascript - 在 Aurelia 中,如何根据按键事件获取文本区域内光标的坐标?