简体   繁体   English

内联文本背景颜色,不分为单独的元素

[英]Inline text background color without dividing into separate elements

I want to have multiple lines of text, which are just one element, to have a background layer which respects the background-padding. 我希望有多行文本,它们只是一个元素,具有一个尊重背景填充的背景图层。 As a result, I want to have this: http://d.pr/i/ImRS 因此,我想拥有这个: http//d.pr/i/ImRS

What I get is either http://jsfiddle.net/bqfk2/ or http://jsfiddle.net/Jmk4D/2/ (the latter is from this question ) which is both not what I want: first example got no left/right paddings at the beginning and end of lines, second example requires multiple elements. 我得到的是http://jsfiddle.net/bqfk2/http://jsfiddle.net/Jmk4D/2/ (后者来自这个问题 )这不是我想要的:第一个例子没有留下/右边的填充在行的开头和结尾,第二个例子需要多个元素。

<div>
    <p>desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>

<style>
div {
    font-size: 2em;
    background: #fff;
    border: 1px solid #ccc;
    padding: 1em;
    width: 300px;
}

div > p {
    background: yellow;
    display: inline;
    line-height: 1.4em;
}
</style>

I would also accept a solution which would divide the text into multiple elements which still correspond to the original text line breaks (respecting every font). 我也接受一个解决方案,将文本分成多个元素,这些元素仍然对应于原始文本换行符(尊重每个字体)。 The goal should be not to let the user divide the text into parts and still have this effect. 目标应该是让用户将文本分成几部分并仍然具有这种效果。

Try Box-shadow, 试试Box-shadow,

div {
    font-size: 2em;
    background: #fff;
    border: 1px solid #ccc;
    padding: 1em;
    width: 300px;
}
div > p {
    background: yellow;
    display: inline;
    line-height: 1.4em;
    box-shadow:0.5em 0 0 #ff0, -0.5em 0 0 #ff0;
    -moz-box-shadow:0.5em 0 0 #ff0, -0.5em 0 0 #ff0;
    -webkit-box-shadow:0.5em 0 0 #ff0, -0.5em 0 0 #ff0;
}

Source 资源

Working Fiddle 工作小提琴

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

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