Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a background color surrounding an <hr> element but not adjacent text

Tags:

html

css

I am trying to add color surrounding an <hr> element but not the surrounding lines, something like this:

A horizontal line HTML element between two words, the horizontal line has been highlighted yellow in a separate notes app

enter image description here

I tried a div with a background-color set like so:

test
<div style="background-color: yellow;">
  <hr>
</div>
test

But it does not work.

Trying this instead:

<div style="background-color: yellow;">
  <hr>
  test
</div>

I noticed that the <hr>’s background can only really be styled by the surrounding text.

Is there a good way around this?

like image 432
Linux in a Bit Avatar asked Oct 26 '25 05:10

Linux in a Bit


2 Answers

The default <hr> element is a single inset border, without height, so it can't display background. You can use box-shadow instead:

.divider {
  box-shadow: 0 0 0 0.5rem yellow;
}
test
<hr class="divider">
test
like image 179
Ori Drori Avatar answered Oct 27 '25 18:10

Ori Drori


You can use padding on the div element to make the div expand some so that the background is visible. In your current example, the div is the same dimensions as the hr, so the hr occludes the background. And also an hr has no height.

test
<div style="background-color: yellow; padding: 0.25em 0.5em;">
  <hr>
</div>
test
like image 44
Sean Avatar answered Oct 27 '25 18:10

Sean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!