简体   繁体   English

使用CSS flex,如何在标题之后到行尾之间获得一行(如hr元素)

[英]Using CSS flex, how can I get a line (like the hr element) starting after my title to the end of the line

I want to add something that look like a line after my title to the end of the line: 我想在标题后的行末添加类似于行的内容:

 .d-flex { display: flex; } .text-right { text-align: right; } .text-primary { color: white; } .d-flex h2.text-primary { background: blue; flex: 0 0 3rem; } .d-flex h2:not(.text-primary) { background: red; flex: 1; } .hr { height: 2px; background: cyan; } 
 <b>This is aligned left</b> <div class="d-flex"> <H2 class="text-primary">1&nbsp;/&nbsp;</H2> <div> <H2>Hello World</H2> <P>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea</P> </div> </div> <b>This is aligned right</b> <div class="d-flex"> <H2 class="text-primary">2&nbsp;/&nbsp;</H2> <div> <H2 class="text-right">Hello World</H2> <P class="text-right">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea</P> </div> </div> <b>This should have a "hr" <u>after</u> the title to the end of the line</b> <div class="d-flex"> <H2 class="text-primary">3&nbsp;/&nbsp;</H2> <div> <H2>Hello World</H2> <div class="hr"></div> <P>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea</P> </div> </div> <b>This should have a "hr" <u>before</u> the title to the beggining of the title line</b> <div class="d-flex"> <H2 class="text-primary">3&nbsp;/&nbsp;</H2> <div> <H2 class="text-right">Hello World</H2> <div class="hr"></div> <P class="text-right">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea</P> </div> </div> 

In my 3rd example, I have added div.hr . 在第三个示例中,我添加了div.hr

Using flexbox , is it possible to: 使用flexbox可以:

  1. Make it displayed after the title 使其显示在标题之后
  2. Positioned on the first title line 定位在第一个标题行上
  3. Later, I will also need to achieve the same, but from the beginning of the line to the title (for right aligned text). 稍后,我还需要实现相同的功能,但是要从行的开头到标题(对于右对齐的文本)。

Set the heading to display: flex , align-items: baseline to make horizontal line and text in the same line. 将标题设置为display: flexalign-items: baseline以在同一行中制作水平线和文本。 Then put the title text into <span> and add div .hr with flex: 1 to make its width grow to full. 然后将标题文本放入<span>并添加带有flex: 1 div .hr以使其宽度变大。 Take a look at demo: 看一下演示:

 .d-flex { display: flex; } .text-right { text-align: right; } .text-primary { color: white; } .d-flex h2.text-primary { background: blue; flex: 0 0 3rem; } .d-flex h2:not(.text-primary) { background: red; flex: 1; } .hr-baseline { display: flex; align-items: baseline; } .hr { height: 2px; background: cyan; flex: 1; } 
 <b>This is aligned left</b> <div class="d-flex"> <H2 class="text-primary">1&nbsp;/&nbsp;</H2> <div> <H2>Hello World</H2> <P>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea</P> </div> </div> <b>This is aligned right</b> <div class="d-flex"> <H2 class="text-primary">2&nbsp;/&nbsp;</H2> <div> <H2 class="text-right">Hello World</H2> <P class="text-right">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea</P> </div> </div> <b>This should have a "hr" <u>after</u> the title to the end of the line</b> <div class="d-flex"> <H2 class="text-primary">3&nbsp;/&nbsp;</H2> <div> <H2 class="hr-baseline"><span>Hello World</span><div class="hr"></div></H2> <P>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea</P> </div> </div> <b>This should have a "hr" <u>before</u> the title to the beggining of the title line</b> <div class="d-flex"> <H2 class="text-primary">3&nbsp;/&nbsp;</H2> <div> <H2 class="text-right hr-baseline"><div class="hr"></div><span>Hello World</span></H2> <P class="text-right">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea</P> </div> </div> 

Here's how you can achieve this effect, regardless of Bootstrap: 无论采用哪种引导程序,都可以通过以下方法实现此效果:

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

Can be used with any not empty element ). 可以与任何 空元素一起使用 )。

Grows to parent width or to max-width value. 增长到父级宽度或max-width值。

SCSS version, allowing setting color, stroke, padding and selector: SCSS版本,允许设置颜色,笔触,填充和选择器:

$divider-selector: ".divider";
$divider-line-color: #ccc;
$divider-padding: 1rem;
$divider-stroke: 1em;

#{$divider-selector} {
  display: flex;
  align-items: center;
  padding: 0 $divider-padding;
  &:before,
  &:after {
    content: '';
    flex: 0 1 100%;
    border-bottom: $divider-stroke solid $divider-line-color;
    margin: 0 $divider-padding;
    transform: translateY(#{$divider-stroke} / 2)
  }
  &:before {
    margin-left: 0;
  }
  &:after {
    margin-right: 0;
  }
  &[data-position="right"]:after,
  &[data-position="left"]:before {
    content: none;
  }
}

fiddle . 摆弄

如何使用<span>或</span>创建水平线<div> , 代替<hr> ?</div><div id="text_translate"><p> 我想知道如何使用标签&lt;span&gt;或&lt;div&gt;制作标签&lt;hr&gt;效果。</p><p> 我在大型网站中看到水平线,但它们不使用&lt;hr&gt;标签。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;!DOCTYPE html&gt; &lt;html lang="pt-Br"&gt; &lt;head&gt; &lt;title&gt; H - Line &lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;hr/&gt; &lt;/body&gt; &lt;/html&gt;</pre></div></div><p></p><p> .</p></div> - How can I create a horizontal line with <span> or <div>, instead of <hr>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 标题之间和之后的 Hr 行 - Hr line between and after title CSS:如何使无序列表之后的元素不从新行开始? - CSS: How can I keep the element after an unordered list from starting on a new line? 如何在 flex 元素之后创建新行? - How can I create a new line after a flex element? 带有hr线的标题 - Title with hr line 如何使用 JS 和 CSS 对 HR 标签(水平线)进行动态效果? - How can I make a dynamic effects on HR tag(horizontal line) with JS and CSS? 在我的网站上看不到hr线 - Can't see the hr line on my site 如何使用<span>或</span>创建水平线<div> , 代替<hr> ?</div><div id="text_translate"><p> 我想知道如何使用标签&lt;span&gt;或&lt;div&gt;制作标签&lt;hr&gt;效果。</p><p> 我在大型网站中看到水平线,但它们不使用&lt;hr&gt;标签。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;!DOCTYPE html&gt; &lt;html lang="pt-Br"&gt; &lt;head&gt; &lt;title&gt; H - Line &lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;hr/&gt; &lt;/body&gt; &lt;/html&gt;</pre></div></div><p></p><p> .</p></div> - How can I create a horizontal line with <span> or <div>, instead of <hr>? 如何使用CSS在bootstrap 3面板标题旁边排列一组按钮? - How can i line up a group of buttons next to a bootstrap 3 panel title using CSS? 如何添加HTML水平hr线 - How can I add HTML horizontal hr line 如何在文本下方对齐 <hr> 线正确吗? - How can I align text under a <hr> line properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM