html - 我错过了什么?CSS中的字体颜色
问题描述
如果我在下面编写相同的代码块但不包含“body”元素(它似乎也没有做任何事情),那么我的 h1 文本保持黑色。为什么会这样?如果我理解正确,我应该能够添加 h1 部分并且它应该改变颜色对吗?
编辑:我想澄清我想排除“身体”部分,并想了解为什么我不能。作为另一个例子,我之前在代码中设置了图像的大小。当我将这些设置放在 h1 部分之前时,h1 改变了颜色。如果 h1 在之后,那么它再次保持黑色。
<style>
body{
color: pink;
}
h2{
color: blue;
}
</style>
<h1 id="title" class="title">Katherine Johnson</h1>
<div id="img-div">
<img id= "image" src="https://www.nasa.gov/sites/default/files/thumbnails/image/26646856911_ca242812ee_o_1.jpg" alt="Katherine Johnson" class="center">
<h2 id="tribute-info"><ul> <li>Born in White Sulphur Springs, West Virginia, in 1918, her intense curiosity and brilliance with numbers vaulted her ahead several grades in school.</li> </li>By 13, she was attending the high school on the campus of historically black West Virginia State College. </li> <li>At 18, she enrolled in the college itself, where she made quick work of the school’s math curriculum and found a mentor in math professor W. W. Schieffelin Claytor, the third African American to earn a PhD in mathematics. </li> <li>She graduated with highest honors in 1937 and took a job teaching at a black public school in Virginia. </li> </ul></h2>
<a id="tribute-link" target="_blank" href="https://www.nasa.gov/content/katherine-johnson-biography"> Learn more about Katherine here</a>
</div>
解决方案
您提供了 class 和 id 以使 CSS 生效。使用 class/id 来使 CSS 生效,而不是使用锚标签。
<style>
body{
color: pink;
}
h2Class{
color: blue;
}
</style>
<h1 id="title" class="h2Class">Katherine Johnson</h1>
推荐阅读
- timestamp - 在现有表中为雪花中的默认时间戳添加新列
- vb.net - 在 Visual Studio Code 中,为什么数字“7”的突出显示与其他整数不同?我怎样才能改变它?
- python - 如何在颜色条上绘制散点图?
- database-design - 重复行也重复组吗?(1NF)
- excel - Aspose.Cells - 在 .Net Core 中签署 VBA 项目
- c# - 如何在日志中没有任何密码痕迹的情况下创建 Postgresql 用户?
- multithreading - 使用 atomic c++11 实现线程安全倒数为零
- c# - C# for 循环的语法和时间复杂度差异
- nuget-package - 从 launchSettings.json 调用 Nuget 包缓存中的可执行文件
- java - NumberFormatException:对于输入字符串:“2”