因此,我正在设计这个水平线,中间的图像和文字的想法,并卡住了。我如何在“TEXT”左侧对齐图像而不在其下方?这是演示当前状态的链接:
http://codepen.io/anon/pen/MJWJad
感谢所有帮助。
.horizontal__rule--modified {
line-height: 1em;
position: relative;
border: 0;
color: #666666;
text-align: center;
height: 1.5em;
opacity: 0.7;
letter-spacing: 1px;
font-size: 16px;
&:before {
content: url(http://www.metalguitarist.org/forum/images/mgtwitter.png);
background: red;
position: absolute;
left: 0;
top: 50%;
width: 100%;
height: 2px;
}
&:after {
content: attr(data-content);
position: relative;
display: inline-block;
color: black;
padding: 0 .5em;
line-height: 1.5em;
color: red;
background-color: #fcfcfa;
}
}
<hr class="horizontal__rule--modified" data-content="TEXT">
答案 0 :(得分:5)
实际上你根本不需要<hr />
。您可以使用伪元素并使其成为可能:
* {
font-family: 'Segoe UI';
font-weight: normal;
}
h1 {
text-align: center;
}
h1 span {
background-color: #fff;
padding: 15px;
}
h1::after {
display: block;
content: "";
border: 1px solid #ccc;
margin-top: -0.5em;
}
<h1><span>Hello</span></h1>
如果需要图像,例如a twitter icon,则可以使用:Source:
* {
font-family: 'Segoe UI';
font-weight: normal;
vertical-align: middle;
}
h1 {
text-align: center;
font-size: 14pt;
}
h1 span {
background-color: #fff;
padding: 15px;
}
h1::after {
display: block;
content: "";
border: 1px solid #ccc;
margin-top: -0.5em;
}
<h1>
<span>
<img src="http://www.metalguitarist.org/forum/images/mgtwitter.png" alt="" />
Hello
</span>
</h1>
预览强>
答案 1 :(得分:2)
除Praveen之外的另一个解决方案是使用flex-box。
<强> HTML 强>
<div class="wrapper">
<div class="line"></div>
<div class="text">
Test
</div>
<div class="line"></div>
</div>
<强> CSS 强>
.wrapper {
display: flex;
justify-content: space-between;
align-items: center;
}
.line {
height: 10px;
background: black;
width: 100%;
}
.text {
padding: 0 10px;
}