簡體   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