.banner-bottom h2 {
font-size: 2em;
color: #372568;
position: relative;
padding-bottom: 1em;
text-align: center;
}
.banner-bottom h2:before {
content: '';
position: absolute;
top: 25%;
left: 25%;
background: #372568;
height: 2px;
width: 8%;
}
.banner-bottom h2:after {
content: '';
position: absolute;
top: 25%;
right: 25%;
background: #372568;
height: 2px;
width: 8%;
}Run Code Online (Sandbox Code Playgroud)
<div class="banner-bottom">
<div class="container">
<h2>Special Promo</h2>
<h2>Promo</h2>
<div>
<div>Run Code Online (Sandbox Code Playgroud)
结果:
如何根据编写示例的长度css其行如下:
试试这个:
.banner-bottom h2 {
font-size: 2em;
color: #372568;
padding-bottom: 1em;
text-align: center;
}
.line:before {
content: '';
display: inline-block;
background: #372568;
height: 2px;
width: 8%;
margin: 10px;
}
.line:after {
content: '';
display: inline-block;
background: #372568;
height: 2px;
width: 8%;
margin: 10px;
}
.promo {
display: inline-block;
}Run Code Online (Sandbox Code Playgroud)
<div class="banner-bottom">
<div class="container">
<h2><span class="line"><span class="promo">Special Promo</span></span></h2>
<h2><span class="line"><span class="promo">Promo</span></span></h2>
<div>
<div>Run Code Online (Sandbox Code Playgroud)
设置display属性值inline-block是很好的,因为@Vinothin已经为您解答了.这是你可以使用的另一个技巧.
使用transform属性将帮助您使用绝对位置.
.banner-bottom h2 {
font-size: 2em;
color: #372568;
position: relative;
padding-bottom: 1em;
text-align: center;
}
.banner-bottom h2:before {
content: '';
position: absolute;
top: 25%;
transform: translateX(-100%);
/* no left value is required */
background: #372568;
height: 2px;
width: 8%;
}
.banner-bottom h2:after {
content: '';
position: absolute;
top: 25%;
/* no right value is required */
background: #372568;
height: 2px;
width: 8%;
}Run Code Online (Sandbox Code Playgroud)
<div class="banner-bottom">
<div class="container">
<h2>Special Promo</h2>
<h2>Promo</h2>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
如果您需要在单词前后间隔,则通过增加或减少它来使用translate值.
使用transform房产将在未来帮助你很多.假设您有一个居中的列表,并且您需要将图标左中心放置到列表中,然后transform只采用相应的方式.我已经告知了这个技巧,因为我之前也遇到过类似的问题.
希望这可以帮助!