Reputation: 317
I'm constructing a site and a piece of the mockup is below
Since I'm using a content management system that builds the HTML, all I have to work with a single h3
tag. I want the line behind to have the width of the div
containing the h3
tag. Is this possible?
Here's the closest that I can get: http://jsfiddle.net/rmgtq6h6/
h3.line-behind { width: auto; position: relative; text-align: center}
h3.line-behind:after { content: " "; border-top: 3px solid black; position: absolute; width:100%; top: 50%; left: 0; z-index: 1; }
Upvotes: 5
Views: 4087
Reputation: 106
Simple answer using grid:
h1 {
display: grid;
grid-template-columns: 1fr max-content 1fr;
align-items: center;
gap: 0.5rem;
}
h1:before,
h1:after {
content: '';
height: 1px;
background-color: #CBD5E1;
}
https://jsfiddle.net/6cq5whgr/
Upvotes: 3
Reputation: 27465
We can build is easily with these guaranties 👇
h1:before {
content:"";
display: block;
border-top: solid 1px black;
width: 100%;
height: 1px;
position: absolute;
top: 50%;
z-index: 1;
}
h1 span {
background: #fff;
padding: 0 20px;
position: relative;
z-index: 5;
}
Demo: http://jsfiddle.net/7s79zwz5/
h1+p {
border-top: solid 1px black;
padding-top: 40px;
margin-top: -40px;
}
Demo: http://jsfiddle.net/v9g3d4ua/
h1 {
background: -moz-linear-gradient(#ffffff 0%, #ffffff 49%, #000000 50%, #000000 51%, #ffffff 52%, #ffffff 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffffff), color-stop(49%, #ffffff), color-stop(50%, #000000), color-stop(51%, #000000), color-stop(52%, #ffffff), color-stop(100%, #ffffff));
background: -webkit-linear-gradient(#ffffff 0%, #ffffff 49%, #000000 50%, #000000 51%, #ffffff 52%, #ffffff 100%);
background: -o-linear-gradient(#ffffff 0%, #ffffff 49%, #000000 50%, #000000 51%, #ffffff 52%, #ffffff 100%);
background: linear-gradient(#ffffff 0%, #ffffff 49%, #000000 50%, #000000 51%, #ffffff 52%, #ffffff 100%);
}
Demo: http://jsfiddle.net/2m30vsgm/
Upvotes: 0
Reputation: 83
if i understand correctly this is what are you looking for
h3.line-behind:after {
position:absolute;
content:'';
height:2px;
width:150%;
top:50%;
transform:translateY(-50%);
left:50%;
transform:translateX(-50%);
z-index:-1;
background: linear-gradient(to right, rgba(25,25,25,0) 0%,
rgba(25,25,25,1) 35%,
rgba(255,255,255,0) 36%,
rgba(255,255,255,0) 65%,
rgba(25,25,25,1) 66%,
rgba(25,25,25,0) 100%);}
or another example more practical add another pseudo-element ::before
h3.line-behind:after { position:absolute;
content:'';
height:2px;
width:50%;
top:50%;
transform:translateY(-50%);
left:0;
transform:translateX(-50%);
z-index:-1;
background: #000; }
h3.line-behind:before {position:absolute;
content:'';
height:2px;
width:50%;
top:50%;
transform:translateY(-50%);
right:0;
transform:translateX(50%);
z-index:-1;
background: #000; }
Upvotes: 0
Reputation: 3501
Here you go:
https://jsfiddle.net/rmgtq6h6/1/
div.line-behind {
width: auto;
position: relative;
text-align: center
}
span {
content: " ";
border-top: 3px solid black;
position: absolute;
width: 100%;
top: 50%;
left: 0;
z-index: -1;
}
h3 {
background-color: #fff;
display: inline-block;
}
}
<div class="line-behind"><span></span>
<h3>Begin My Giving Journey</h3>
</div>
Or take a look here:
http://codepen.io/ericrasch/pen/Irlpm
Upvotes: 3
Reputation: 18995
Place you text in some container
center this text
apply some width to the container
give the container some background color
h3.line-behind { width: auto; position: relative; text-align: center}
h3.line-behind:after { content: " "; border-top: 3px solid black; position: absolute; width:100%; top: 50%; left: 0; z-index: -1; }
#container{
margin:0 auto;
background-color:white;
width:40%;
}
<h3 class="line-behind"><div id="container">Begin My Giving Journey</div></h3>
Upvotes: 0