简体   繁体   English

如何在单个块元素中仅为不同颜色的文本加下划线?

[英]How to underline only the text, in a different color, in a single block element?

I want to produce the following result with a single element: 我想用单个元素生成以下结果:

<style type="text/css">
    div { background-color: #000; color: #fff; }
    span { border-bottom: 1px solid #f00; }
</style>
<div><span>White text, red underline, black background</span></div>

jFiddle: http://jsfiddle.net/ARbmG/ jFiddle: http//jsfiddle.net/ARbmG/

Is this possible? 这可能吗?

I tried a couple of things and I came up with this odd little thingy. 我尝试了几件事,然后我想出了这个奇怪的小东西。 I may not recommend this, but it's very close to what you want using only one single HTML tag: 我可能不推荐这个,但它只使用一个HTML标签非常接近你想要的东西:

HTML HTML

<span>White text, red underline, black background</span>

CSS CSS

span {
    color: white;
    background: black;
    border-bottom: 2px solid red;
}

span:after {
    content: '_';
    position: absolute;
    width: 100%;
    background: black;
    color: black;
}

Demo 演示

http://jsfiddle.net/uWGx5/ http://jsfiddle.net/uWGx5/

related to : CSS text-decoration underline color and Changing Underline color 相关: CSS文本 - 装饰下划线颜色更改下划线颜色

NO, you cannot 'yet' change text-decoration underline color in a cross-plateform, cross-browser way. 不,你不能'以'跨平台,跨浏览器的方式改变文字装饰下划线颜色。

Mozilla soluce : Mozilla解决方案:

span {
    color:#fff;
    text-decoration: underline;
    -moz-text-decoration-color: red;
    /* you'll have to search for other -proprietary selectors */
}

Otherwise, to border-bottom an inline element : You just did it. 否则,要内联元素的边框 - 你只是做了它。

I found an even better solution. 我找到了更好的解决方案。

My example--white text, red underline, black background--wasn't 100% accurate. 我的例子 - 白色文字,红色下划线,黑色背景 - 不是100%准确。 It has to be a "block" element, but the background can be transparent. 它必须是“块”元素,但背景可以是透明的。

Here is a new one: http://jsfiddle.net/gpNZX/2/ 这是一个新的: http//jsfiddle.net/gpNZX/2/

The magic is the :before and :after '\\A', which is an escaped ASCII line feed. 神奇的是:之前和之后:'\\ A',这是一个转义的ASCII换行符。 I also tried using { content:' '; 我也试过用{content:''; display:block; 显示:块; } but it didn't seem to work 100% in all browsers. 但它似乎并没有在所有浏览器中100%工作。

span {
    ...
    display: inline;
    ...
}
span:before {
    content: '\A';
    white-space: pre;
}
span:after {
    content: '\A';
    white-space: pre;
}

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

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