如何解决位置:固定元素未扩展到其父宽度的常见问题?
看看这个小提琴: http://jsfiddle.net/h4VS7/
如何制作黄色元素无论窗口大小如何调整,都与灰色背景对齐(水平)?我不相信CSS不能做到这一点。是的,js hacks 和 Scroll Follow 插件可以工作,但有滞后。
请问有人吗?
编辑:
找到解决方案。如果容器边距表示为百分比,则内容部分可以表示为剩余百分比。请参阅此处: http://jsfiddle.net/h4VS7/1/
虽然不确定为什么它不完美对齐。我应该这么想。可能与 jsfiddle 边距/填充相关。
Have a look at this fiddle: http://jsfiddle.net/h4VS7/
How do I make the yellow element align (horz) with the grey background no matter how the window is resized? I refuse to believe it can't be done with css. Yes, js hacks and Scroll Follow plugin works but lags.
Please, anyone?
Edit:
Found a solution. If the container margins are expressed as percentages the content part can be expressed as the remainder percentage. See here: http://jsfiddle.net/h4VS7/1/
Though not sure why it doesn't align perfectly. It should I think. Could be jsfiddle margin/padding related.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不介意添加额外的元素来包装
.top
,这并不是特别困难:http: //jsfiddle.net/Ud3ZQ/
而且,解决方案的正确对齐(好吧,几乎)版本:
http://jsfiddle.net/h4VS7/3/
问题是 jsFiddle 加载 http://fiddle.jshell.net/css/result-light.css:
任何比
*
更具体的内容(包括body
),因此,无论* {padding:0; 如何,都应用了
padding
边距:0}It's not particularly difficult if you don't mind adding an extra element to wrap
.top
:http://jsfiddle.net/Ud3ZQ/
And also, a properly aligning (well, almost) version of your solution:
http://jsfiddle.net/h4VS7/3/
The problem was that jsFiddle loads http://fiddle.jshell.net/css/result-light.css:
Anything is more specific than
*
(includingbody
), so thepadding
was being applied, regardless of* {padding:0; margin:0}