段落标记内的行上的背景被阻止

发布于 2024-08-02 12:45:07 字数 407 浏览 6 评论 0原文

我正在寻找一种方法来突出显示(或填充背景)段落中的文本行。如果这很重要的话,我正在使用 WordPress 进行开发。

下面是一个示例代码:

<p> this is a line
This is a line a little longer
Just a short one
a really really long one that is longer than any
a medium sized one to end it</p>

我希望每一行都有一个黑色背景,只包含文本的长度。这些线条仅包含在 1 个 P 标签中,因此我不能只对每个 P 进行样式设置。

有任何想法/教程链接吗?我不认为我需要诉诸 jquery 来使每一行成为 P,但如果这是一个选项,我会这样做。

I'm looking for a way to highlight (or fill in background) of lines of text within a paragraph. I am developing in wordpress if that matters.

Here is an example code:

<p> this is a line
This is a line a little longer
Just a short one
a really really long one that is longer than any
a medium sized one to end it</p>

I want every line to have a black background that goes only the length of the text. The lines are wraped in only 1 P tag so I can't just style each P.

Any ideas/ links to tutorials? I dont think I need to resort to jquery to make each line a P, but will if thats an opiton.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浸婚纱 2024-08-09 12:45:07

最简单的方法是将段落设置为显示:内联并为其添加背景颜色。不过,您需要在每行末尾添加换行符:

p {
   display: inline;
   color: white;
   background-color: black;
}

并且该段落将变为:

<p> this is a line<br/>
This is a line a little longer<br/>
Just a short one<br/>
a really really long one that is longer than any<br/>
a medium sized one to end it</p>

The easiest way would be to set the paragraph to display: inline and add a background colour to it. You would need to add line breaks to the end of each line though:

p {
   display: inline;
   color: white;
   background-color: black;
}

and the paragraph would become:

<p> this is a line<br/>
This is a line a little longer<br/>
Just a short one<br/>
a really really long one that is longer than any<br/>
a medium sized one to end it</p>
不醒的梦 2024-08-09 12:45:07

浏览器将换行符视为单个空格,因此您必须修改 HTML。

我不相信你能用 Javascript 找出换行符在哪里。在这种情况下,您将必须自己修改 HTML,并且基本上按照 Pat 的建议进行操作。

Browsers treat line breaks as a single space, so you are going to have to modify the HTML.

I'm not convinced you can figure out where the line breaks exist with Javascript. In this case you're going to have to modify the HTML yourself and basically do what Pat suggested.

人间☆小暴躁 2024-08-09 12:45:07

使用 jquery,您可以使用 wrapInner 函数简单地用 HTML 包装某个标记的内容。

如果您在段落内容周围放置一个内联元素(例如span),

$("p").wrapInner("<span></span>");

则只需将CSS应用于该特定内联元素,例如像这样。

p span {
    background-color: black;
    color: white;
}

Using this solution there is no need to edit the HTML in any way.

using jquery, you can simply wrap the contents of a certain tag with HTML with the wrapInner function.

If you put an inline element (span for example) around the contents of the paragraph

$("p").wrapInner("<span></span>");

you would only need to apply the CSS to that certain inline element, for example like this.

p span {
    background-color: black;
    color: white;
}

Using this solution there is no need to edit the HTML in any way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文