繁体   English   中英

如何在我的h1标签之前和之后进行划线?

[英]How to make a line before and after my h1 tag?

我四处寻找可以找到解决方案的方法:

-----------我的文字--------------在HTML / CSS中。 所以我需要在文字前后加上hr标签。

有谁知道该如何解决?

<!DOCTYPE html>
<html>
<head>
<title>Inline HR</title>
<style type="text/css">
div {
    text-align: center;
}

hr {
    display: inline-block;
    width: 40%;
}
</style>
</head>
<body>
<div style="center">
<hr>
My Text
<hr>
</div>
</body>
</html>

演示: http//jsfiddle.net/cKrqK/

但是,如果由于40%宽度而使HR不能一直延伸到页面边缘,您不满意,这就是解决方法。

<!DOCTYPE html>
<html>
<head>
<title>Text on HR</title>
<style type="text/css">
.text {
    position: relative;
    top: -1em;
    text-align: center;
}

.text span {
    background-color: #ffffff;
    margin-left: auto;
    margin-right: auto;
    padding-left: 0.5em;
    padding-right: 0.5em;
}
</style>
</head>
<body>
<hr>
<div class="text">
<span>
My text
</span>
</div>
</body>
</html>

演示: http//jsfiddle.net/EdtwL/

第二个示例,将带有白色背景的文本(将其更改为页面上具有背景的任何文本)放在HR上,并以margin-left: automargin-right: auto居中对齐。

您可以创建一个图像,将其设置为重复如下:

h1 {
background-image: url('line.png');
background-position: center center;
background-repeat: repeat-x;
text-align: center;
}
h1 span {
background-color: #FFF  //Or your website background color if not white

//UPDATE: to add padding around the text:
padding: 5px 10px;
}

然后在HTML中:

<h1><span>My text</span></h1>

这很简单!

h2 { text-align: center; /*optional*/ line-height: 2; background: #ccc; }
h2:before,
h2:after { padding: 1em; position: relative; top: -.4em; content: '__________________'; }

http://jsfiddle.net/xhj1poj0/

<!DOCTYPE html>
<html>
<head><title>Test</title>
<style type="text/css">
div{
    position:relative;
    height:10px;
    border-bottom:1px solid black;
}


span{
    position:absolute;
    left:50%;
    margin-left:-10px;
    background-color:white;
    padding:0px 10px 0px 10px;
}
</style>
</head>
<body>

<div>

<span>My Text</span>

</div>


</body>
</html>

像这样的东西?

h1:before {
    padding-right: 5px;
    content: "---------------------";
}
h1:after {
padding-left: 5px;
    content: "---------------------";
}

如果您真的想要一条线而不是破折号,那么我没有解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM