如何用css画一条线并在上面显示文字或图像

时间:2015-01-11 18:05:11

标签: html css css3

我正在尝试使用CSS绘制一条线并在该线的中间显示文本/图像。

.featured-images {
  color: #666666;
  border: 2px solid #333333;
}
<p class="featured-images">Featured</p>

这就是我想要做的事情:

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:6)

将文本换行到span并使用:伪元素作为行。

使用span将行(:伪元素)放在z-index: -1后面,这样您就可以在文本中移动而不必担心该行。

&#13;
&#13;
.featured-images {
  position: relative;
  color: #666666;
  border: 2px solid #333333;
  padding: 0 10px 0 30px;
}
.featured-images span {
  color: #666666;
  background: white;
  padding: 0 10px;
}
.featured-images:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background: #666666;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: -1;
}
&#13;
<p class="featured-images"><span>Featured</span></p>
&#13;
&#13;
&#13;


复制以下内容:

enter image description here

enter image description here

enter image description here

您可以使用repeating-linear-gradient执行此操作。

&#13;
&#13;
body {
  background: #E7EAE3;
}
.featured-images {
  position: relative;
  color: #666666;
  padding: 0 10px 0 30px;
  overflow: hidden;
}
.featured-images span {
  color: #517575;
  background: white;
  padding: 0 10px;
}
.featured-images:after {
  content: '';
  position: absolute;
  width: 120%;
  height: 100%;
  background: -webkit-repeating-linear-gradient(180deg, #463A3A 10px, #F2F2F2 10px, #F2F2F2 11px, #463A3A 11px, #463A3A 20px) repeat-x;
  background: -moz-repeating-linear-gradient(180deg, #463A3A 10px, #F2F2F2 10px, #F2F2F2 11px, #463A3A 11px, #463A3A 20px) repeat-x;
  background-size: 10px 31px;
  margin-left: -30px;
  transform: skew(-45deg);
  left: 0;
  z-index: -1;
}
&#13;
<p class="featured-images"><span>Featured</span>
</p>
&#13;
&#13;
&#13;


使用图片而不是文字。

&#13;
&#13;
.featured-images {
  position: relative;
  color: #666666;
  border: 2px solid #333333;
  padding: 0 10px 0 30px;
}
.featured-images span {
  display: block;
  width: 80px;
  height: 13px;
  background: url(http://lorempixel.com/80/13) no-repeat white 10px 0;
  padding: 0 10px;
}
.featured-images:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background: #666666;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: -1;
}
&#13;
<p class="featured-images"><span></span></p>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

这样的东西? JSFiddle

CSS:

.featured-images {
    color: #666666;
}
p span {
    margin:0;padding: 0 10px;
    background: #FFFFFF;
    display: inline-block;
}
p {
    padding-left: 20px;
    position: relative;
    z-index: 2;   
}
p:after {
    content: "";
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    border-top: solid 1px #666666;
    z-index: -1;
}

答案 2 :(得分:2)

演示1

enter image description here

&#13;
&#13;
:root{padding: 40px}
p{

    position: relative;
    margin:40px auto;padding:0 10px;
    background:white;
    display:inline-block;
}
p:before,p:after{
    content:"";
    position:absolute;
    z-index:-1;
    right:0
}
p:before{
    top:-4px;
    left: -24px;
    height: 24px;
    width: 480px;
    border:solid 1px #666666
}
p:after{
    top: 50%;
    width: 466px;
    left: -16px;
    border-top: solid 1px #666666
}
&#13;
<p class="featured-images">Featured</p>
&#13;
&#13;
&#13;

使用伪元素

&#13;
&#13;
:root{padding: 40px}
p{

    position: relative;
    margin:40px auto;padding:0 10px;
    background:white;
    display:inline-block;
}
p:before,p:after{
    content:"";
    position:absolute;
    z-index:-1;
    left:-14px;
    right:0;
    width: 480px
}
p:before{
    top:-4px;
    height: 24px;
    border:solid 1px #666666
}
p:after{
    top:50%;
    border-top:solid 1px #666666
}
&#13;
<p class="featured-images">Featured</p>
&#13;
&#13;
&#13;

演示2

&#13;
&#13;
p{

    position: relative;
    margin:0;padding:0 10px;
    background:white;
    display:inline-block;
}

p:after{
    content:"";
    position:absolute;
    top:50%;
    left:-14px;
    right:0;
    width: 100vw;
    border-top:solid 1px #666666;
    z-index:-1;
}
&#13;
<p class="featured-images">Featured</p>
&#13;
&#13;
&#13;

<强>更新

enter image description here

&#13;
&#13;
:root{padding: 40px}
p{

    position: relative;
    margin:40px auto;padding:0 10px;
    background:white;
    display:inline-block;
}
p:before,p:after{
    content:"";
    position:absolute;
    z-index:-1;
    right:0
}
p:before{
    top:-4px;
    left: -24px;
    height: 24px;
    width: 480px;
    border:solid 1px #666666;
    background-color: gray;
    background-image: repeating-linear-gradient(-45deg, transparent, transparent 2px, rgba(255,255,255,.5) 2px, rgba(255,255,255,.5) 6px)
}
}
p:after{
    top: 50%;
    width: 466px;
    left: -16px;
    border-top: solid 1px #666666
}
&#13;
<p class="featured-images">Featured</p>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

结果http://jsfiddle.net/p8jdzqvL/embedded/result/

 <div style="height: 2px; background-color: black; text-align: center">
      <span style="background-color: white; position: relative; top: -0.5em;">
        Stay Connected
      </span>
    </div>