[英]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 / </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 / </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 / </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 / </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可以:
Set the heading to display: flex
, align-items: baseline
to make horizontal line and text in the same line. 将标题设置为
display: flex
, align-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 / </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 / </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 / </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 / </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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.