CSS:高亮文字效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18746649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:20:02  来源:igfitidea点击:

CSS: highlighted text effect

csshighlighting

提问by Caspid

I'm trying to produce a highlighted text effect with a bit of padding, but the padding is only applied to the beginning and end, not new lines.

我试图用一点填充来产生突出显示的文本效果,但填充仅应用于开头和结尾,而不是新行。

#highlight {
background: rgba(255,230,0,0.5);
padding: 3px 5px;
margin: -3px -5px;
line-height: 1.7;
border-radius: 3px;
}

<span id=highlight>text<br>here</span>

Please see here: http://jsfiddle.net/CNJZK/7/

请看这里:http: //jsfiddle.net/CNJZK/7/

Are there any pure-CSS fixes to get the internal ("sharp") edges to extend a bit farther? i.e. like in this image: http://i.imgur.com/j8mIJZS.jpg

是否有任何纯 CSS 修复可以使内部(“锐利”)边缘延伸得更远一些?即像这张图片:http: //i.imgur.com/j8mIJZS.jpg

回答by j08691

Try setting the display on your span to inline-block:

尝试将 span 上的显示设置为 inline-block:

#highlight {
    background: rgba(255, 230, 0, 0.5);
    padding: 3px 5px;
    margin: -3px -5px;
    line-height: 1.7;
    border-radius: 3px;
    display:inline-block;
}

jsFiddle example

jsFiddle 示例

回答by Beejay

If you're not limited to a specific HTML standard, you could take a look at the <mark>tag, which was introduced with HTML5. This sitegives you a quick overlook.

如果您不限于特定的 HTML 标准,您可以查看<mark>HTML5 中引入的标签。该网站让您快速浏览。

Hope it helps!

希望能帮助到你!

回答by Ima

Solution is this CSS:

解决方案是这个CSS:

-webkit-box-decoration-break: clone;
box-decoration-break: clone;
display: inline;

https://css-tricks.com/almanac/properties/b/box-decoration-break/

https://css-tricks.com/almanac/properties/b/box-decoration-break/

回答by Nathan Upchurch

In case anyone is still looking for an answer:

如果有人仍在寻找答案:

p {
    box-shadow: 0px 0px 0px 5px yellow;
    display: inline;
    line-height: 2;
    margin: -5px -5px;
    background-color: yellow;
    border-radius: 1px;
}

See jsfiddle here.

请参阅此处的 jsfiddle。

回答by crush

You need to set the display to inline-blockor block.

您需要将显示设置为inline-blockblock

#highlight {
    display: inline-block;
    /* ... */
}