【发布时间】:2016-12-06 07:10:45
【问题描述】:
我在 css 中为我的 html 元素添加了一个背景图像,以便它随着网络浏览器的大小而扩展。我将此 html 元素中的不透明度更改为 0.5。我想将子元素(特别是 h1 和段落元素)改回不透明度为 1,因此文本不透明。它不起作用。请帮忙:)
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link rel = "stylesheet" href = "style.css">
</head>
<body>
<p id ="topBar"></p>
<h1>Heading</h1>
<h3>Name</h3>
<p>
Paragraph
</p>
<h3>Heading</h3>
<p>More text</p>
<h3>Send us an email!</h3>
<form>
<input style ="width:200px" type="email" placeholder ="Email" name="email"><br><br>
<textarea style ="height:100px;width:200px"placeholder = "Message" name="message"></textarea><br><br>
<input type="submit" value ="Send" name="send">
</form>
<p id ="bottomBar"></p>
</body>
</html>
CSS:
html {
background: url(pen.jpg) no-repeat center fixed;
background-size: cover;
font-family: Garamond;
text-align: center;
opacity: 0.5;
}
h1 {
opacity: 1;
}
【问题讨论】:
-
@LaurIvan 不。 h1 规则比 html 规则更具体,因此适用。
-
整个 html 文档都有一个 opacity 指令,它会影响所有内容,而不管具体性或优先级。 @LaurIvan 添加 !important 什么都不做。您只需要在特定元素上设置较低的不透明度。
-
@SLowLoris 但是怎么做? h1 嵌套在 html 中,因此根据 css 优先级,h1 的规则应该覆盖 html 的规则
-
@geeksal opacity 不能被覆盖。如果您有一个不透明度为 0.5 的元素和该元素中的一个不透明度为 0.5 的子元素,则该子元素的不透明度将为 0.25。它已经降低了其父级的不透明度,并进一步降低了其自身的指令。很简单
-
@SLowLoris 谢谢我不知道