简体   繁体   English

如何使用 position:fixed 制作一个 div 占据整个父宽度(并随之调整大小)?

[英]How to make a div with position:fixed occupy the entire parent width (and resize with it)?

See this fiddle: http://jsfiddle.net/eEe2C/2/看到这个小提琴: http://jsfiddle.net/eEe2C/2/

The green row 1 is fixed but does not stretch the entire parent width which the other blue rows do.绿色的第 1 行是固定的,但不会像其他蓝色行那样拉伸整个父宽度。 Think of row 1 as the column headers in a grid.将第 1 行视为网格中的列标题。 It needs to be fixed in order to be visible at all times and for all rows to "slide" underneath it when scrolling.需要对其进行修复,以便在任何时候都可见,并且在滚动时所有行都可以在其下方“滑动”。 The only difference between row 1 and the other rows is that row 1 has position:fixed.第 1 行和其他行之间的唯一区别是第 1 行具有 position:fixed。

When resizing horizontally, the "cells" of the blue rows expand just fine according to their width percentage.水平调整大小时,蓝色行的“单元格”会根据它们的宽度百分比很好地扩展。 The "cells" of the green row ignores this (I know why, just need a solution).绿色行的“单元格”忽略了这一点(我知道为什么,只需要一个解决方案)。

How do I get the same stretching behaviour for row 1 still keeping it fixed?如何为第 1 行获得相同的拉伸行为仍然保持它固定? The headers should of course be aligned with the data "cells".标题当然应该与数据“单元格”对齐。 Please, no post-js hack if possible.如果可能,请不要使用 post-js hack。

Edit: can't use tables as I'm using drag'n'drop sorting that doesn't play well at all with tables.编辑:不能使用表格,因为我正在使用拖放排序,这对表格根本不起作用。

If I'm understanding it correctly, do you mean like this?如果我理解正确,你的意思是这样吗? Although you might want to set a specific width on the container.尽管您可能希望在容器上设置特定宽度。

http://jsfiddle.net/eEe2C/6/ http://jsfiddle.net/eEe2C/6/

Even though it's a demo, it looks like tabular data to me so just use a <table> .尽管它是一个演示,但对我来说它看起来像表格数据,所以只需使用<table> Using a table with a fixed height <tbody> might be a good solution to your problem.使用具有固定高度<tbody>的表格可能是解决问题的好方法。

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

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