Reputation: 341
How can I draw a horizontal line (hr) in a react component using dynamic color?
Here's what I have so far:
render {
let color = 'red';
return (
<div>
There is a red HR
<hr />
<div>
)
}
Upvotes: 31
Views: 162277
Reputation: 3996
Below is the code for a simple Horizontal line styled with CSS.
.separator {
height: 10px;
background-color: red;
border: none;
}
<hr class="separator" />
Key points I noticed after working with the <hr />
tag
color
from styles has no effect on <hr />
. Use background-color
to change hr
color with styles.background-color
works only if height
is mentionedborder: none
is recommended as hr comes with a default border of 1px
function Separator({ color = "black", height = 1 }) {
return (
<hr
style={{
backgroundColor: color,
height: height,
border: "none"
}}
/>
);
}
<Separator color="red" height="10px" />
Key points
color
to black
and height
to 1
. These are the default <hr />
styles excluding border and if not defaulted you won't see a horizontal line if one of color, height
is missingUpvotes: 1
Reputation: 51
Here is a simple way to create a horizontal line in your react js app
<div style={{ borderTop: "2px solid #fff ", marginLeft: 20, marginRight: 20 }}></div>
Upvotes: 5
Reputation: 1633
<hr style={{
color: '#000000',
backgroundColor: '#000000',
height: .5,
borderColor : '#000000'
}}/>
Only adding the borderColor
, to change the exact full-color change of <hr />
tag
Upvotes: 9
Reputation: 8580
One way to set up the component:
const ColoredLine = ({ color }) => (
<hr
style={{
color: color,
backgroundColor: color,
height: 5
}}
/>
);
And then use it with:
<ColoredLine color="red" />
For a full breakdown on how to style <hr />
, see http://www.sovavsiti.cz/css/hr.html
Upvotes: 59