html - 如何在 CSS 中定义和使用类?
问题描述
我以前几乎从未用过 CSS 编码,我在我的 web 开发课程的 power point 中看到了这一点(它最初是用法语写的,所以我不确定我的翻译是否完全准确):
The <p> with the property class=indented and inside the element <td> will have the following CSS properties:
td p .indented {text-indent: 50px;}
所以我尝试使用此代码对其进行测试,但它不起作用:
<body>
this is the body of the document <br>
<style>
td p .indented {font-weight: bold; color:red}
td {color:blueviolet}
</style>
<table>
<tr>
<th> 1 </th>
<th> 2 </th>
<th> 3 </th>
</tr>
<tr>
<td><p>test</p></td>
<td>test</td>
<td><p class=indented>test</p></td>
</tr>
</table>
</body>
所有 3 个test
都以紫色显示,但没有以红色和粗体文本显示。难道我做错了什么?谢谢
解决方案
你的 CSS 规则有一个小错误。td p .indented
目标元素的类indented
是p
元素的后代,而元素的后代又是td
元素的后代。
您可能正在寻找的是使用作为元素子p
级的类来定位元素。删除选择器定义之间和中的空格:indented
td
p
.indented
<body>
this is the body of the document <br>
<style>
td p.indented {font-weight: bold; color:red}
td {color:blueviolet}
</style>
<table>
<tr>
<th> 1 </th>
<th> 2 </th>
<th> 3 </th>
</tr>
<tr>
<td><p>test</p></td>
<td>test</td>
<td><p class=indented>test</p></td>
</tr>
</table>
</body>
推荐阅读
- android - Android 深层链接以“启动 URL”打开,但不输入确切的 url
- eclipse - 运行 Eclipse.exe 并提供工作区后出现错误
- excel - 如何在 Excel 工作表中使用 IF 编写多个条件?
- c# - C#如何序列化二进制增量数据
- excel - 值卡在 excel 宏中
- angularjs - 默认情况下,如何在角度页面加载时使复选框为“真”
- json - 团队中的自适应卡片,无法让按钮在中心对齐
- javascript - 动态检测滚动条装订线
- mediawiki - 如何将 xml 转储中的模板添加到我的 MediaWiki?
- asp.net-core - .Net Core WebApi 目标框架“net5.0”在构建应用程序时解释为“net50”