两侧带有水平线

Tom*_*ver 38 html css pseudo-class

我正在研究一些CSS,其中设计要求页面标题(标题)以水平线为中心,水平线在两侧垂直居中.此外,页面上有背景图像,因此标题的背景需要是透明的.

我已经集中了标题,我可以使用伪类来创建该行.但是当我越过标题的文本时,我需要该行消失.

我考虑过使用一个透明的背景渐变,但由于每个标题的长度不同,我不知道在哪里放置停止.

这是迄今为止的CSS:

h1 {  
    text-align: center;  
    position: relative;  
    font-size: 30px;  
    z-index: 1;  
}  

h1:after {  
    content: '';  
    background-color: red;  
    height: 1px;  
    display: block;  
    position: absolute;  
    top: 18px;  
    left: 0;  
    width: 100%;  
}  
Run Code Online (Sandbox Code Playgroud)

这是我所在的地方:http: //jsfiddle.net/XWVxk/1/

这可以用CSS完成而不添加任何额外的HTML吗?

Rom*_*rin 69

看看这个http://blog.goetter.fr/post/36084887039/tes-pas-cap-premiere-edition,这是你的答案.

这是您修改的原始代码

h1 {
    position: relative;
    font-size: 30px;
    z-index: 1;
    overflow: hidden;
    text-align: center;
}
h1:before, h1:after {
    position: absolute;
    top: 51%;
    overflow: hidden;
    width: 50%;
    height: 1px;
    content: '\a0';
    background-color: red;
}
h1:before {
    margin-left: -50%;
    text-align: right;
}
.color {
    background-color: #ccc;
}
Run Code Online (Sandbox Code Playgroud)
<h1>This is my Title</h1>
<h1>Another Similar Title</h1>
<div class="color"><h1>Just Title</h1></div>
Run Code Online (Sandbox Code Playgroud)

注意:该文章不再在线,这是最后一个很好的存档版本:http://web.archive.org/web/20140213165403/http: //blog.goetter.fr/post/36084887039/tes-pas-cap -premiere版

  • 有没有办法在单词的开头/结尾和行的结尾/开头之间放置一个"边距"? (2认同)

ele*_*eau 36

几天前需要这个,但接受的答案不适用于IE.

这就是我提出的:适用于所有主流浏览器(> = ie8)

jsfiddle:http://jsfiddle.net/gKve7/

HTML:

<h2 class="decorated"><span>My Title</span></h2>
Run Code Online (Sandbox Code Playgroud)

CSS:

/* headlines with lines */
.decorated{
     overflow: hidden;
     text-align: center;
 }
.decorated > span{
    position: relative;
    display: inline-block;
}
.decorated > span:before, .decorated > span:after{
    content: '';
    position: absolute;
    top: 50%;
    border-bottom: 2px solid;
    width: 592px; /* half of limiter */
    margin: 0 20px;
}
.decorated > span:before{
    right: 100%;
}
.decorated > span:after{
    left: 100%;
}
Run Code Online (Sandbox Code Playgroud)

  • 优秀!这应该是公认的答案。 (2认同)