CSS 问题,100% 宽度不断移出位置
我目前正在这个网站上工作, http://no-illusions.nl 一切都很顺利,但我发现了一件我无法解决的非常愚蠢的事情。
我有一个宽度为 500px 的 div(包装器),里面有不同的包含图像和文本的 div。现在的问题是文本的宽度是动态的,在某些部分它可能是 200px 宽度,在某些页面上它可能是 350px。
所以我将宽度设置为 100%,但现在该部分只是浮在我想要的位置下方!
它应该是这样的 ![它应该是什么样子][1]
但这就是它的样子,
我希望你们能在正确的方向上推动我。
I'm currently working on this website,
http://no-illusions.nl
It's going fine but I found a pretty stupid thing that I can't resolve.
I have an div (wrapper) with an width of 500px and inside I have different div's containing images and text. Now the problem is that the text is dynamic in width, on some parts it could be 200px width and on some pages it could be 350px.
So I put the width on 100% but now the part just floats underneath where I want it!
This is how it should look,
![how it should look][1]
But this is how it looks,
I hope you guys can give me a push in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请记住,相对大小是相对于包含元素的。如果您说
width: 50%
,您将获得一个为其容器宽度 1/2 的元素。所以你说的width: 100%
使文本 div 占据容器宽度的 100%,并且它也变成 500px。您可以将图像放在文本 div 内并使其浮动。有了适当的边距/填充,文本将环绕图像,您会得到类似的内容:
基本的 html 结构将是:
否则您需要使用固定的 div,并根据 动态更改文本 div 的宽度是否存在图像。
if (has image) { width: 350} else { width: 500}
类型的东西。Remember that relative sizes are relative to the containing element. If you say
width: 50%
, you'll get an element that's 1/2 the width of its container. So your sayingwidth: 100%
makes the text div take 100% of the container's width, and it becomes 500px as well.You can put the image inside the text div and float it. With appropriate margins/padding, the text will wrap around the image, and you'd get something like:
The basic html structure woul be:
otherwise you'd need to go with fixed with divs, and dynamically change the text div's width based on whether there's an image present or not.
if (has image) { width: 350} else { width: 500}
type of thing.这曾经困扰过我,我通常通过设置段落的最大宽度来解决它。这应该会导致文本在该宽度内换行。
This has troubled me before and I usually solve it by setting the paragraph's max-width. That should cause the text to wrap within that width.
您可以在 .update_respond 类上设置 330px 的最大宽度。
You can set a max-width of 330px on your .update_respond class.
标签根本不需要任何宽度,因为它们是块元素,会拉伸到可用的最大宽度。
You don't need any width at all for
<p>
-tags, as they are block-elements, which are strechted to the maximum width available.