ChatGPT解决这个技术问题 Extra ChatGPT

Changing the color of an hr element

I want to change the color of my hr tag using CSS. The code I've tried below doesn't seem to work:

hr {
    color: #123455;
}
Just FYI, I tried doing what you did with a different color and it works in Firefox 5 Beta, but not IE 9, jsfiddle.net/TGtSd
@Keoki Zee Not working for me (Chrome).
@Marty background-color works in Chrome, but you're right not color...weird...
chrome not working background-color too not working for me
Okay, just in case anyone wants to test, here's a fiddle I've got going so far, jsfiddle.net/TGtSd/9...

A
Anton Strogonoff

I think you should use border-color instead of color, if your intention is to change the color of the line produced by <hr> tag.

Although, it has been pointed in comments that, if you change the size of your line, border will still be as wide as you specified in styles, and line will be filled with the default color (which is not a desired effect most of the time). So it seems like in this case you would also need to specify background-color (as @Ibu suggested in his answer).

HTML 5 Boilerplate project in its default stylesheet specifies the following rule:

hr { display: block; height: 1px;
    border: 0; border-top: 1px solid #ccc;
    margin: 1em 0; padding: 0; }

An article titled “12 Little-Known CSS Facts”, published recently by SitePoint, mentions that <hr> can set its border-color to its parent's color if you specify hr { border-color: inherit }.


The problem with border-color is that if you make the hr larger, it just colors the border, jsfiddle.net/TGtSd/9...
@Anton that's not what I was referring to about the size of the hr...if you increase the height, and you only use border-color, you'll get a rectangle with a colored border, but the inside won't be colored...
Why even bother with the border at all? Why not just give it a background-color and be done with it? Edit: nvm, I didn't see the answer on browser compatibility.
@anton many thanks! I was really banging my head against the wall on this one :-O border-color: inherit; also worked well for me.
U
Uwe Keim

border-color works in Chrome and Safari.

background-color works in Firefox and Opera.

color works in IE7+.


If we wish our site to be seen on all browsers, should we use them all?
border-color doesn't work on Chrome. Try setting it to white on white background. These two will work: Either 1) color:white; border-style:solid; or 2) border-color:white; border-style:solid;.
@Pacerier yes it does. Might need to specify a style and/or width
the information re. IE (at least) is incorrect. 'border-color' works fine in IE; 'color' does not (IE11)
F
Flimm

I think this can be useful. this was simple CSS selector.

hr { background-color: red; height: 1px; border: 0; }



This definitely worked for me in chrome desktop:
Works for me in both chrome and firefox
A
Alexander Online Media
hr {
    height: 1px;
    color: #123455;
    background-color: #123455;
    border: none;
}

Doing it this way allows you to change the height if needed. Good luck. Source: How To Style HR with CSS


U
Uwe Keim

Tested in Firefox, Opera, Internet Explorer, Chrome and Safari.

hr {
    border-top: 1px solid red;
}

See the Fiddle.


A
Alexander van Oostenrijk

This will keep the Horizontal Rule 1px thick while also changing the color of it:

hr {
  height: 0; 
  border: 0; 
  border-top: 1px solid #083972; 
}

Best answer here. Setting the height to 0px and adding the border doe not make the hr 2 px wide. Great call !!
j
jefi

Only border-top with color is enough to make the line in different color.

hr { border-top: 1px solid #ccc; }



it has strange dots to the right and left of the line
A
Alexander van Oostenrijk
hr {
  color: #f00;
  background-color: #f00;
  height: 5px;
}

A
Alexander van Oostenrijk

I believe this is the most effective approach:

<hr style="border-top: 1px solid #ccc; background: transparent;">

Or if you prefer doing it on all hr elements write this on you CSS:

hr {
  background-color: transparent;
  border-top: 1px solid #ccc;
}

A
Alexander van Oostenrijk
hr {
  background-color: #123455;
}

The background is the one you should try to change.

You can also work with the borders color. I am not sure; I think there are cross-browser issues with this. You should test it in different browsers.


I tried it in Firefox 5 Beta, IE 9, Chrome, Opera, and Safari...you're good, they all work ;)
@kool, which part doesnt work? background-color? or border-color? i just tested border-color: blue; and it worked in chrome
[border-color] works in Chrome. [background-color] does not. I would vote down if I had enough rep.
It was a bit of a surprise for me, but border-color didn't work in my case, but background did, thanks!
A
Alexander van Oostenrijk

After reading all the answers here, and seeing the complexity described, I set upon a small diversion for experimenting with HR. And, the conclusion is that you can throw out most of the monkeypatched CSS you wrote, read this small primer and just use these two lines of pure CSS:

hr {
  border-style: solid;
  border-color: cornflowerblue; /* or whatever */
}

That is ALL you need to style your HRs.

Works cross-browser, cross-device, cross-os, cross-english-channel, cross-ages.

No "I think this will work...", "you need to keep Safari/IE in mind...", etc.

no extra css - no height, width, background-color, color, etc. involved.

Just bulletproof colourful HRs. It's that simpleTM.

Bonus: To give the HR some height H, just set the border-width as H/2.


I took a bet each way: ``` hr { border-top: 1px solid purple; border-color: purple; background-color: purple; color: purple; } ~~~
S
Sebastian Gomes

You can add bootstrap bg class like

<hr class="bg-light" />

Turns out this was the perfect answer for me, although I've done it in the past by adjusting the border. BTW, Bootstrap 4 does it this way: .bg-light { background-color: #f8f9fa !important; }
.bg-dark is another good option, for sites with a black or dark background.
d
deepi

if u use css class then it will be taken by all 'hr' tags , but if u want for a particular 'hr' use the below code i.e, inline css

<hr style="color:#99CC99" />

if it's not working in chrome try below code:

<hr color="red" />

A
Alexander van Oostenrijk

Some browsers use the color attribute and some use the background-color attribute. To be safe:

hr {
  color: #color;
  background-color: #color;
}

M
Mert

It's simple and my favorite.

<hr style="background-color: #dd3333" />

J
John Slegers

I'm testing on IE, Firefox and Chrome May 2015 and this works best with the current versions. It centers the HR and makes it 70% wide:

hr.light { width:70%; margin:0 auto; border:0px none white; border-top:1px solid lightgrey; }



this exacly what I was looking for. My mistake was I was adding a space between hr and class in css definition ( hr .light {} )
J
John Slegers

You should set border-width to 0; It works well in Firefox and Chrome.

hr { clear: both; color: red; background-color: red; height: 1px; border-width: 0; }


This is a test


2
2 revs

Since i don't have reputation to comment, i will give here a few ideas.

if you want a css variable height, take off all borders and give a background color.

    hr{
        height:2px;
        border:0px;
        background:green;
        margin:0px;/*sometimes useful*/
    }
    /*Doesn't work in ie7 and below and in Quirks Mode*/

if you want simply a style that you know that will work (example: to replace a border in a ::before element for most email clients or

    hr{
        height:0px;
        border:0px;
        border-top:2px solid blue;
        margin:0px;/*useful sometimes*/
    }

In both ways, if you set a width, it will always have it's size.

No need to set display:block; for this.

To be totally safe, you can mix both, 'cause some browsers can get confused with height:0px;:

    hr{
        height:1px;
        border:0px;
        background:blue;
        border-top:1px solid blue;
        margin:0px;/*useful sometimes*/
    }

With this method you can be sure that it will have at least 2px in height.

It's a line more, but safety is safety.

This is the method you should use to be compatible with almost everything.

Remember: Gmail only detects inline css and some email clients may not support backgrounds or borders. If one fails, you will still have a 1px line. Better than nothing.

In the worst cases, you can try to add color:blue;.

In the worst of the worst cases, you can try to use a <font color="blue"></font> tag and put your precious <hr/> tag inside it. It will inherit the <font></font> tag color.

With this method, you WILL want to do like this: <hr width="50" align="left"/>.

Example:

    <span>
        awhieugfrafgtgtfhjjygfjyjg
        <font color="#42B3E5"><hr width="50" align="left"/></font>
    </span>
    <!--Doesn't work in ie7 and below and in Quirks Mode-->

Here is a link for you to check: http://jsfiddle.net/sna2D/


u
user2689252

You can use CSS to make a line with a different color, example would be like that:

border-left: 1px solid rgb(216, 216, 216);
border-right: medium none;
border-width: medium medium medium 2px;
border-style: none none none solid;
border-color: -moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(216, 216, 216);

that code will display vertical grey line.


R
Rautermann

I like the answers setting border-top, but they are somehow still a little off in Chrome...
BUT if I set border-top: 1px solid black; and border-bottom: 0px; I end up with a truly single line (that also works fine with higher thickness).


M
Murtaza

Well, I am new in HTML, CSS and in Java but I tried my way which worked for me in all browsers. I have used JS instead of CSS which doesn't work with some browsers.

First of all I have given id="myHR" to HR element and used it in Java Script.
Here is the Code.

x = document.getElementById("myHR");
y = x.style.width = "600px";
y = x.style.color = "white";
y = x.style.height = "2px";
y = x.style.border = "none";
y = x.style.backgroundColor = "lightgrey";

S
Shivpe_R

Code Works For older IE Tried For Many Colors




L
Larry

Using font colours to modify horizontal rules makes them more flexible and easy to use.

The color property isn't inherited by default, so the following needs to be added to hr's to allow color inheritance:

/* allow hr to inherit color */ hr { border: 1px solid;} /* reusable colour modifier */ .fc_-alpha { color: crimson;} normal hr:


hr with colour modifier:


J
John Slegers

You could do this :

hr { border: 1px solid red; }


This s a test


J
John Slegers

You can give the <hr noshade> tag and go to your css file and add :

hr { border-top:0; color: #123455; }


This s a test


A
Aubrey Love

As a general rule, you can’t just set the color of a horizontal line with CSS like you would anything else. First of all, Internet Explorer needs the color in your CSS to read like this:

“color: #123455”

But Opera and Mozilla needs the color in your CSS to read like this:

“background-color: #123455”

So, you will need to add both options to your CSS.

Next, you will need to give the horizontal line some dimensions or it will default to the standard height, width and color set by your browser. Here is a sample code of what your CSS should look like to get the blue horizontal line.

hr {
border: 0;
width: 100%;
color: #123455;
background-color: #123455;
height: 5px;
}

Or you could just add the style to your HTML page directly when you insert a horizontal line, like this:

<hr style="background:#123455" />

Hope this helps.


A
Alexander van Oostenrijk

I took a bet each way:

hr {
  border-top: 1px solid purple;
  border-color: purple;
  background-color: purple;
  color: purple;
}