gpt4 book ai didi

如果没有 h1 选择器,HTML 和 CSS p 选择器将无法工作

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:12 24 4
gpt4 key购买 nike

大家好,我是 HTML 和 CSS 的新手,我遇到了一个奇怪的问题。我正在尝试使用样式和 p 选择器更改文本的颜色,只要我也使用 h1 选择器,它就可以工作。这是我的代码。

<!DOCTYPE html> <!-- Indicates which HTML version is being used  -->
<html> <!-- HTML starts here --> <!-- Also called root element -->
<head> <!-- background/setup information goes inside head -->
<meta charset = "utf-8" /> <!-- defines character set -->
<title> Hello Internet! </title> <!-- website title name -->

<style type = "text/css"> <!-- Added for CSS -->


h1{

}


p{
color: red;
}

</style>

</head>

<body>


<p> This text is red </p>



</html>

所以对于这段代码,我的文本行实际上是红色的,但是一旦我从代码中删除 h1 选择器,文本就会变回黑色。谁能解释为什么会这样?

最佳答案

你在那里放了一个 HTML 注释!删除评论,它将起作用:

<!DOCTYPE html>
<!-- Indicates which HTML version is being used -->
<html>
<!-- HTML starts here -->
<!-- Also called root element -->

<head>
<!-- background/setup information goes inside head -->
<meta charset="utf-8" />
<!-- defines character set -->
<title> Hello Internet! </title>
<!-- website title name -->
<style type="text/css">
p {
color: red;
}
</style>
</head>

<body>
<p> This text is red </p>
</body>

</html>

如果你想给 <style> 添加评论标记,使用 CSS 注释:

/**
* This is a valid CSS comment
*/
/* This one too! */
/***************************
****************************
Even this one!
****************************
***************************/

关于如果没有 h1 选择器,HTML 和 CSS p 选择器将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com