以下是一种不使用伪元素的方法:
h1 {
display: inline-block;
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
background:linear-gradient(to right, #ccc 50%, maroon 50%) bottom,
linear-gradient(to right, maroon 50%, #ccc 50%) top;
background-repeat: no-repeat;
background-size:100% 2px;
}
header {
text-align: center;
background-color: #7f7f7f;
}
h1 {
display: inline-block;
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
background:linear-gradient(to right, #ccc 50%, maroon 50%) bottom,
linear-gradient(to right, maroon 50%, #ccc 50%) top;
background-repeat: no-repeat;
background-size:100% 3px;
}
<header>
<h1>HEADER</h1>
</header>
header {
text-align: center;
background-color: #7f7f7f;
--color1: maroon;
--color2: #ccc;
}
h1 {
position: relative;
display: inline-block;
font-size: 30px;
color: var(--color1);
background: linear-gradient(to right, var(--color1) 50%, var(--color2) 50%) bottom, linear-gradient(to right, var(--color2) 50%, var(--color1) 50%) top;
background-repeat: no-repeat;
background-size: 100% 2px;
padding: 5px 0;
white-space: nowrap;
}
h1:before {
content: attr(data-text);
overflow: hidden;
position: absolute;
left: 0;
top: 5px;
width: 50%;
color: var(--color2);
}
<header>
<h1 data-text="HEADER">HEADER</h1>
</header>
<hr>
<header>
<h1 data-text="Some text here">Some text here</h1>
</header>
::before
和::after
在h1
标签上,结合使用linear-gradient
作为background
,使用height
而非border
来实现这种样式。
header {
text-align: center;
background-color: #7f7f7f;
}
h1{
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
display: inline-block;
position:relative;
}
h1:before{
content:"";
width:100%;
height:3px;
left:0;
top:0;
position:absolute;
z-index:9;
background:linear-gradient(to right, white 50%, brown 50%);
}
h1:after{
content:"";
width:100%;
height:3px;
left:0;
bottom:0;
position:absolute;
z-index:9;
background:linear-gradient(to right, brown 50%, white 50%);
}
<header>
<h1>HEADER</h1>
</header>
#880015
和 #fff
background-size: 100% 3px
header {
text-align: center;
background-color: #7f7f7f;
}
h1 {
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
display: inline-block;
background-image:
linear-gradient(90deg, #880015 50%, #fff 50%),
linear-gradient(-90deg, #880015 50%, #fff 50%);
background-size: 100% 3px;
background-position:
top left,
bottom left;
background-repeat: no-repeat;
}
<header>
<h1>HEADER</h1>
</header>
:before
和 :after
伪元素上,通过绝对定位来实现。linear-gradient(to right, #fff, #fff 50%, rgba(0,0,0,0) 50%, rgba(0,0,0,0))
header {
text-align: center;
background-color: #7f7f7f;
}
h1 {
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
display: inline-block;
border-top: 3px solid #880015;
border-bottom: 3px solid #880015;
position: relative;
width: 199px;
}
h1::before {
display: block;
content: "";
position: absolute;
top: -3px;
left: 0px;
width: 5em;
background: linear-gradient(to right, #fff, #fff 50%, rgba(0,0,0,0) 50%, rgba(0,0,0,0));
height: 3px;
}
h1::after {
display: block;
content: "";
position: absolute;
bottom: -3px;
left: 0px;
width: 5em;
background: linear-gradient(to right, rgba(0,0,0,0), rgba(0,0,0,0) 50%, #fff 50%, #fff);
height: 3px;
}
<header>
<h1>HEADER</h1>
</header>
你可以使用Position
和伪元素来实现它。
header {
text-align: center;
background-color: #7f7f7f;
position: relative;
}
h1 {
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
display: inline-block;
border-top: 3px solid #880015;
border-bottom: 3px solid #880015;
position: relative;
}
h1:after,
h1:before {
content: '';
height: 3px;
width: 50%;
background: #fff;
position: absolute;
}
h1:after {
top: -3px;
left: 0;
}
h1:before {
bottom: -3px;
right: 0;
}
<header>
<h1>HEADER</h1>
</header>
在正确的位置添加带有边框的before
和after
元素即可解决问题。
header {
text-align: center;
background-color: #7f7f7f;
}
h1 {
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
display: inline-block;
border-top: 3px solid #880015;
border-bottom: 3px solid #880015;
position: relative;
}
h1:before {
border-top: 3px solid lightgray;
display: block;
position: absolute;
content: '';
left: 0;
right: 50%;
bottom: -3px;
width: 50%;
}
h1:after{
border-top: 3px solid lightgray;
display: block;
position: absolute;
content: '';
left: 50%;
right: 0;
top: -3px;
width: 50%;
}
<header>
<h1>HEADER</h1>
</header>
我认为这就是你想要的。
header {
text-align: center;
background-color: #7f7f7f;
}
h1 {
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
display: inline-block;
position:relative;
}
h1:before, h1:after {
background: linear-gradient(to right, rgb(136, 0, 21) 0%, rgb(136, 0, 21) 50%, rgb(195, 195, 195) 50%, rgb(195, 195, 195) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f85032', endColorstr='#e73827', GradientType=1 );
position: absolute;
width: 100%;
height: 2px;
display: inline-block;
content: "";
}
h1:before {
top: 0;
left: 0;
}
h1:after {
bottom: 0;
left: 0;
transform: rotate(180deg);
}
<header>
<h1>HEADER</h1>
</header>
header {
text-align: center;
background-color: #7f7f7f;
}
h1 {
color: #00a2e8;
font-size: 40px;
padding: 5px 0;
display: inline-block;
}
h1:after {
content: "";
width: 50%;
display: block;
border-bottom: 3px solid #880015;
}
h1 span:after {
content: "";
width: 50%;
display: block;
border-bottom: 3px solid #fff;
float:right;
}
h1:before {
content: "";
width: 50%;
display: block;
border-bottom: 3px solid #fff;
}
h1 span:before {
content: "";
width: 50%;
display: block;
border-bottom: 3px solid #880015;
float:right;
margin-top: -3px;
}
<header>
<h1><span>HEADER</span></h1>
</header>
这里你去...
z-index
吗? - RaJesh RiJo