将元素的宽度设置为兄弟元素的组合宽度?
流体容器内有三个元件,其配置如下:
+-----------------+
| +-----+ +-----+ |
| | A | | B | |
| +-----+ +-----+ |
| +-------------+ |
| | C | |
| +-------------+ |
+-----------------+
元件 A 和 B 的宽度为 50%。元素C的宽度为100%。您可以在此处查看实例。
当容器的宽度为奇数个像素时,由于计算元素 A 和 B 的宽度时进行舍入,行的右边缘不会对齐。您可以在调整框架大小时观察边缘未对齐和重新对齐。当顶行包含两个以上元素时,例如 10,效果会加剧。
相反,我希望元素 C 的宽度等于元素 A 和 B 的实际宽度之和,而不是百分比宽度之和。这是否可以在不使用 JavaScript 或求助于表格的情况下实现?
Three elements are inside a fluid container in the following configuration:
+-----------------+
| +-----+ +-----+ |
| | A | | B | |
| +-----+ +-----+ |
| +-------------+ |
| | C | |
| +-------------+ |
+-----------------+
The width of elements A and B is 50%. The width of element C is 100%. You can see a live example here.
The right edges of the rows do not align when the width of the container is an odd number of pixels, due to rounding when computing the width of elements A and B. You can watch the edges misalign and realign while resizing the frame. The effect is exacerbated when the top row contains many more than two elements, say ten.
Instead, I would like the width of element C to be equal to the combined actual width of elements A and B, rather than the combined percentage width. Is this possible without using JavaScript or resorting to a table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是使第一个元素向左浮动,为其提供所需的宽度。右边的不会浮动,并且有一个默认宽度,允许它拉伸,填充左边留下的空间。请参阅对示例的修改以了解其工作原理: http://jsfiddle.net/QDVeD/12/。基本原理可以总结为:
不过,对于两个以上的项目,这不会像您可能希望的那样扩展。最后一个元素通常比其他元素稍长: http://jsfiddle.net/QDVeD/18/。
One solution would be to make the first element float left, giving it the desired width. The right one would not float, and have a default width, allowing it to stretch, filling the space left by the left. See a modification of your example for how it might work: http://jsfiddle.net/QDVeD/12/. The basics can be summed up with:
This wouldn't scale as well as you might like for more items than two, though. The last element will usually be slightly longer than the others: http://jsfiddle.net/QDVeD/18/.