I'm trying to give a fixed element a width
of a percentage parent (here #container
). When I use pixel instead of the percentage then it works. How can I do it? Is this possible with CSS?
HTML
<div id="outer">
<div id="container">
<div id="fixed">
Sitename
</div>
</div>
</div>
CSS
#outer{
width:300px;
border: 1px solid yellow;
}
#container {
width: 90%; /*When I use e.g 250 px it works. But I need it in percentage*/
border: 1px solid red;
height: 300px;
}
#fixed {
position: fixed;
width: inherit;
border: 1px solid green;
}
Add:
I need position:fixed
. Because I want to place it at the bottom of the page like this:
Solution with position:relativ
doesn't work for me.
It seems as though the static value (250px) can be propagated through normal inheritance. Whereas when the relative value (90%) is being used, the fixed div has already been taken out-of-flow, and now inheritance-wise, it's parent is the viewport. It seems to me that you're going to have to use good old js.
But, this question is months old now, so it probably doesn't matter either way.
There's a belief that inherited values are 'translated' from relative ones (such as percentages) into absolute ones. Guess what? It's wrong. Here's what MDN says about it:
Now an example. Let's say we have the following structure:
... and the following CSS:
... you'll see this picture:
... meaning that while
#charlie
element has the same height as its#bravo
parent, its width is 50% of its parent. Inherited was a computed value:100px
for height,50%
for width.While this feature might be good or bad, depending on situation, for non-fixed elements, it seems to be definitely hurting the fixed ones. As
50%
value forwidth
property is inherited as is, theused value
for that dimension will be based off the viewport. It's the same withpercentage-using
values, such ascalc(50%)
.You have #outer as width:300px, #container as 90% means 270px, now you have given width:inherit and position:fixed that is ambiguous to browser, so use position:fixed with width:x% for #fixed
set the width of "fixed" to 100%, and give it (let's say) a position: relative instead of fixed. http://jsfiddle.net/J7yE4/