html - 样式属性嵌套在一个 id 一个类和另一个属性中
问题描述
我有以下 HTML 代码:
html body .section1 #red-text p {
color: red;
}
<html>
<head>
<link ref="stylesheet" href="styles.css">
</head>
<body>
<div class="section1">
<div id="red-text">
<p>This text should be red.</p>
</div>
</div>
<p>This text should be left alone.</p>
</body>
</html>
我想使用 CSS 更改“此文本应为红色”文本的颜色。我尝试了以下CSS代码无济于事:
html body .section1 #red-text p {
color: red;
}
我在这里做错了什么?
解决方案
正如上面其他人所说,代码在技术上是好的,但是你最好在造型时练习使用类而不是 ID!此外,正如 Mike 所说,ID 是唯一的,因此不需要前缀。
也就是说,您还没有标记为已解决,所以这可能是代码看起来更有效的样子。
`<html>
<head>
<link ref="stylesheet" href="styles.css">
</head>
<body>
<div id="section1">
<div id="inner-section">
<p class="red-text">This text should be red.</p>
</div>
</div>
<p>This text should be left alone.</p>
</body>
</html>`
然后您可以简化 CSS,因为您需要为红色的任何其他文本都可以是同一个类!所以:
`.red-text {
color: red;
}`
或者,如果您只想为该 div 中的红色文本设置样式:
`#inner-section.red-text {
color: red;
}`
推荐阅读
- dart - dart中await引入程序的后果是什么?
- node.js - 远程 Lerna 构建失败 - 内部包的符号链接错误
- mysql - 从表更新到另一个 WHERE t1.substring=t2.col
- javascript - 在字典数组中附加数据类型(javascript)
- math - 如何将 10^4 转换为二进制
- node.js - 如何在电子 js 中从渲染器或预加载过程更改应用程序图标?
- python - Lomuto 算法验证测试用例
- flask - 我不断收到 AttributeError: 'str' object has no attribute 'request' with google APIs in flask
- c - C 有符号到有符号整数的转换如何工作?
- java - 打开输入流时找不到文件异常