CSS:div 的文本内容溢出到填充中是否正确?
我预计 div 内的填充不会包含任何文本。但是给定以下 html/css,内容文本会溢出到填充中;
<div class="foo">helloworld</div>
.foo {
float: left;
overflow: hidden;
background: red;
padding-right: 10px;
width: 50px;
border: 1px solid green;
}
文本溢出其 50 像素大小并进入 10 像素内边距。这是设计使然吗?如果是这样,那看起来很愚蠢——如果里面有东西的话,填充就不是填充了!或者我只是做错了什么?
问候,CSS 新手。
I expected that the padding inside a div would remain clear of any text. But given the following html/css, the content-text spills out into the padding;
<div class="foo">helloworld</div>
.foo {
float: left;
overflow: hidden;
background: red;
padding-right: 10px;
width: 50px;
border: 1px solid green;
}
The text overflows it's 50px size and into the 10px padding. Is that by design? If so it seems pretty dumb - padding isn't padding if it's got stuff in it! Or am I just doing something wrong?
Regards, CSS newbie.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是正确的行为。
overflow:hidden
将剪辑超出框范围的内容。填充位于框内,因此如果需要,内容会很高兴地溢出到该空间中。(source)
要获得您似乎想要的效果,一解决方案是将您的 div.foo 包装在另一个 div 中,并在该 div 上设置背景,如下所示:
This is the correct behavior.
overflow: hidden
will clip content that extends outside the box. Padding is inside the box, so content will happily overflow into that space if necessary.(source)
To get the effect you seem to be aiming for, one solution is wrap your div.foo in an another div and set the background on that one instead, like this:
为此,我创建了两个 div:一个主要的,一个包装的。我最终定义了内部主 div 的高度并隐藏溢出,它解决了问题。代码如下:
包装器具有填充和边框(以及其他属性)。 main 具有高度和溢出属性 - 这些是解决问题的方法。随意测试一下,您会发现无论向主 div 添加多少单词,它们都不会部分显示,或者根本不会显示。跨浏览器也是如此。
To do this, I created two divs: one main one, and one wrapper. I ended up defining a height for the inner main div and hiding overflow, and it solved the problem. Here is the code:
The wrapper has the padding and the border (among other attributes). The main has a height and the overflow attribute - these are the ones that solve the problem. Feel free to test and you'll see that no matter how many words are added to the main div, they will not be shown partially, or at all. Cross-browser, too.
这是因为您将 div 的宽度限制为 50px,导致文本溢出到填充中。删除该宽度语句,div 将根据其中 txt 的大小进行扩展和收缩。
希望对您有帮助。
It is because you have constrained the width of the div to 50px causing the text to spill into the padding. Remove that width statement and the div will expand and contract with the size of the txt within it.
Hope that helps you.
我能看到这个工作的唯一方法是摆脱宽度:50px...除了我难倒的!?
The only way i could see this working is by getting rid of the width: 50px...other than that i stumped!?
另一种方法是使用右轮廓作为伪填充。
首先将元素的宽度减少 10 像素(以考虑轮廓向前延伸的额外量)。
然后向您的元素添加一个 10 像素的红色实心轮廓。轮廓将覆盖任何“隐藏”文本。
如果有任何元素出现在 foo 的右侧,请确保在其右边距中添加 10px(因为轮廓不会像正常宽度扩展那样将它们推到一边)。
Another approach is to use an outline-right as a pseudo-padding.
First reduce the width of your element by 10px (to account for the extra amount the outline will extend forth).
Then add a 10px solid red outline-right to your element. The outline will cover any 'hidden' text.
If there are any elements that appear to the right of foo, be sure to add 10px to its right margin (because the outline will not push them aside like a normal width extension would).
我刚刚也遇到了这个问题,并且不喜欢它的工作方式。无论猫有多大,填充物始终位于它和盒子之间!因此,当使用overflow:hidden时,内容到达padding时应该被隐藏。
如果您想要边框,这里有一个不起作用的技巧,但可能对某些人(我)有用:使用边框作为填充。
I just ran into this issue as well and don't like the way it works. No matter how large the cat gets, the padding will always be between it and the box! Therefore, when using overflow: hidden, the content should be hidden when it reaches the padding.
Here's a hack that doesn't work in case you want a border, but might for some (me): use the borders as padding.
这是设计使然,因为
overflow:hidden
使用边框作为其剪辑,因此内容将流入填充中。您可以在此处使用与背景颜色相同的框阴影来生成一些文本不会流入的假填充。
您必须调整边距和填充来解决这个问题,但这是迄今为止我发现的最轻松的解决方案。
It is by design as
overflow: hidden
uses the border as its clip so contents will flow into the padding.You can use a box-shadow here that is the same color as your background to generate some fake padding that the text won't flow into.
You'll have to adjust margins and padding to account for this but its the most painless solution I've found so far.