CSS:浮动 div 和负边距
因此,我想要一个与 IE 兼容的居中、恒定宽度、三列布局,并且其列将拉伸到所有相同的高度(等于内容最高的列的高度)。我知道——继续做梦吧?
好吧,我几乎想通了。将我在 Stack Overflow 上找到的技术与用于修复宽度问题的漂亮 CSS hack 和用于居中的文本对齐技巧相结合,以及尝试不同的边距/填充值后,我有了一个近乎完美的布局。我正在为朋友的阿姨做这个,你可以在 www.allfourseasonslawncare.com/index.php 上看到我到目前为止所拥有的内容(index.html 是她的原始网站,她花了 500 美元为她制作。我是使用 CSS 重新创建页面)
我现在遇到的唯一错误是,在 IE 中,左列有大约 30 像素的左边距,并且左列内的所有元素都有第二个 30 像素的左边距。
我可以使用 html>body 技巧轻松解决此问题,为 IE 提供更大的负边距,但我试图找出这个神奇的边距从何而来,以便我可以寻找另一个解决方案。有什么想法吗?
So I wanted a centered, constant width, three-column layout that was compatible with IE and whose columns would stretch to all be of equal height (equal to the height of whichever column had the tallest content). I know- keep dreaming, right?
Well I almost figured it out. After combining the techniques I found here on Stack Overflow with a nifty CSS hack for fixing width issues and the text-align trick for centering, as well as experimenting with different margin/padding values, I have a nearly perfect layout. I'm doing this for a friend's aunt and you can see what I have so far at www.allfourseasonslawncare.com/index.php (The index.html is her original site which she paid someone $500 to make for her. I'm re-creating the page using CSS)
The only bug I'm getting now is that in IE the left column has about a 30 pixel left margin, and all elements inside of the left column have a second 30 pixel left margin.
I can easily fix this using the html>body trick to give a larger negative margin to IE, but I'm trying to figure out where this magical margin is coming from so I can look for another solution. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是因为你的 ul 元素有 30px 的边距。尝试向每个列表项添加一个跨度,然后添加填充。
另外,关于布局,您不需要为每列添加边距(我看到您在 .leftRail、.rightRail 等上有边距)。我注意到您的 .columnWrapper 元素只有 615px 宽,因此您使用负边距来移动这些元素。
相反,您可以使用clearfix hack来有效地浮动元素。 (CSS 的链接如下,将其粘贴到 CSS 文件的底部)
在 css 中放置clearfix 后,转到包装所有列的包装器 div,然后简单地放置一个 class="clearfix"在它上面。
然后,您只需将每一列向左浮动(将 .rightRail 向右浮动),指定宽度即可!它应该是一个 100% 有效、不那么麻烦的解决方案。
当你破解太多时,IE 往往会出现错误。如果您需要任何帮助,请评论。
Clearfix: http://gist.github.com/550114
It might be because your ul elements have a 30px margin. Try adding a span to each list item and then add the padding.
Also, about the layout, you shouldn't need to add margin to each column (I see you have margin on .leftRail, .rightRail, etc). I noticed your .columnWrapper element is only 615px wide, thus you're using negative margin to move these elements.
Instead, you can use a clearfix hack to effectively float elements. (Link for the CSS is below, paste it into the bottom of your CSS file)
After you placed the clearfix in your css, then you go to your wrapper div that is wrapping all of the columns and simply place a class="clearfix" on it.
Then, you just float each column left (float the .rightRail right), specify widths and voila! It should be a 100% working, less-hacky solution.
When you hack too much, IE tends to bug out. Comment if you need any help.
Clearfix: http://gist.github.com/550114
所以我昨天花了一两个小时来解决这个问题,今天早上我又花了一个小时(当我开始接近我自己的问题的评论时发布更新),我终于弄清楚了。
在 Internet Explorer 中,“width: 100%”命令被解释为父元素的整个宽度,而不是宽度减去内边距。这使得表格的宽度为 615 像素(中心列的宽度)。考虑到这会延伸到容器之外,Internet Explorer 仅考虑左侧填充,并且表格将超出列右侧 15 像素(左侧填充的大小)。
由于它延伸到列之外,因此以 Internet Explorer 看来合乎逻辑的方式重新排列了所有浮动元素。也就是说,由于某种不正当的原因,将左轨移动了右侧填充(15 个像素)到左侧填充的右侧的距离(中心列中的 15 个像素),从而将其总共移动了30 像素进入中心列,或者神奇的 30 像素边距。
解决方案是什么?我还在研究这个。作为实验,我将填充从 #content div 移至 #center div,这导致了 Chrome 中出现问题,同时在 Internet Explorer 中解决了该问题。不过,至少主要问题得到了回答。我们现在知道神奇的 30 像素边距从何而来。作为一个快速解决方案,我在 #content div 上设置了 585 像素的恒定宽度,并为其指定了边距而不是填充。
So I spent an hour or two yesterday wrestling with it and I spent another hour this morning (posting updates as I started to get closer as comments to my own question) and I've finally figured it out.
In Internet Explorer, the "width: 100%" command is interpreted as the entire width of the parent element, not the width minus padding. This was giving the table a width of 615 pixels (the width of the center column). Seeing that this would stretch beyond the container, Internet Explorer respected only the left padding and the table stretched 15 pixels (the size of the left padding) beyond the right side of the column.
Since it stretched outside of the column, this re-arranged all floating elements in a way that to Internet Explorer seemed logical. That being, for some ungodly reason, moving the left rail the distance of the right padding (15 pixels) to the right of the left padding (15 pixels into the center column), thus moving it a total of 30 pixels into the center column, or the magical 30 pixel margin.
The solution? I'm still working on this. I moved the padding from the #content div to the #center div as an experiment and this caused the issue to arise in Chrome while simultaneously resolving it in Internet Explorer. At least the primary question was answered, though. We now know where the magical 30 pixel margin was coming from. As a quick solution I've set a constant width on the #content div of 585 pixels and given it margins instead of padding.