我想在网页的主标题下面创建一条细线,居中对齐,类似于这样。我该如何实现?使用/hr会创建一条端到端的线。
<hr style="width:40%">
如果您想定位横线,您可能需要使用position-style。
您可以在w3c示例网站上轻松尝试此操作。
请注意,hr-width-attribute(而不是样式!)在html5下不再起作用。
<center>Your Code Here</center>
<hr align="center" width="50%">
这里是带有简单 CSS 代码的 HR 标签:
hr {
width: 50%;
margin-left: auto;
margin-right: auto;
}
<center>Your sample code here</center>
<hr>
注意:在HTML5规范中不支持的属性都与标签的外观有关。外观应该在CSS中设置,而不是在HTML本身中设置。
因此,请使用没有属性的<hr>
标签,然后在CSS中设置其外观以使其呈现您想要的样式。
<hr>
标签缺少结束符号。 - Rob<hr width="50%">
在你给出的例子中,它使用了带有底部边框的div的css属性。
#yourDiv {
border-bottom: 1px solid gray;
}
width
属性已经在 HTML5 中被废弃,从而变得过时。https://developers.whatwg.org/obsolete.html#obsolete - Rob您可以重新定义并添加边距到您的分隔线
hr {
border:none;
border-top:1px solid;/* or bottom */
margin:1em 15%; /* 15% or whatever fixed or % value that suits your needs*/
}
body {text-align:center;}
<h1>Any content before</h1>
<hr/>
<p>any content after</p>
有一个固定的宽度,还涉及到边距
hr {
border:none;
border-top:1px solid;/* or bottom */
margin:1em auto;
width:17em;
}
body {text-align:center;}
<h1>Any content before</h1>
<hr/>
<p>any content after</p>
You can also use the border-bottom of any tag ...
h1 {
border-bottom:1px solid;
padding-bottom:0.5em;
display:table; /* to fit to content's width else margin or a fixed width works too */
margin:1em auto; /* if not display:table use width or margin values as first snippet */
}
body {text-align:center;}
<h1>Any content before</h1>
<p>any content after</p>
HTML:
CSS: . hr { width: 50% color: #222 }
CSS: .hr { 宽度:50%; 颜色:#222; }HTML:
<h1> Your code </h1>
<hr>
CSS:
hr {
align-content : center;
width : 20%;
margin-left : auto;
margin-right : auto;
}
编辑:我使用宽度来控制出现在标题或文本下方的水平线的长度。Margin-left 和 margin-right :auto 会自动将线条放置在中心位置,而通常情况下,线条会从页面的一端开始并结束在另一端。