Fluid width fixed position
Asked Answered
S

7

19

Imagine:

<div class="outer">
    <div class="inner">
    </div>
</div>

Where:

  • .outer is part of a column structure, and its width is a percentile and therefore fluid.
  • .inner represents a fixed position element that should fill with a 100% width the .outer element. However its position vertically remains the same, therefore fixed.

I’ve tried to implement this layout with the following CSS:

.outer {
    position: relative;
    width: %;
}
.inner {
    position: fixed;
    width: 100%;
}

However, .inner does not calculate its width as a percentage of its relative parent. Instead it fills the full width of the window/document. Attempting any left or right properties result in the same parent-ignoring qualities.

Is there any way around this?

Sarnoff answered 3/9, 2012 at 20:42 Comment(10)
position: fixed is always and always "relative" to the browser window/view port and never "relative" to a positioned (absolute or relative) parent/ancestor.Dickman
"Is there any way around this?"Sarnoff
#7846661Dickman
#6976548Dickman
#5874065Dickman
#6794500Dickman
#11431858Dickman
The third solution of @Dickman is actually brilliant !Sigurd
I also used @Dickman 3rd answer ( #5874065 )Collocate
just because you want an element to have a non changing height / vertical position does mean that the element has to be marked as fixed. the fiddle provided by @Ilan Biala clearly demonstrates a working exampleDatestamp
W
7
.outer {
    position: relative;
    width: %;
}
.inner {
    position: fixed;
    width: inherit;
}

That should do the trick.

Wassail answered 8/10, 2014 at 20:39 Comment(1)
Was having this issue, and this actually solved it. For reference, the inherit property is inheriting the width set by its nearest parent.Ecclesiolatry
A
4

position: fixed is always relative to the window/browser, thus it cannot be used to solve your problem. Fixed positioning removes the element from the natural order of the DOM, and thus does not remain within your outer div anymore, hence why it takes the full width of the browser and not of your container. What you need to use is position: absolute to place .inner relative to .outer. You'll be able to position your element as well as have its width be contained by the .outer div.

Akerley answered 20/1, 2014 at 8:53 Comment(0)
D
3

Use this :

.inner { position: fixed; left:0; right:0;

}

Dextral answered 10/7, 2015 at 10:1 Comment(1)
Left and right here are in relation to the document, not the container div as is desired though?Raffarty
S
1

Fixed elements take only absolute values as width. If your parent container is fluid (width is a percentage), you need to set the width of the fixed element dynamically. You need to get the width of the wrapping container and set it on the sticky element.

CSS

.outer {width: 25%;}
.inner {position: fixed;}

JS

var fixedWidth = $('.outer').css('width');
$('.inner').css('width', fixedWidth);

Additionally, you can add an event listener in case window resizes.

JS

window.addEventListener('resize', resize);
function resize() {
    var fixedWidth = $('.outer').css('width');
    $('.inner').css('width', fixedWidth);
}
Secrete answered 20/6, 2016 at 0:53 Comment(0)
W
0

You can take a look at this jsfiddle that i made that illustrates the fix for your problem, you can use this code exactly as it does what you want.

Waterer answered 3/9, 2012 at 22:31 Comment(2)
How is your JSFiddle related to position:fixed?Oosperm
I gave a fix, I didn't say it relates to position:fixed;. It's related to his problem and my answer proposes a solution.Waterer
B
0

position:fixed is always relative to viewport/browser window, not to ancestors.

Banna answered 28/1, 2014 at 6:34 Comment(1)
This doesn't hold anymore once transform is applied in modern browsers...Pasol
F
0

What about using something like this fiddle ?

.outer {
    width: 20%;
    height: 1200px;
    margin-left: 5%;
    float: left;
}
.inner {
    height: 200px;
    position: fixed;
    top: 0;
    background: orange;
    right: 75%;
    left: 5%;
}
Fulbright answered 8/2, 2014 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.