带换行符的内联元素上的 XHTML/CSS 填充
我有一个内联元素,其中有一个换行符。它的所有侧面都有衬垫。但是,换行符切割元素处的侧边距不存在。
这就是我的意思:
右侧应该有 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在某些情况下,您可以使用
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 twobox-shadow
s.The result: http://jsfiddle.net/FpLCt/1/
Browser support for
box-shadow
: http://caniuse.com/css-boxshadowUpdate:
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
为您找到了一个解决方案,但它并不漂亮:)
由于您无法使用 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/
也许您可以使用
float: left
而不是display: inline
:http://jsfiddle.net/GolezTrol/4Gs2E/1/
Maybe you can use
float: left
instead ofdisplay: inline
:http://jsfiddle.net/GolezTrol/4Gs2E/1/
通常这是通过将每个单词包装在一个自己的有边框的
SPAN
中来实现的。Usually that is implemented by wrapping each word in an own
SPAN
which has border.我只是想为自己制作 CSS 动画菜单。我发现的解决方法是将您的 INLINE-BLOCK 元素(如有必要,在 css 中进行更改,出于此解决方案的目的,将其称为具有此类属性的 span)到 block元素。然后我使用 span 的边距,因为它是周围 div 的填充。
例子:
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.
Example:
http://jsfiddle.net/ApbQS/
hope it will help anyone