简体   繁体   English

在文本下用不同颜色加下划线

[英]Underline with different color under text

I'm trying to write CSS to get this design 我正在尝试编写CSS来获得这个设计

在此输入图像描述

Here's my CSS so far: 到目前为止这是我的CSS:

.exp {
    font-weight: 300;
    display: inline-block;
    padding-bottom: 15px;
    position: relative;
    margin-bottom: 30px;
    font-style: italic;
}
.exp:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 1px;
    bottom: 0;
    left: 0%;
    border-bottom: 1px solid #219b65;
}

HTML: HTML:

<h1 class="exp">Experience</h1>

And here's the JSFIDDLE Any idea how to go about doing this? 这里是JSFIDDLE任何想法如何去做这个? I did it a few years ago but couldn't get it to work again! 我几年前做过但是再也无法上班了!

Here's a complete answer for you. 这是一个完整的答案。 Adjust widths to you needs. 根据您的需要调整宽度。

.FromTheFounder {
    font-weight: 300;
    display: inline-block;
    padding-bottom: 15px;
    position: relative;
    margin-bottom: 30px;
    font-style: italic;
}
.FromTheFounder:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 1px;
    bottom: 13px;
    left: 0%;
    border-bottom: 1px solid #219b65;
    z-index: 1;
}

.FromTheFounder:after {
    content: "";
    position: absolute;
    width: 300%;
    height: 1px;
    bottom: 13px;
    left: 0%;
    border-bottom: 1px solid #ccc;
}

https://jsfiddle.net/vjhg7mna/ https://jsfiddle.net/vjhg7mna/

Brett's answer will work perfectly if you know the width of your container, or want the underline to only span a certain width. 如果您知道容器的宽度,或者希望下划线仅跨越一定宽度,Brett的答案将完美无缺。 If you want the underline to fill the available space, you'll need two elements - one for the full-width underline, and the other for the highlighted underline. 如果希望下划线填充可用空间,则需要两个元素 - 一个用于全宽下划线,另一个用于突出显示的下划线。

This would also be possible with one element using ::after in place of exp-title and setting the content property to "Experience", but that's not very user-friendly. 这也可以使用::after代替exp-title并将content属性设置为“Experience”,但这不是非常用户友好的。

Note that I've made the underline significantly fatter (5px) so the effect is more obvious. 请注意,我使下划线显着更胖(5px),因此效果更明显。

 .exp { position: relative; font-style: italic; border-bottom: 5px solid #ccc; } .exp-title { display: inline-block; border-bottom: 5px solid #f00; font-weight: 300; padding-bottom: 15px; margin-bottom: -5px; } 
 <h1 class="exp"> <span class="exp-title">Experience</span> </h1> 

For what it's worth, another option here is to use a linear-gradient background on a pseudo element, instead of an actual border. 对于它的价值,这里的另一个选择是在伪元素上使用线性渐变背景,而不是实际边框。

The disadvantage here is that this option doesn't have the flexibility to automatically match the width of any arbitrary length of text in your h1 . 这里的缺点是该选项不具有自动匹配h1任意长度文本宽度的灵活性。 But then again, if you've got several headers, and you want the highlighted portion of the underline to be the same width for all of them, regardless of text length, this may be the way to go. 但话说回来,如果你有几个标题,并且你希望下划线的突出显示部分对于所有标题都是相同的宽度,无论文本长度如何,这可能是要走的路。

 .some-container { position: relative; padding-bottom: 15px; } .exp { font-weight: 300; display: inline-block; margin-bottom: 0; font-style: italic; } .some-container::after { content: ""; display: block; position: absolute; width: 100%; height: 3px; bottom: 0; left: 0%; background-image: linear-gradient(to right, steelblue, steelblue 25%, lightgray 25%, lightgray); } 
 <div class="some-container"> <h1 class="exp">Experience</h1> </div> 

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

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