简体   繁体   English

CSS伪元素在居中标题上加下划线

[英]CSS pseudo element underline on centered header

I have a heading which is centered in the container, and I'm using border bottom and pseudo element to draw the underline, however I'm not sure how I to make the underline stop at where the heading text ends. 我有一个以容器为中心的标题,我使用border bottom和pseudo元素来绘制下划线,但是我不知道如何在标题文本结束处使下划线停止。

在此输入图像描述

My mark-up: 我的加价:

 .headerWrap { background: #0a1633; position: relative; } /* Trying to use this to mask the overflow */ .headerWrap:after { position: absolute; display: block; content: ""; bottom: 0; left: 65%; right: 0; border-bottom: 10px solid #0a1633; } .header { margin: 0 auto 24px; text-align: center; font-weight: bold; position: relative; padding-bottom: 10px; color: #ff6347; border-bottom: 2px solid #ff6347; position: relative; } .header:before { position: absolute; right: 0; bottom: 0; left: 0; display: block; margin-bottom: 2px; content: ""; border-bottom: 6px solid #ff6347; } 
 <div class="headerWrap"> <h3 class="header">Header</h3> </div> 

Here is an idea without extra element that will work with any content. 这是一个没有额外元素的想法,可以使用任何内容。 The trick is to rely on display:inline-block to have the shrink-to-fit behavior and be able to consider the right part of the text. 诀窍是依靠display:inline-block来实现收缩到适合的行为,并能够考虑文本的正确部分。 Then we can simply rely on overflow on the left side. 然后我们可以简单地依靠左侧的溢出。

I will consider multiple background to create the line to only use one pseudo element: 我将考虑多个背景来创建只使用一个伪元素的行:

 .headerWrap { background: #0a1633; text-align: center; overflow:hidden; margin:10px; } .header { margin: 5px 0 25px; font-weight: bold; position: relative; display:inline-block; color: #ff6347; position: relative; } .header:before { content:""; position: absolute; right: 0; top:100%; left: -100vw; height:10px; background: linear-gradient(#ff6347,#ff6347) top, linear-gradient(#ff6347,#ff6347) bottom; background-size:100% 6px,100% 2px; background-repeat:no-repeat; } 
 <div class="headerWrap"> <h3 class="header">Header</h3> </div> <div class="headerWrap"> <h3 class="header">Another Header</h3> </div> 

Let's use a bit of flex . 让我们使用一点flex

By setting your container ( .headerWrap ) with a display: flex , you can have its own pseudo-elements to always shrink your content ( .header ) in the middle like you wish. 通过使用display: flex设置容器( .headerWrap ),您可以拥有自己的伪元素,以便始终.header您的意愿缩小中间内容( .header )。
From then, you just have to style the ::before pseudo element so that it gets both lines, and to style your content so that it also gets the same two lines, leaving the ::after unstyled. 从那时起,你只需要设置::before伪元素的样式,这样它就可以获得两行,并为你的内容设置样式,使它也得到相同的两行, ::after样式的情况下留下::after

 .headerWrap { background: #0a1633; position: relative; display: flex; justify-content: center; } .header { text-align: center; font-weight: bold; position: relative; color: #ff6347; border-bottom: 6px solid #ff6347; position: relative; white-space: nowrap; flex: 0 1 0; height: 25px; padding: 0; } .headerWrap:before, .headerWrap::after { display: inline-block; content: ""; flex: 1 0 0; } /* left lines */ .headerWrap:before { margin-top: 44px; border-top: 6px solid #ff6347; border-bottom: 2px solid #ff6347; height: 3px; } /* small horizontal line under text */ .header::before { content: ''; position: absolute; display: inline-block; width: 100%; margin-top: 34px; border-bottom: 2px solid #ff6347; } /* just to show you can change the content dynamically */ .hidden { display: none; } .headerWrap:hover .hidden{ display: inline; } 
 <div class="headerWrap"> <h3 class="header"><span class="hidden">This is an </span>Header</h3> </div> 

And here is a version a bit more dynamic, but using one more wrapping element, so that we can align the ::before on the content's baseLine (above version is using hard-coded height and margin-top rules). 这里有一个更动态的版本,但是使用了一个更多的包装元素,这样我们就可以将::before放在内容的baseLine上(上面的版本使用的是硬编码的高度和margin-top规则)。

 .headerContainer { display: flex; align-items: center; background: #0a1633; } .headerWrap { position: relative; display: flex; align-items: flex-end; height: 3em; color: #ff6347; flex: 1 0 0; padding: 1em 0; } .header { text-align: center; font-weight: bold; position: relative; position: relative; white-space: nowrap; flex: 0 1 0; margin: 0; padding: 0 0 12px; border-bottom: 2px solid; } .headerWrap:before, .headerWrap::after { display: inline-block; content: ""; flex: 1 0 0; } /* left lines */ .headerWrap:before { border-top: 6px solid; border-bottom: 2px solid; height: 3px; } /* small horizontal line under text */ .header::before { content: ''; position: absolute; display: inline-block; width: 100%; bottom: 3px; left: 0; border-bottom: 6px solid #ff6347; } /* just to show you can change the content dynamically */ .hidden { display: none; } .headerWrap:hover .hidden{ display: inline; } 
 <div class="headerContainer"> <div class="headerWrap"> <h3 class="header"><span class="hidden">This is an </span>Header</h3> </div> </div> 

If you don't mind using jQuery you could do the following: 如果您不介意使用jQuery,可以执行以下操作:

var height= $('#test').height();
var width= $('#test').width();
var position= $('#test').position().left
var set= position+width + 'px';
$('<style>div.headerWrap:after{left:'+set+'}</style>').appendTo('body');

.position() .left is used to get the x coordinate, .width() to get the element width. .position() .left用于获取x坐标, .width()用于获取元素宽度。 And add them altogether to make the underline stop at where the heading text ends. 并完全添加它们以使标题文本结束处的下划线停止。

Then add $(window).on('resize', function(){} to detect window resize. 然后添加$(window).on('resize', function(){}来检测窗口大小调整。

Snippet: 片段:

 function count(){ var height= $('#test').height(); var width= $('#test').width(); var position= $('#test').position().left var set= position+width + 'px'; $('<style>div.headerWrap:after{left:'+set+'}</style>').appendTo('body'); } count(); $(window).on('resize', function(){ count(); }); 
 .headerWrap { background: #0a1633; position: relative; } /* Trying to use this to mask the overflow */ .headerWrap:after { position: absolute; display: block; content: ""; bottom: 0; left: 65%; right: 0; border-bottom: 10px solid #0a1633; } .header { margin: 0 auto 24px; text-align: center; font-weight: bold; position: relative; padding-bottom: 10px; color: #ff6347; border-bottom: 2px solid #ff6347; position: relative; } .header:before { position: absolute; right: 0; bottom: 0; left: 0; display: block; margin-bottom: 2px; content: ""; border-bottom: 6px solid #ff6347; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="headerWrap"> <h3 class="header"><label id="test">Header</label></h3> </div> 

If you know(calculate) the width of text "Header", you can do it like this: (and you don't need to use the mask.) 如果您知道(计算)文本“Header”的宽度,您可以这样做:(并且您不需要使用蒙版。)

 .headerWrap { background: #0a1633; position: relative; } .header { margin-top:0; margin-bottom:24px; text-align: right; font-weight: bold; position: relative; padding-bottom: 10px; color: #ff6347; border-bottom: 2px solid #ff6347; right:-webkit-calc(50% - 30px); right:-moz-calc(50% - 30px); right:calc(50% - 30px); } .header:before { position: absolute; right: 0; bottom: 0; left: 0; display: block; margin-bottom: 2px; content: ""; border-bottom: 6px solid #ff6347; } 
 <div class="headerWrap"> <h3 class="header">Header</h3> </div> 

i have created a empty div with same background color with padding bottom and so we can adjust the with of as such u need right now 我已经创建了一个具有相同背景颜色和填充底部的空div,因此我们可以根据您的需要立即调整

i have changed property of h3, instead of h3 you can even use div with custom css as u need. 我已经改变了h3的属性,而不是h3你甚至可以根据需要使用div和自定义css。

check the below code. 检查以下代码。

  .headerWrap { background: #0a1633; position: relative; } .header { margin-top:0; font-weight: bold; position: relative; color: #ff6347; text-align: center; } .inner { background-color: #ff6347; width: 54%; height: 0px; background-color: ff6347; padding-bottom: 5px; } h3{ margin-block-start: 0em; margin-block-end: 0em; } 
  <div class="headerWrap"> <h3 class="header">Header</h3> <div class="inner">&nbsp;</div> </div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM