0

I am trying to achieve a horizontal line behind a heading as shown below.

enter image description here

I can do that fairly easily with a div around the heading. Something like:

  .holder{
    height:4px;
    background:red;
    width:100%;
    text-align:center;
  }
  h4{
    display:inline-block;
    text-align:center;
    background:white;
    line-height:50px;
    padding: 0 20px;
    margin-top:-23px;
    font-size:30px;
  }
<div class="holder">
  <h4>
    TESTING
  </h4>
</div>

But I would like to be able to do the same without adding the exterior div. The closest I have got is:

  h3{
    text-align:center;
    line-height:50px;
    position:relative;
    font-size:30px;
  }
  
  h3::before{
    display:block;
    background:red;
    width:100%;
    height:4px;
    top:50%;
    margin-top:-3px;
    position:absolute;
    content:'';
  }
<h3>
  TESTING
</h3>

Which results in the following:

enter image description here

Can anyone suggest how I might do this without adding the div?

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
niccol
  • 129
  • 4

1 Answers1

0

You could achieve what you want as below, using ::before, ::after, and CSS Grid Layout:

h3 {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  font-size: 30px;
}

h3::before,
h3::after {
  background: red;
  height: 4px;
  content: "";
}
<h3>
  TESTING
</h3>
Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65