简体   繁体   English

CSS 溢出:滚动导致文本顶部出现额外的换行符

[英]CSS overflow:scroll causing an extra line break at the top of the text

I'm trying to create a div section with an image and text inside it, with scrolling overflow, but when I add the overflow:scroll, an extra line break is added at the top of the text, and I can't figure out how to get rid of it.我正在尝试创建一个带有图像和文本的 div 部分,并带有滚动溢出,但是当我添加溢出:滚动时,文本顶部添加了一个额外的换行符,我无法弄清楚如何摆脱它。

I know it isn't the padding or margin that's the problem, because the image is still positioned at the top.我知道问题不是填充或边距,因为图像仍然位于顶部。 The text just has extra space.文本只是有额外的空间。 And removing just the overflow:scroll line gets rid of it, so I can't figure out why it's happening.仅删除溢出:滚动行即可摆脱它,所以我无法弄清楚为什么会发生这种情况。

<div class="scroll">
<image class="avatar" src="(link here)"><p>text here</p>
</div>

.scroll {
height: 160px;
overflow: scroll;
}

img.avatar {
height: 50px;
width: 50px;
padding: 0px 6px 1px 0px;
float: left;
}


Any ideas?有任何想法吗?

It is indeed the margin of the p tag which is the problem here.确实是p标签的边距是这里的问题。

You may reproduce the behaviour you mentioned on other ways, eg, using a border around the .scroll container.您可以通过其他方式重现您提到的行为,例如,在.scroll容器周围使用border The margin of the p tag is measured against the next element (in a minimal example, to the top of the page). p标记的边距是针对下一个元素(在一个最小的示例中,到页面顶部)进行测量的。 If you set up a border or scrollbars, the margin is instead measured against the container itself, so it moves down.如果您设置边框或滚动条,则边距是根据容器本身测量的,因此它会向下移动。

To prohibit that behaviour, you could, for example, set a margin of 0 to the p tag.例如,要禁止这种行为,您可以将p标记的边距设置为 0。

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

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