You can consider the pseudo element relative to h2
instead and rely on overflow to hide the non needed parts:
.section-header {
position: relative;
overflow:hidden; /*mandatory*/
}
.section-header h2 {
border: 2px solid black;
padding: 0.3em 0.8em;
display: inline-block;
position:relative;
}
.section-header h2::before,
.section-header h2::after {
content: " ";
width: 100vw; /*big value here*/
height: 1px;
position: absolute;
top: 0.6em;
background:black;
}
.section-header h2::after {
left: calc(100% + 40px); /*40px offset from the title*/
}
.section-header h2::before {
right: calc(100% + 40px);
}
.text-center {
text-align: center;
}
<div class="section-header text-center">
<h2>Testing</h2>
</div>
Another idea without transparency is to consider background and box-shadow like below:
.section-header {
position: relative;
background:linear-gradient(black,black) center/100% 1px no-repeat;
}
.section-header h2 {
border: 2px solid black;
padding: 0.3em 0.8em;
display: inline-block;
position:relative;
background:#fff;
box-shadow:
40px 0 0 #fff,
-40px 0 0 #fff;
}
.text-center {
text-align: center;
}
<div class="section-header text-center">
<h2>Testing</h2>
</div>