创建一个中间带文本的水平规则?

时间:2013-08-29 23:10:07

标签: html css

我想在我的页面上有一个看起来像这样的分隔符:

My "Mock Up"

最好的方法是什么?

5 个答案:

答案 0 :(得分:2)

HTML

<h3><span>My latest work</span></h3>

css

h3 {
position:relative;
text-align:center;}

h3 span {
    display:inline-block;
    padding:0 10px;
    background:#fff;
}
h3:before {
    content:"";
    display:block;
    position:absolute;
    z-index:-1;
    left:0;
    right:0;
    top:50%;
    height:1px;
    background:#ccc;
}

答案 1 :(得分:1)

演示:http://jsfiddle.net/5tqE5/1/

这使用旧版浏览器不支持的attr()。它可以用额外的元素替换。

<div class="lines" data-text="Some Text Goes Here"></div>

.lines {
    position: relative;
    font-size: 20px;
    font-family: sans-serif;
    margin: 0 auto;
    border-top: 1px solid silver;
    margin-top: 20px;
}

.lines:before{
    content: attr(data-text);
    background-color: #fff;
    position: absolute;
    text-align: center;
    left: 50%;
    width: 220px;
    margin-left: -110px;
    padding: 10px;
    top: -20px;
}

答案 2 :(得分:1)

我们可以在没有图像或遮蔽线like so的情况下执行此操作:

HTML

<div class="rule">
    <div class="line"><div></div></div>
    <div class="words">words are cool</div>
    <div class="line"><div></div></div>
</div>

CSS

.rule {
    display: table;
}

.rule>div {
    display: table-cell;
    white-space:nowrap;
}

.line>div {
    border-bottom: 1px solid silver;
    height: 1px;
}

.words {
    padding: 0 5px;
}

.line {
    width: 50%;
    vertical-align: middle;
}

答案 3 :(得分:0)

Dunno关于“最好的” - 你没有给出评估这个的条款。最小,最快,最兼容等等。

Anyhoo,我刚刚拍了1张像素宽的图片并保存了。然后我用它作为div的背景图像。

<强> CSS:

#myDiv
{
    background: url(horizline1x41px.png) repeat;
    text-align: center;
    line-height: 41px;
}
#myDiv span
{
    padding-left: 16px;
    padding-right: 16px;
    background: white;
    font-weight: bold;
    font-size: 1.5em;
}

<强> HTML:

<div id='myDiv'><span>OUR LATEST WORK</span></div>

答案 4 :(得分:0)

演示:http://jsfiddle.net/8zve4/

我不喜欢额外的标记,但这应该有用。

<强> CSS:

.hline {
  border: 1px solid #EEE;
  color: #666;
  font-family: helvetica;
  font-weight: bold;
  font-variant: small-caps;
  letter-spacing: .1em;
  line-height: 0px;
  text-align: center;
  text-transform: uppercase;
}

.hline > span {
  background-color: #FFF;
    padding: 0px 1em;
}

<强> HTML:

<div class="hline"><span>Our latest work</span></div>
相关问题