CSS - 绝对定位还是浮动?

发布于 2024-08-20 18:05:04 字数 290 浏览 4 评论 0原文

我有一个基本布局,其中 body div 设置为特定宽度。并将左右边距设置为自动。导致div居中。

现在,当要定位这个 div 中的所有子元素时,最好使用绝对定位还是固定坐标?这些子元素基本上是文本列、一些图像和一些标题。我发现将它们全部浮动到左侧会使它们堆叠在一起。这基本上就是我想要的。但是指定坐标会更好吗?你看,我在 IE 中遇到了一个常见的小边距错误。我知道绝对定位可以解决这个问题。但这会是一个肮脏的修复吗?

我的意思是,布局本身在某种意义上是流动的。但 body div 有其宽度。因此,无论分辨率如何,子元素都不会移动。

I've got a basic layout where the body div is set to a specific width. And with margins left and right are set to auto. Resulting in the div being centered.

Now, when it now comes to positioning all sub elements within this div, is it best to use absolute positioning or fixed coordinates? These sub elements are basically columns of text, some images and a few headings. I've found that floating all of them to the left makes them stack behind each other. Which is basically what I want. But would it be better to specify the coordinates? I ran into a small common margin bug with IE you see. And I know absolute positioning would fix it. But would that be a dirty fix?

I mean, the layout itself is liquid in some sense. But the body div has its width. So the sub elements won't move around regardless of resolution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不放开 2024-08-27 18:05:04

注意:IE 中的边距错误也可以通过将带有边距的 div 包裹在没有边距的 div 中并浮动该外部 div 来“修复”。

至于绝对与浮动:我建议您查看此页面,其中似乎是一个很好的总结,问问自己是否有任何缺点对您提出了危险信号。

如果所有 div 内容都是静态的,我个人会使用绝对定位,如果某些内容是动态的,我会使用浮动定位。但这是一个主观答案。

Note: the margin bug in IE can also be "fixed" by wrapping the div with a margin in a div without a margin, and floating that outer div.

As to absolute vs. float: I suggest you have a look at this page, which seems like a good summary, and ask yourself if any of the cons raise a red flag for you.

I personally would use absolute positioning if all the div's contents are static, and float if some are dynamic. But that's a subjective answer.

鹿港小镇 2024-08-27 18:05:04

我认为使用绝对定位没有问题。一个可能的缺点是您的样式表会更难维护,因为您需要指定宽度和位置。如果您将来必须更改任何列的宽度,则必须更新 2 个位置的像素值。 IMO 使用浮动和保证金将来会更容易修改。

I don't see a problem with using absolute positioning. One possible downside would be that your style sheet would be a little harder to maintain, because you would have widths and positions specified. If you had to change the width of any of your columns in the future, you would have to update the pixel value in 2 places. IMO using float and margin would be easier to modify in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文