html - 将居中文本添加到水平线的中间

我想知道在 xhtml 1.0 strict 中有哪些选项可以像这样在文本的两侧创建一条线:

Section one
----------------------- Next section -----------------------
Section two

I've thought of doing some fancy things like this:

<div style="float:left; width: 44%;"><hr/></div>
<div style="float:right; width: 44%;"><hr/></div>
Next section

或者,因为上面的对齐有问题(垂直和水平):

<table><tr>
<td style="width:47%"><hr/></td>
<td style="vertical-align:middle; text-align: center">Next section</td>
<td style="width:47%"><hr/></td>
</tr></table>

这也有对齐问题,我用这个烂摊子解决了:

<table><tr>
<td style="border-bottom: 1px solid gray; width: 47%">&nbsp;</td>
<td style="vertical-align:middle;text-align:center" rowspan="2">Next section</td>
<td style="border-bottom: 1px solid gray; width: 47%">&nbsp;</td>
</tr><tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr></table>

除了对齐问题之外,这两个选项都让人感觉“笨拙”,如果您之前碰巧见过这个并且知道一个优雅的解决方案,我会非常感激。

最佳答案

怎么样:

<div style="width: 100%; height: 20px; border-bottom: 1px solid black; text-align: center">
  <span style="font-size: 40px; background-color: #F3F5F6; padding: 0 10px;">
    Section Title <!--Padding is optional-->
  </span>
</div>

看看这个JSFiddle .

您可以使用 vw% 使其具有响应性

https://stackoverflow.com/questions/2812770/

相关文章:

html - XHTML 中所有有效的自闭合元素(由主要浏览器实现)是什么?

html - 如何防止滚动条覆盖 IE10 中的内容?

html - 并排的两个 div - 流体显示

html - 如何更改 Canvas 元素中元素的不透明度(alpha、透明度)?

html - 标签中是否需要 ="text/css"类型?

javascript - 获取元素的父div

image - 如何在保持图像纵横比的同时拉伸(stretch)图像以填充

javascript - 如何使用 JavaScript 获取 HTML 页面的标题?

html - 如何在 div 中使按钮居中?

html - 如何删除css中的背景图像?