Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 为什么我的类选择器没有覆盖标记选择器?_Html_Css_Css Selectors - Fatal编程技术网

Html 为什么我的类选择器没有覆盖标记选择器?

Html 为什么我的类选择器没有覆盖标记选择器?,html,css,css-selectors,Html,Css,Css Selectors,所以我已经编写了一周的代码,我在谷歌上搜索了30分钟,试图找到一个解决方案。如果已经有人问过,请原谅。我试着在每节课后写一个我学到的东西的总结,但它不起作用 <body> <center> h1> Module 40 </h1> </center> <p>In this module I have learned on how to use the tag <!-- <div> ---> the pur

所以我已经编写了一周的代码,我在谷歌上搜索了30分钟,试图找到一个解决方案。如果已经有人问过,请原谅。我试着在每节课后写一个我学到的东西的总结,但它不起作用

<body> <center> h1> Module 40 </h1> </center>


<p>In this module I have learned on how to use the tag <!-- <div> ---> the purpose of this tag is to create a specific group whether it is images, headers, paragraphs, etc, which you can attribute seperate properties to so it is unaffected by tag selectors. by adding a class or ID to it. </p> <br>

<div class="p1">

 <p> Like for example this paragraph is inside a div called "p1". And I have added a specific font-size for this one compared to the previous paragraph which is affected by a <strong> tag </strong> selector instead of a <strong> class </strong> selector.
</p>

</div> 

</body>

类选择器不应该覆盖标记选择器吗?字体大小15px应用于整个文本。如果我将class=“p1”添加到第二段,它将起作用。但是如果我把它添加到div中,它不应该工作吗?这不是有div的目的吗?

不,因为您的段落是.p1的子段落

所有子项都继承其父项的样式(字体大小:20px),但有能力覆盖此样式(您通过将段落样式设置为字体大小:15px完成了此操作)

您可以在此处阅读有关CSS中继承的更多信息:

必须是
。p1 p

p
{
字体大小:15px;
}
/*****类选择器*****/
.p1 p
{
字体大小:20px;
}
在本模块中,我学习了如何使用标记。此标记的目的是创建一个特定的组,无论是图像、标题、段落等,您可以将单独的属性赋予该组,使其不受标记选择器的影响。通过向其添加类或ID


例如,本段位于名为“p1”的div中。与上一段相比,我添加了一个特定的字体大小,它受标记选择器而不是选择器的影响。


您的
标记是
标记的子项,这就是它不起作用的原因。尝试将类添加到
标记中

这是因为特殊性特异性是浏览器决定哪些CSS属性值与元素最相关的方法,因此将被应用。特异性是基于由不同种类的数据组成的匹配规则。 您可以在这里找到最有用的文档之一-


在尝试生成答案之前,请先整理一下语法。例如,在第一行中,
h1>
需要是
。另外,请不要使用
标签。因为现代HTML不支持它。使用CSS将标题居中。
p
{
font-size: 15px;  
}

/*****class selector*****/
.p1
{
font-size: 20px;  
}