問(wèn)題描述
我正在嘗試為 div(位置:固定)提供 100% 的寬度(與其父 div 相關(guān)).但是我有一些問(wèn)題......
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...
第一個(gè)問(wèn)題是用inherit解決的,但還是不行.我認(rèn)為問(wèn)題在于我正在使用多個(gè) div 占用 100%/繼承寬度.您可以在 jsfiddle 更新中找到第二個(gè)問(wèn)題:http://jsfiddle.net/4bGqF/7/
狐貍示例
#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/
問(wèn)題似乎是固定元素總是占用窗口/文檔的寬度.有誰(shuí)知道如何解決這個(gè)問(wèn)題?
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?
我不能給我的固定元素一個(gè)固定的,因?yàn)槲艺谑褂?jScrollPane 插件.是否有滾動(dòng)條取決于內(nèi)容.
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 個(gè) div 的文本相互重疊.這只是一個(gè)例子,所以并不重要.
PS: The text of the 2 divs are on top of each other. This is just an example so that doesn't really matter.
推薦答案
我不確定第二個(gè)問(wèn)題是什么(基于您的編輯),但如果您應(yīng)用 width:inherit
對(duì)于所有內(nèi)部 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/
您可能想為需要支持但不支持 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
這篇關(guān)于設(shè)置“位置:固定"的寬度div 相對(duì)于父 div的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!