Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css "fixed" child element positions relative to parent element not to the viewport, why? [duplicate]

I'm developing a wordpress theme, with an isotope grid and where a hover on a post should display its title with a fixed position on the bottom of the browser. I have this simplified structure:

<div id="main">     <article class="hubbub">         //article content     <h2 class="subtitled">         //h2 content     </h2>     </article> </div> 

Via jQuery, a hover on <article> should display its child element h2.subtitled. <article> is positioned relative, but gets an absolute position by the isotope script. The h2.subtitled is positioned fixed with:

.subtitled {             display: none;             position: fixed;             z-index: 999999999;             bottom: 20px;             left: 0;             width: 100%;             font-family: Arial, Helvetica, sans-serif;             font-size: 42px;             text-align: center;             color: yellow; } 

For some reason, the h2.subtitle elements get positioned fixed related to the parent <article> element, so an the bottom of each <article>. If I set the <h2> outside of the <article>, it is positioned fixed related to the browser, but they need to be inside of the <article> element, because an infinite scroll appends the new <article> elements and they should as well contain the <h2> titles.

Does anyone know, how to make this position fixed and related to the browser window?

Thanks!

like image 587
R4ttlesnake Avatar asked Jan 13 '14 13:01

R4ttlesnake


People also ask

Can I position an element fixed relative to parent?

It's now possible in modern browsers to position an element fixed relative to its container. An element that has a transform property acts as the viewport for any of its fixed position child elements.

When using position fixed What will the element always be positioned relative to the viewport?

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.

Why is position fixed not working?

Position fixed doesn't work with transform CSS property. It happens because transform creates a new coordinate system and your position: fixed element becomes fixed to that transformed element. To fix the problem you can remove transform CSS property from the parent element.

How can you set position of a child element with reference to its parent element?

The one key thing to remember when trying to position a child div relative to it's parent is that the child should be given the CSS property position:absolute; and the parent set to either position:absolute; or position:relative;.


2 Answers

FWIW, when I ran into this, the problem turned out to be a parent div with -webkit-transform: translate3d(0, 0, 0) in its CSS. Apparently, this is a known source of potential mayhem in child elements with position: fixed.

For what I was trying to do (turning fixed on and off as a way of sticking a key nav element to the top of the page as it scrolled by), the solution was to append it to the page body element when it was time to hold it in place and sticking it back in its wrapper div when it wasn't. No idea if any of this would have helped the OP, but if you're chasing this bug yourself, worth looking into.

like image 84
BlairHippo Avatar answered Sep 19 '22 07:09

BlairHippo


Remove the transform property from the parent of the fixed element.

For some reason this causes the fixed element to become relative to the parent instead of the document.

Codepen example.

like image 22
Alex Avatar answered Sep 20 '22 07:09

Alex