设置“位置:固定”的宽度。 div相对于父div [英] Set width of a "Position: fixed" div relative to parent div

查看:440
本文介绍了设置“位置:固定”的宽度。 div相对于父div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给一个div(位置:固定)的宽度为100%(与它的父div相关)。但我有一些问题...

I'm trying to give a div (position: fixed) the width of 100% (relating to it's parent div). But I've got some problems...

编辑:
第一个问题是通过使用inherit,但它仍然不工作。我认为问题是,我使用多个div,采取100%/继承宽度。
您可以在jsfiddle更新中找到第二个问题: http://jsfiddle.net/4bGqF/7/

The first problem is sovled by using inherit, but it still doesn't work. I think the problem is that I'm using multiple divs that take the 100%/inherit width. You can find the second problem on the jsfiddle update: http://jsfiddle.net/4bGqF/7/

Fox示例

#container {
    width: 800px;
}

#fixed {
    position: fixed;
    width: 100%;
}

和html

<div id="container">
    <div id="fixed">Sitename</div>
    <p>
        blaat
    </p>
</div>

或者你可以试试:http://jsfiddle.net/4bGqF/

问题似乎在于固定元素总是占用宽度窗口/文档。有人知道如何解决这个问题吗?

The problems seems to be that the fixed element is always taking the width of the window/document. Does anyone know how the fix this?

我不能给我的固定元素,因为我使用jScrollPane插件。

I can't give my fixed element a fixed with, because I'm using the jScrollPane plugin. It depends on the content whether there's a scrollbar or not.

非常感谢!

PS:文字的2个div是在彼此的顶部。这只是一个例子,所以没关系。

PS: The text of the 2 divs are on top of each other. This is just an example so that doesn't really matter.

推荐答案

我不知道第二个问题是什么(根据你的编辑),但如果你应用 width:inherit 到所有内部div,它的工作原理: http://jsfiddle.net/4bGqF/9/

I´m not sure as to what the second problem is (based on your edit), but if you apply width:inherit to all inner divs, it works: http://jsfiddle.net/4bGqF/9/

您可能想查看一个JavaScript解决方案,支持和不支持 width:inherit

You might want to look into a javascript solution for browsers that you need to support and that don´t support width:inherit

这篇关于设置“位置:固定”的宽度。 div相对于父div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆