【问题标题】:Add centered text to the middle of a horizontal rule [duplicate]将居中文本添加到水平线的中间[重复]
【发布时间】:2011-02-18 06:12:15
【问题描述】:

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

第一节 ------------------------ 下一节 ------------ 第二节

我想过做一些像这样的花哨的事情:

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

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

【问题讨论】:

标签: html css xhtml line vertical-alignment


【解决方案1】:

怎么样:

<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% 使其具有响应性

【讨论】:

  • 获得准确的最高值有点困难。不完全是 -0.5em;
  • 完美。 “响应式”也是
  • 查看我的答案以获得不需要您指定背景颜色或宽度的解决方案。
  • 请参阅my answer 了解响应式、透明背景、可变样式和分隔高度、可变文本位置。也可以多次应用于不同的选择器,以便在同一个项目中使用多种样式的分隔符,使用 SCSS。适用于任何font-size
【解决方案2】:

在不知道宽度和背景颜色的情况下解决这个问题的方法如下:

结构

<div class="strike">
   <span>Kringle</span>
</div>

CSS

.strike {
    display: block;
    text-align: center;
    overflow: hidden;
    white-space: nowrap; 
}

.strike > span {
    position: relative;
    display: inline-block;
}

.strike > span:before,
.strike > span:after {
    content: "";
    position: absolute;
    top: 50%;
    width: 9999px;
    height: 1px;
    background: red;
}

.strike > span:before {
    right: 100%;
    margin-right: 15px;
}

.strike > span:after {
    left: 100%;
    margin-left: 15px;
}

示例:http://jsfiddle.net/z8Hnz/

双线

要创建双线,请使用以下选项之一:

1) 固定行间距

.strike > span:before,
.strike > span:after {
    content: "";
    position: absolute;
    top: 50%;
    width: 9999px;
    border-top: 4px double red;

示例:http://jsfiddle.net/z8Hnz/103/

2) 自定义行间距

.strike > span:before,
.strike > span:after {
    content: "";
    position: absolute;
    top: 50%;
    width: 9999px;
    height: 5px; /* space between lines */
    margin-top: -2px; /* adjust vertical align */
    border-top: 1px solid red;
    border-bottom: 1px solid red;
}

示例:http://jsfiddle.net/z8Hnz/105/


SASS (SCSS) 版本

基于这个解决方案,如果它可以帮助某人,我添加了“带有颜色属性”的 SCSS...

//mixins.scss
@mixin bg-strike($color) {

    display: block;
    text-align: center;
    overflow: hidden;
    white-space: nowrap; 

    > span {

        position: relative;
        display: inline-block;

        &:before,
        &:after {
            content: "";
            position: absolute;
            top: 50%;
            width: 9999px;
            height: 1px;
            background: $color;
        }

        &:before {
            right: 100%;
            margin-right: 15px;
        }

        &:after {
            left: 100%;
            margin-left: 15px;
        }
    }
}

使用示例:

//content.scss
h2 {
    @include bg-strike($col1);
    color: $col1;
}

【讨论】:

  • 如果您使用图像作为线条,此版本效果更好 - 或者至少更容易编辑并保持响应
  • 非常出色的工作!你能用你的魔法创造一条双线吗? (否则,我将只使用一个微小的重复背景图像。)
  • 很棒的sn-p!谢谢:)
  • 这是最好的答案,因为它可以在没有设置背景的情况下工作,并且当你必须设置透明背景时
  • 第一个例子简直太棒了!恭喜!应该是正确答案!
【解决方案3】:

Flexbox 是解决方案:

.separator {
  display: flex;
  align-items: center;
  text-align: center;
}

.separator::before,
.separator::after {
  content: '';
  flex: 1;
  border-bottom: 1px solid #000;
}

.separator:not(:empty)::before {
  margin-right: .25em;
}

.separator:not(:empty)::after {
  margin-left: .25em;
}
&lt;div class="separator"&gt;Next section&lt;/div&gt;

现在every browser supports it,如果需要,您可以通过添加相应的供应商前缀来确保与十年前浏览器的兼容性。无论如何它都会优雅地降级。

【讨论】:

  • 目前考虑到 flexbox 支持的最佳答案
  • 这是一个不错的解决方案
  • 我相信放置一个 span 并为其提供边距而不是伪类是为文本提供边距的更灵活的解决方案。这样,它可以用作带有或不带有文本的单个分隔符组件。 &lt;div class="separator"&gt;&lt;span&gt;Next section&lt;/span&gt;&lt;/div&gt;.separator span { margin-left: .25em; margin-right: .25em; }
  • @ErdemErgin 我编辑了我的答案以解决您的用例。
【解决方案4】:

您可以在不知道容器宽度或背景颜色的情况下使用 :before 和 :after 完成此操作,并且使用它们可以更好地设置换行符的样式。比如这个可以修改成双线、虚线等。

JSFiddle

CSS 和 HTML 用法:

.hr-sect {
    display: flex;
    flex-basis: 100%;
    align-items: center;
    color: rgba(0, 0, 0, 0.35);
    margin: 8px 0px;
}
.hr-sect:before,
.hr-sect:after {
    content: "";
    flex-grow: 1;
    background: rgba(0, 0, 0, 0.35);
    height: 1px;
    font-size: 0px;
    line-height: 0px;
    margin: 0px 8px;
}
&lt;div class="hr-sect"&gt;CATEGORY&lt;/div&gt;

SCSS 版本:

.hr-sect {
    display: flex;
    flex-basis: 100%;
    align-items: center;
    color: rgba(0, 0, 0, 0.35);
    margin: 8px 0;

    &:before, &:after {
        content: "";
        flex-grow: 1;
        background: rgba(0, 0, 0, 0.35);
        height: 1px;
        font-size: 0;
        line-height: 0;
        margin: 0 8px;
    }
}

【讨论】:

  • 优雅的解决方案,虽然它有一个警告:如果没有文字,你仍然会有行与行之间的差距。
  • 老实说,这是该线程中最干净的解决方案,没有文本的问题可以忽略,因为您希望始终使用带有文本的分隔符。
  • background: rgba(0, 0, 0, 0.35);改成border-top: 1px dashed rgba(0, 0, 0, 0.35);来控制线条样式。
【解决方案5】:

试试这个:

.divider {
	width:500px;
	text-align:center;
}

.divider hr {
	margin-left:auto;
	margin-right:auto;
	width:40%;

}

.left {
	float:left;
}

.right {
	float:right;
}
<div class="divider">
    <hr class="left"/>TEXT<hr class="right" />
</div>

jsFiddle 上的实时预览。

【讨论】:

  • 它完美运行并使用
    那么这个答案可能有什么问题?我什么也不说。
  • +1 无需弄乱背景颜色或以非预期方式使用标签。一个缺点是.divider hr 的宽度取决于文本的长度。建议:.divider.divider hr 宽度应以em 为单位指定。要获得hr 宽度,请使用 (.divider 宽度减去字符数) / 2。
  • 40% 在这里是错误的。如果文本较长,则会错位
【解决方案6】:

我刚遇到同样的问题,这里有一种解决方法:

<table width="100%">
  <tr>
    <td><hr /></td>
    <td style="width:1px; padding: 0 10px; white-space: nowrap;">Some text</td>
    <td><hr /></td>
  </tr>
</table>​

它无需在文本元素上设置背景即可工作,即无论其背后是什么背景,它都会看起来不错。

您可以在这里试用:http://jsfiddle.net/88vgS/

【讨论】:

  • 不能解决 OP 的问题。这会在它们之间创建两行文本。他想要一条中断的线,并且在中断的部分有文字,然后这条线继续。
  • 它实际上做了 OP 所要求的。你可以在jsfiddle.net/88vgS看到它
  • 另外,什么是中途断线,断线处有文本,如果不是两行,它们之间有文本?
  • 您应该在其中放置适当的 TR 标签。我想这让我一时糊涂了。出于某种原因,我最初认为它是在三行上,而不是三列上。正确的标记可能不会让我这么看。无论如何,这绝对是一个可行的解决方案。
  • 但这使用了表格,这是不允许的!哦,开个玩笑。 GRIDS 有自己的位置,几乎所有其他 GUI XML 框架都认识到这一点并在任何地方使用它们(例如 WPF / XAML)。感谢大佬的解决方案!这值得超过 3 票。我怀疑人们对讨厌的桌子的厌恶会成为一个障碍。
【解决方案7】:

更新:这不适用于 HTML5

相反,请查看此问题以了解更多技术:CSS challenge, can I do this without introducing more HTML?


我使用line-height:0在我的站点guerilla-alumnus.com的标题中创建效果

<div class="description">
   <span>Text</span>
</div>

.description {
   border-top:1px dotted #AAAAAA;
}

.description span {
   background:white none repeat scroll 0 0;
   line-height:0;
   padding:0.1em 1.5em;
   position:relative;
}

另一个好方法是http://robots.thoughtbot.com/

他使用背景图片和浮动来达到很酷的效果

【讨论】:

  • 我喜欢这个,它在您的网站上看起来很棒,但我无法让它工作(我怀疑受到 jQuery css 的干扰)。 :( 但它真的很酷。
  • 我认为你的 h1 将跨度向下推。
  • 这是一个“hack”,它采用了 DOCTYPE XTHML 1.0 过渡的不同呈现行为。如果您使用 DOCTYPE html(用于 HTML5),它将无法正常工作。
【解决方案8】:

引导网格:

HTML:

  <div class="row vertical-center">
    <div class="col-xs-5"><hr></div>
    <div class="col-xs-2" style="color: white"> Text</div>
    <div class="col-xs-5"><hr></div>
  </div>

CSS:

.vertical-center{
  display: flex;
  align-items: center;
}

【讨论】:

  • vertical-center 类不再存在于引导程序 (4.3.1) 中。您能否更新您的答案为align-items-center
【解决方案9】:
<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>

【讨论】:

    【解决方案10】:

    如果您可以使用 CSS 并且愿意使用已弃用的 align 属性,则样式化的字段集/图例将起作用:

    <style type="text/css">
    fieldset { 
        border-width: 1px 0 0 0;
    }
    </style>
    
    <fieldset>
    <legend align="center">First Section</legend>
    Section 1 Stuff
    </fieldset>
    
    <fieldset>
    <legend align="center">Second Section</legend>
    Section 2 Stuff
    </fieldset>
    

    字段集的预期目的是对表单字段进行逻辑分组。正如 willoler 所指出的,text-align: center 样式不适用于 legend 元素。 align="center" 已弃用 HTML,但它应该在所有浏览器中正确居中文本。

    【讨论】:

    • 更详细地解释我的目的。
    • 我很确定&lt;legend&gt; 具有blockinline-block 元素的显示特性,因为它可以被填充边距,这意味着@ 987654329@ 不应该工作...
    • 是的,图例对语义非常有用——我用它们来包装广播组,但它们是出了名的固执
    • 我更正了我的答案。不幸的是,由于某些浏览器在设置legend 元素的样式时顽固,align="center" 是我能找到的唯一能够可靠地将legend 居中的解决方案。
    【解决方案11】:

    您可以只使用相对位置并在父级上设置高度

    .fake-legend {
      height: 15px;
      width:100%;
      border-bottom: solid 2px #000;
      text-align: center;
      margin: 2em 0;
    }
    .fake-legend span {
      background: #fff;
      position: relative;
      top: 0;
      padding: 0 20px;
      line-height:30px;
    }
    <p class="fake-legend"><span>Or</span>
    </p>

    【讨论】:

      【解决方案12】:

      这是一个只有 css 的简单解决方案,没有背景技巧......

      .center-separator {
          display: flex;
        line-height: 1em;
        color: gray;
      }
      
      .center-separator::before, .center-separator::after {
          content: '';
          display: inline-block;
          flex-grow: 1;
          margin-top: 0.5em;
          background: gray;
          height: 1px;
          margin-right: 10px;
          margin-left: 10px;
        }
      

      HTML:

        <div class="center-separator">
          centered text
        </div>
      

      小提琴示例:https://jsfiddle.net/0Lkj6wd3/

      【讨论】:

      • 伟大且可重复使用的解决方案,例如实用程序类。
      【解决方案13】:
      <fieldset style="border:0px; border-top:1px solid black">
          <legend>Test</legend>
      </fieldset>
      

      邪恶的黑客...

      【讨论】:

      • 我不会称其为 hack,但我会测试它是否适用于您打算支持的所有浏览器。
      • fieldset 不想被这样使用,它已经被黑客入侵了 ;-)
      【解决方案14】:

      更新一个 2 年前的帖子,但这是我仅使用一个元素和 CSS 来处理这些情况的方法。

      ​h1 {
          text-align: center;
      }
      
      h1:before,
      h1:after {
          content: "";
          background: url('http://heritageonfifth.com/images/seperator.png') left center repeat-x;
          width: 15%;
          height: 30px;
          display: inline-block;
          margin: 0 15px 0 0;
      }
      
      h1:after{
          background-position: right center;
          margin: 0 0 0 15px;
      }
      

      ​这里有一个小技巧可以检查一下:http://jsfiddle.net/sRhBc/(来自 Google 的随机图片,用于边界)。

      这种方法的唯一缺点是它不支持 IE7。

      【讨论】:

        【解决方案15】:

        随便用

            hr
            {
                padding: 0;
                border: none;
                border-top: 1px solid #CCC;
                color: #333;
                text-align: center;
                font-size: 12px;
                vertical-align:middle;
            }
            hr:after
            {
                content: "Or";
                display: inline-block;
                position: relative;
                top: -0.7em;
                font-size: 1.2em;
                padding: 0 0.25em;
                background: white;
            }
        

        【讨论】:

          【解决方案16】:

          哇哦我的第一篇文章,即使这是一岁了。为了避免包装器的背景着色问题,您可以将 inline-block 与 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>
          

          【讨论】:

            【解决方案17】:

            我使用h1,中间有一个跨度。

            HTML 示例:

            <h1><span>Test archief</span></h1>
            

            CSS 示例:

            .archive h1 {border-top:3px dotted #AAAAAA;}
            .archive h1 span { display:block; background:#fff; width:200px; margin:-23px auto; text-align:center }
            

            就这么简单。

            【讨论】:

            • 欢迎来到 StackOverflow,Youri。您可以考虑使用div,而不是span。它具有相同的目的,除了它自然是一个块元素。 :)
            • @Nix 块元素内联元素?不用了,span 是个不错的选择
            • @MrAzulay h1 是一个内联元素。 span 非常适合包装东西,但在这种情况下我更喜欢 div 的原因是因为 Youri 使用 CSS 将 span 设置为 display:block;。当有合适的块元素 (div) 随时可用时,我看不出将内联元素变成块元素的意义。
            • @Nix 这不是很常见的事情吗?虽然将块放在内联中是不好的做法。这是一篇关于它的好帖子skypoetsworld.blogspot.se/2008/10/…
            • 糟糕,我的最新评论打错了。我同意你不应该在 inline 元素中放置一个块,但 h1 实际上是一个 BLOCK 元素。对困惑感到抱歉。不过,我看不出将span 变成块元素的意义,但足够公平。
            【解决方案18】:

            看了上面,我修改为:

            CSS

            .divider {
                font: 33px sans-serif;
                margin-top: 30px;
                text-align:center;
                text-transform: uppercase;
            }
            .divider span {
                position:relative;
            }
            .divider span:before, .divider span:after {
                border-top: 2px solid #000;
                content:"";
                position: absolute;
                top: 15px;
                right: 10em;
                bottom: 0;
                width: 80%;
            }
            .divider span:after {
                position: absolute;
                top: 15px;
                left:10em;
                right:0;
                bottom: 0;
            }
            

            HTML

            <div class="divider">
                <span>This is your title</span></div>
            

            似乎工作正常。

            【讨论】:

              【解决方案19】:

              采用@kajic 的解决方案并将样式放入 CSS:

              <table class="tablehr">
                <td><hr /></td>
                <td>Text!</td>
                <td><hr /></td>
              </table>
              

              然后是 CSS(但它在使用第 n 个选择器时取决于 CSS3):

              .tablehr { width:100%; }
              .tablehr > tbody > tr > td:nth-child(2) { width:1px; padding: 0 10px; white-space: nowrap; }
              

              干杯。

              (PS 在 tbody 和 tr 上,Chrome、IE 和 Firefox 至少会自动插入一个 tbody 和 tr,这就是为什么我的示例(如 @kajic 的示例)没有包含这些,以便在所需的 html 标记中保持更短的内容。截至 2013 年,此解决方案已使用最新版本的 IE、Chrome 和 Firefox 进行了测试。

              【讨论】:

                【解决方案20】:

                DEMO PAGE

                HTML

                <header>
                  <h1 contenteditable>Write something</h1>
                </header>
                

                CSS

                header{ 
                  display:table;
                  text-align:center; 
                }
                header:before, header:after{ 
                  content:'';
                  display:table-cell; 
                  background:#000; 
                  width:50%;
                  -webkit-transform:scaleY(0.3);
                  transform:scaleY(0.3);
                }
                header > h1{ white-space:pre; padding:0 15px; }
                

                【讨论】:

                  【解决方案21】:

                  这对我有用,并且不需要文本后面的背景颜色来隐藏边框线,而是使用实际的 hr 标签。您可以使用宽度来获得不同大小的 hr 线。

                  <div>
                      <div style="display:inline-block;width:45%"><hr width='80%' /></div>
                      <div style="display:inline-block;width: 9%;text-align: center;vertical-align:90%;text-height: 24px"><h4>OR</h4></div>
                      <div style="display:inline-block;width:45%;float:right" ><hr width='80%'/></div>
                  </div>
                  

                  【讨论】:

                    【解决方案22】:

                    我制作了一个fiddle,它使用了 FlexBox,还会为您提供不同样式的 HR(双线、线中心的符号、阴影、插入、虚线等)。

                    CSS

                    button {
                        padding: 8px;
                        border-radius: 4px;
                        background-color: #fff;
                        border: 1px solid #ccc;
                        margin: 2px;
                      }
                    
                      button:hover {
                        cursor: pointer;
                      }
                    
                      button.active {
                        background-color: rgb(34, 179, 247);
                        color: #fff;
                      }
                    
                      .codeBlock {
                        display: none;
                      }
                    
                      .htmlCode, .cssCode {
                        background-color: rgba(34, 179, 247, 0.5); 
                        width: 100%;
                        padding: 10px;
                      }
                    
                      .divider {
                        display: flex;
                        flex-direction: row;
                        flex-flow: row;
                        width: 100%;
                      }
                    
                      .divider.fixed {
                        width: 400px;
                      }
                    
                      .divider > label {
                        align-self: baseline;
                        flex-grow: 2;
                        white-space: nowrap;
                      }
                    
                      .divider > hr {
                        margin-top: 10px;
                        width: 100%;
                        border: 0;
                        height: 1px;
                        background: #666;
                      }
                    
                      .divider.left > label {
                        order: 1;
                        padding-right: 5px;
                      }
                    
                      .divider.left > hr {
                        order: 2
                      }
                    
                      .divider.right > label {
                        padding-left: 5px;
                      }
                      /* hr bars with centered text */
                      /* first HR in a centered version */
                    
                      .divider.center >:first-child {
                        margin-right: 5px;
                      }
                      /* second HR in a centered version */
                    
                      .divider.center >:last-child {
                        margin-left: 5px;
                      }
                      /** HR style variations **/
                    
                      hr.gradient {
                        background: transparent;
                        background-image: linear-gradient(to right, #f00, #333, #f00);
                      }
                    
                      hr.gradient2 {
                        background: transparent;
                        background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(255, 0, 0, 0.5), rgba(0, 0, 0, 0));
                      }
                    
                      hr.dashed {
                        background: transparent;
                        border: 0;
                        border-top: 1px dashed #666;
                      }
                    
                      hr.dropshadow {
                        background: transparent;
                        height: 12px;
                        border: 0;
                        box-shadow: inset 0 12px 12px -12px rgba(0, 0, 0, 0.5);
                      }
                    
                      hr.blur {
                        background: transparent;
                        border: 0;
                        height: 0;
                        /* Firefox... */
                        box-shadow: 0 0 10px 1px black;
                      }
                    
                      hr.blur:after {
                        background: transparent;
                        /* Not really supposed to work, but does */
                        content: "\00a0";
                        /* Prevent margin collapse */
                      }
                    
                      hr.inset {
                        background: transparent;
                        border: 0;
                        height: 0;
                        border-top: 1px solid rgba(0, 0, 0, 0.1);
                        border-bottom: 1px solid rgba(255, 255, 255, 0.3);
                      }
                    
                      hr.flared {
                        background: transparent;
                        overflow: visible;
                        /* For IE */
                        height: 30px;
                        border-style: solid;
                        border-color: black;
                        border-width: 1px 0 0 0;
                        border-radius: 20px;
                      }
                    
                      hr.flared:before {
                        background: transparent;
                        display: block;
                        content: "";
                        height: 30px;
                        margin-top: -31px;
                        border-style: solid;
                        border-color: black;
                        border-width: 0 0 1px 0;
                        border-radius: 20px;
                      }
                    
                      hr.double {
                        background: transparent;
                        overflow: visible;
                        /* For IE */
                        padding: 0;
                        border: none;
                        border-top: medium double #333;
                        color: #333;
                        text-align: center;
                      }
                    
                      hr.double:after {
                        background: transparent;
                        content: "§";
                        display: inline-block;
                        position: relative;
                        top: -0.7em;
                        font-size: 1.5em;
                        padding: 0 0.25em;
                      }
                    

                    HTML

                    <div class="divider left">
                      <hr />
                      <label>Welcome!</label>
                    </div>
                    <p>&nbsp;</p>
                    <div class="divider right">
                      <hr />
                      <label>Welcome!</label>
                    </div>
                    <p>&nbsp;</p>
                    <div class="divider center">
                      <hr />
                      <label>Welcome!</label>
                      <hr />
                    </div>
                    <p>&nbsp;</p>
                    <div class="divider left fixed">
                      <hr />
                      <label>Welcome!</label>
                    </div>
                    <p>&nbsp;</p>
                    <div class="divider right fixed">
                      <hr />
                      <label>Welcome!</label>
                    </div>
                    <p>&nbsp;</p>
                    <div class="divider center fixed">
                      <hr />
                      <label>Welcome!</label>
                      <hr />
                    </div>
                    

                    或者这里是一个交互式 Fiddle:http://jsfiddle.net/mpyszenj/439/

                    【讨论】:

                      【解决方案23】:

                      使用 Bootstrap 4 这似乎对我有用:

                      <div class="row">
                        <div class="col"><hr/></div>
                        <div class="col-auto">Or</div>
                        <div class="col"><hr/></div>
                      </div>

                      【讨论】:

                        【解决方案24】:

                        总是有不错的旧FIELSET

                         fieldset
                         {
                        border-left: none;
                        border-right: none;
                        border-bottom: none;
                         }
                         fieldset legend
                         {
                        text-align: center;
                        padding-left: 10px;
                        padding-right: 10px;
                         }
                        

                        【讨论】:

                        • 字段集只能与表单一起使用。
                        【解决方案25】:

                        您可以尝试使用fieldset,并将“图例”元素(您的“下一部分”文本)与仅设置border-top 的字段中间对齐。我不确定如何根据 fieldset 元素定位图例。不过,我想这可能只是一个简单的margin: 0px auto 就可以了。

                        示例:

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

                        【讨论】:

                          【解决方案26】:

                          .orSpan{
                            position: absolute;
                            margin-top: -1.3rem;
                            margin-left:50%;
                            padding:0 5px;
                            background-color: white;
                          }
                          <div>
                             <hr> <span class="orSpan">OR</span>
                          </div>

                          您可能需要调整 ma​​rgin 属性

                          【讨论】:

                            【解决方案27】:

                            html

                            <div style="display: grid; grid-template-columns: 1fr 1fr 1fr;" class="add-heading">
                                <hr class="add-hr">
                                <h2>Add Employer</h2>
                                <hr class="add-hr">
                            </div>
                            

                            css

                            .add-hr { 
                                display: block; height: 1px;
                                border: 0; border-top: 4px solid #000;
                                margin: 1em 0; padding: 0; 
                              }
                            
                            .add-heading h2{
                              text-align: center;
                            }
                            

                            【讨论】:

                              【解决方案28】:

                              您可以在没有&lt;hr /&gt; 的情况下完成此操作。从语义上来说,设计应该使用 CSS 的方式,在这种情况下是很有可能的。

                              div.header
                              {
                                position: relative;
                                text-align: center;
                                padding: 0 10px;
                                background: #ffffff;
                              }
                              
                              div.line
                              {
                                position: absolute;
                                top: 50%;
                                border-top: 1px dashed;
                                z-index: -1;
                              }
                              
                              <div class="header">
                                Next section
                                <div class="line">&nbsp;</div>
                              </div>
                              

                              【讨论】:

                                【解决方案29】:

                                CSS

                                .Divider {
                                width: 100%; height: 30px;  text-align: center;display: flex;
                                }
                                .Side{
                                width: 46.665%;padding: 30px 0;
                                }
                                .Middle{
                                width: 6.67%;padding: 20px 0;
                                }
                                

                                HTML

                                <div class="Divider">
                                    <div class="Side"><hr></div>
                                    <div class="Middle"><span>OR</span></div>
                                    <div class="Side"><hr></div>
                                </div>
                                

                                您可以根据标签“span”中文本的长度修改“side”和“middle”类的宽度。确保“中间”的宽度加上“边”宽度的2倍等于100%。

                                【讨论】:

                                  【解决方案30】:

                                  响应式、透明背景、可变高度和样式的分隔线、可变的文本位置、可调节的分隔线和文本之间的距离。也可以在同一个项目中使用不同的选择器多次应用多个分隔线样式。
                                  SCSS 下面。

                                  标记 (HTML):

                                  <div class="divider" text-position="right">Divider</div>
                                  

                                  CSS

                                  .divider {
                                    display: flex;
                                    align-items: center;
                                    padding: 0 1rem;
                                  }
                                  
                                  .divider:before,
                                  .divider:after {
                                    content: '';
                                    flex: 0 1 100%;
                                    border-bottom: 5px dotted #ccc;
                                    margin: 0 1rem;
                                  }
                                  
                                  .divider:before {
                                    margin-left: 0;
                                  }
                                  
                                  .divider:after {
                                    margin-right: 0;
                                  }
                                  
                                  .divider[text-position="right"]:after,
                                  .divider[text-position="left"]:before {
                                    content: none;
                                  }
                                  

                                  没有text-position,它默认居中。

                                  演示:

                                  .divider {
                                    display: flex;
                                    align-items: center;
                                    padding: 0 1rem;
                                  }
                                  
                                  .divider:before,
                                  .divider:after {
                                    content: '';
                                    flex: 0 1 100%;
                                    border-bottom: 5px dotted #ccc;
                                    margin: 0 1rem;
                                  }
                                  
                                  .divider:before {
                                    margin-left: 0;
                                  }
                                  
                                  .divider:after {
                                    margin-right: 0;
                                  }
                                  
                                  .divider[text-position="right"]:after,
                                  .divider[text-position="left"]:before {
                                    content: none;
                                  }
                                  <span class="divider" text-position="left">Divider</span>
                                  <h2 class="divider">Divider</h2>
                                  <div class="divider" text-position="right">Divider</div>

                                  SCSS,快速修改:

                                  $divider-selector    : ".divider";
                                  $divider-border-color: rgba(0,0,0,.21);
                                  $divider-padding     : 1rem;
                                  $divider-border-width: 1px;
                                  $divider-border-style: solid;
                                  $divider-max-width   : 100%;
                                  
                                  #{$divider-selector} {
                                      display: flex;
                                      align-items: center;
                                      padding: 0 $divider-padding;
                                      max-width: $divider-max-width;
                                      margin-left: auto;
                                      margin-right: auto;
                                  
                                      &:before,
                                      &:after {
                                          content: '';
                                          flex: 0 1 100%;
                                          border-bottom: $divider-border-width $divider-border-style $divider-border-color;
                                          margin: 0 $divider-padding;
                                          transform: translateY(#{$divider-border-width} / 2)
                                      }
                                  
                                      &:before {
                                          margin-left: 0;
                                      }
                                  
                                      &:after {
                                          margin-right: 0;
                                      }
                                  
                                      &[text-position="right"]:after,
                                      &[text-position="left"]:before {
                                          content: none;
                                      }
                                  }
                                  

                                  fiddle here.

                                  【讨论】:

                                  • 您好,非常感谢您的代码 sn-p。如果我输入除英文以外的分隔符的文本(在我的情况下是韩文),文本每两个字母就会换行。你能找出原因吗?
                                  猜你喜欢
                                  • 2013-04-07
                                  • 2021-02-07
                                  • 1970-01-01
                                  • 2012-02-16
                                  • 2021-11-09
                                  • 2011-03-29
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  相关资源
                                  最近更新 更多