带换行符的内联元素上的 XHTML/CSS 填充

发布于 2024-12-12 11:54:53 字数 766 浏览 3 评论 0原文

我有一个内联元素,其中有一个换行符。它的所有侧面都有衬垫。但是,换行符切割元素处的侧边距不存在。

这就是我的意思:

http://jsfiddle.net/4Gs2E/

右侧应该有 20px 填充标签的左侧和with的左侧,但没有。 我可以看到此工作的唯一其他方法是,如果我为每一行创建一个新元素,但此内容将动态生成,并且不会位于固定宽度的容器中,因此我看不到该工作。有没有其他方法可以在 css 中做到这一点而不需要任何 javascript?

我希望最终结果如下所示: http://jsfiddle.net/GNsw3/

但没有任何额外的元素

我也需要它与显示内联一起使用仅当我希望背景环绕文本时,内联块不会执行此操作

这可能吗?

编辑,更改示例以使我想要的内容更加明显:

当前 http://jsfiddle.net/4Gs2E/2/

我想要它的样子 http://jsfiddle.net/GNsw3/1/

I have an inline element with a line break in it. It has padding on all sides. However, the side padding on where the line break cuts the element is not there.

This is what i mean:

http://jsfiddle.net/4Gs2E/

There should be 20px padding on the right of tag and left of with but there isnt.
The only other way I can see this working is if i create a new element for every line but this content will be dynamically generated and will not be in a fixed width container so i dont see that working out. Is there any other way I can do this in css without any javascript?

I want the final result to look like this :
http://jsfiddle.net/GNsw3/

but without any extra elements

i also need this to work with display inline only as I want the background to wrap around the text as inline block doesnt do this

Is this possible?

edit, altered the examples to make what i want more visible:

current
http://jsfiddle.net/4Gs2E/2/

what i want it to look like
http://jsfiddle.net/GNsw3/1/

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

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

发布评论

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

评论(5

唱一曲作罢 2024-12-19 11:54:53

在某些情况下,您可以使用 box-shadow 来解决问题。

将元素的左右 padding 移动到其父元素,并添加两个 box-shadow

结果: http://jsfiddle.net/FpLCt/1/

浏览器支持 box-shadow: http://caniuse.com/css-boxshadow

更新:

还有一个新的 css此问题的属性称为 box-decoration-break。目前只有 Opera 支持,但希望更多浏览器能够实现此< /a> 很快。

希望这有帮助

In some cases you can use box-shadow for a workaround.

Move the right and left padding of the element to its parent and add two box-shadows.

The result: http://jsfiddle.net/FpLCt/1/

Browser support for box-shadow: http://caniuse.com/css-boxshadow

Update:

There is also a new css property for this issue called box-decoration-break. It is currently only supported by opera, but hopefully more browsers will implement this soon.

Hope this helps

半山落雨半山空 2024-12-19 11:54:53

为您找到了一个解决方案,但它并不漂亮:)

由于您无法使用 css 定位
元素,因此您必须使用 javascript。以下是如何使用 jQuery 完成您想要的事情:


// 在任何
之前和之后添加两个空格标签
$('br').replaceWith('  
>  ');

调整   元素的数量以实现两端的填充。

这是更新的 Fiddle 演示:
http://jsfiddle.net/4Gs2E/8/

Found a solution for you, but it ain't pretty :)

Since you can't target the <br> element with css, you have to use javascript. Here's how you can accomplish what you want with jQuery:


// Add two spaces before and after any <br /> tag
$('br').replaceWith('  <br />  ');

Play with the number of   elements to acheive your padding on both ends.

Here's an updated Fiddle demo:
http://jsfiddle.net/4Gs2E/8/

沫离伤花 2024-12-19 11:54:53

也许您可以使用 float: left 而不是 display: inline

http://jsfiddle.net/GolezTrol/4Gs2E/1/

Maybe you can use float: left instead of display: inline:

http://jsfiddle.net/GolezTrol/4Gs2E/1/

初雪 2024-12-19 11:54:53

通常这是通过将每个单词包装在一个自己的有边框的 SPAN 中来实现的。

Usually that is implemented by wrapping each word in an own SPAN which has border.

贩梦商人 2024-12-19 11:54:53

我只是想为自己制作 CSS 动画菜单。我发现的解决方法是将您的 INLINE-BLOCK 元素(如有必要,在 css 中进行更改,出于此解决方案的目的,将其称为具有此类属性的 span)到 block元素。然后我使用 span 的边距,因为它是周围 div 的填充。

div.menuopt {
    margin: 10px;
    padding: 0px;
    padding-left: 0px;
    overflow: hidden;
    width: 500px;
    height: 150px;
    background: grey;
}
span.menuopt {
    display: inline-block;
    margin: 0px;
    padding: 0px;
    margin-left: 150px;
    margin-top: 10px;
    font-size: 25px;
}

例子:
http://jsfiddle.net/ApbQS/

希望它能帮助任何人

I just wanted to make css-animated menu for myself. Workaround I have found is to wrap your INLINE-BLOCK element (change in css if necessary, lets call it a span with such an attribute for purpose of this solution) into block element. Then I'm using margins of span as it was padding for the surrounding div.

div.menuopt {
    margin: 10px;
    padding: 0px;
    padding-left: 0px;
    overflow: hidden;
    width: 500px;
    height: 150px;
    background: grey;
}
span.menuopt {
    display: inline-block;
    margin: 0px;
    padding: 0px;
    margin-left: 150px;
    margin-top: 10px;
    font-size: 25px;
}

Example:
http://jsfiddle.net/ApbQS/

hope it will help anyone

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