如何设置<hr>标签的背景颜色?

问题描述 投票:0回答:4

为什么

hr
标签元素没有按照下面的代码设置为绿色?

hr {
  background-color: #00ff00;
}
<hr>

html css
4个回答
4
投票

背景颜色只是为 hr 设置背景,通过应用内联样式我们可以使它更容易。 https://jsbin.com/wuvolojuzu/

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <p>Hello</p>
  <hr color="red" width="100%" />
  <p>Hi</p>
</body>
</html>


3
投票

您提供的代码正确设置背景颜色。但是,

<hr>
标签没有可见的背景,因此它看起来不会执行任何操作。您可能想要的是更改其颜色

hr {
   color: #00ff00;
}

这将使

<hr>
线变为绿色。

如果没有,可能还有另一个具有更多特异性的元素。在这种情况下,您有多种选择:

1) 为

hr
选择器添加更多特异性。

2) 设置颜色为

!important
:

hr {
   color: #00ff00 !important;
}

3)内联设置背景颜色:

<hr color="#00ff00" />

希望这有帮助!


1
投票

通常您不能为

background-color
标签设置
hr
。这是一个空元素。它必须有开始标记,但不能有结束标记。
hr
仅接受
color
属性。例如:

<hr color="#00ff00" />

默认

hr
值:

margin: 7px or 8px /*depend on box-modeling. */
border: 1px inset #000;
display: block;
/* No height */

标准使用:

margin-top: 10px; /* You can change margin height */
margin-bottom: 10px; /* You can change margin height */
border: 0; /* or border-top: 5px solid #00ff00 */
border-width: 5px 0 0 0;
border-style: solid;
border-color: #00ff00;

hr{
  margin-top: 15px;
  margin-bottom: 15px;
  border: 0;
  border-top: 5px solid #00ff00;
}
<html>
  <body>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <hr />
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </body>
</html>

我希望这会有所帮助。


0
投票

尝试编写边框:1pxsolid#00ff00;这将具有与颜色相同的效果。

© www.soinside.com 2019 - 2024. All rights reserved.