简体   繁体   English

环绕文字的矩形背景色

[英]Rectangular background color around wrapped text

I'm working on a web app and am looking for a way to show a colored background around a block of text, but in the case where the text wraps onto 2 or more lines, I only want the background to extend as far as the furthest of any of the lines of text, not as far as the edge of the container, which seems to be the default behavior. 我正在开发一个Web应用程序,正在寻找一种在一块文本周围显示彩色背景的方法,但是在文本环绕两行或更多行的情况下,我只希望背景延伸到文本的任何行中最远的地方,不远于容器的边缘,这似乎是默认行为。

Fiddle that illustrates the problem and also what I'm looking for. 小提琴说明了问题以及我在寻找什么。

Here is the simplified issue: 这是简化的问题:

<div class="outerContainer">
 <div style="background:blue">
  wrapped and workingasdesired
 </div>
</div>

I would like a way to display this as such 我想要一种显示这种方式

*wrapped and      *      |  
*workingasdesired *      |

Where | | represents the edge of the outer container, and * would be the highlighted area 表示外部容器的边缘, *为突出显示的区域

However, what actually happens is that the highlighting extends all the way to the end of the outer container: 但是,实际上发生的是突出显示一直延伸到外部容器的末尾:

*wrapped and            *|  
*notworkingasdesired    *|

I've searched around on Stack Overflow and also played with the fiddle for quite a while, but haven't had any luck. 我在Stack Overflow上进行了搜索,还玩了很长时间,但是还没有碰到任何运气。

A CSS-only solution would be the holy grail, but suggestions on good ways to accomplish this with JS would also be great! 纯CSS解决方案将是圣杯,但是有关使用JS实现此目标的好方法的建议也将很棒!

Maybe it's not exactly what you want, but you can try find a solution using inline-block in the inner div like this: 也许这并不是您想要的,但是您可以尝试使用内部div中的inline-block查找解决方案,如下所示:

.outerContainer {
    width:180px;
    background-color: red;
}
.outerContainer div{
    display:inline-block;
    background-color: blue;
}

The fiddle showing it: Fiddle 显示的小提琴: 小提琴

Hope it helps. 希望能帮助到你。

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

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