我想知道在xhtml 1.0中有什么选项可以严格地在文本的两侧创建一行,就像这样:

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

我想过做一些像这样的奇妙的事情:

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

除了对齐问题之外,这两个选项都让人感觉“糊里糊涂”,如果你之前碰巧看到过这个,并且知道一个优雅的解决方案,我将非常感激。


当前回答

Flexbox是解决方案:

.separator { 显示:flex; 对齐项目:中心; text-align:中心; } .separator::, {后.separator:: 内容:”; flex: 1; Border-bottom: 1px实心#000; } .separator:没有(空):::{之前 margin-right: .25em; } .separator:没有(空)::{ margin-left: .25em; } <div class="separator">下一节</div> .

现在每个浏览器都支持它,如果需要,您可以通过添加相应的供应商前缀来确保与10年前的浏览器兼容。无论如何,它都会优雅地退化。

其他回答

你可以尝试做一个fieldset,并将“legend”元素(你的“next section”文本)对齐到字段的中间,只设置border-top。我不确定一个图例是如何按照字段集元素定位的。我想它可能只是一个简单的margin: 0px auto来完成这个技巧。

例子:

<fieldset>
      <legend>Title</legend>
</fieldset>

哇,我的第一个帖子,尽管这是一年以前。为了避免包装器的背景着色问题,可以在hr中使用内联块(没有人明确地说过)。文本对齐应该正确居中,因为它们是内联元素。

<div style="text-align:center">
    <hr style="display:inline-block; position:relative; top:4px; width:45%" />
       &nbsp;New Section&nbsp;
    <hr style="display:inline-block; position:relative; top:4px; width:45%" />
</div>
<div style="text-align: center; border-top: 1px solid black">
  <div style="display: inline-block; position: relative; top: -10px; background-color: white; padding: 0px 10px">text</div>
</div>

Flexbox和SCSS

HTML

<div class="divider">divider</div>

SCSS

.divider {
    display: flex;
    align-items: center;
    text-align: center;
    color: #c2c2c2;

    &::before,
    &::after {
        content: "";
        flex: 1;
        border-bottom: 1px solid #c2c2c2;
    }
    &::before {
        margin-right: 0.25em;
    }
    &::after {
        margin-left: 0.25em;
    }
}

Flexbox是解决方案:

.separator { 显示:flex; 对齐项目:中心; text-align:中心; } .separator::, {后.separator:: 内容:”; flex: 1; Border-bottom: 1px实心#000; } .separator:没有(空):::{之前 margin-right: .25em; } .separator:没有(空)::{ margin-left: .25em; } <div class="separator">下一节</div> .

现在每个浏览器都支持它,如果需要,您可以通过添加相应的供应商前缀来确保与10年前的浏览器兼容。无论如何,它都会优雅地退化。