简体   繁体   English

CSS问题,宽度100%会一直偏离位置

[英]CSS problem, 100% width keeps moving out of position

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. 我目前正在该网站上工作, http://no-illusions.nl一切都很好,但是我发现一个愚蠢的东西我无法解决。

I have an div (wrapper) with an width of 500px and inside I have different div's containing images and text. 我有一个宽度为500px的div(包装器),内部有不同的div,其中包含图像和文本。 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. 现在的问题是,文本的宽度是动态的,在某些部分上可能是200px宽度,在某些页面上可能是350px。

So I put the width on 100% but now the part just floats underneath where I want it! 因此,我将宽度设置为100%,但现在该零件仅漂浮在我想要的位置下面!

This is how it should look, ![how it should look][1] 这就是它的外观!! [它的外观] [1]

But this is how it looks, 但这就是它的样子

I hope you guys can give me a push in the right direction. 我希望你们能给我一个正确的方向。

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. 如果您说width: 50% ,您将得到一个元素为其容器宽度的1/2。 So your saying width: 100% makes the text div take 100% of the container's width, and it becomes 500px as well. 所以您所说的width: 100%使text div占据容器宽度的100%,并且也变为500px。

You can put the image inside the text div and float it. 您可以将图像放在文本div中并使其浮动。 With appropriate margins/padding, the text will wrap around the image, and you'd get something like: 使用适当的边距/填充,文本将环绕图像,并且您将获得类似以下内容的信息:

imgimgimg   text text text 
imgimgimg   text text text
imgimgimg   text text text

text text text text text
text text text text text

The basic html structure woul be: 基本的html结构将是:

<div style="width: 500px;">
    <div style="float: left; margin: 5px;"><img src="..."></div>
    text text text text text
</div>

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. 否则,您将需要使用div固定,并根据是否存在图像来动态更改文本div的宽度。 if (has image) { width: 350} else { width: 500} type of thing. if (has image) { width: 350} else { width: 500}事物类型。

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 p{width: 273px;}

您可以在.update_respond类上将最大宽度设置为330px。

<p> -tag根本不需要任何宽度,因为它们是块元素,被拉伸到最大可用宽度。

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

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