/    Sign up×
Community /Pin to ProfileBookmark

Fixed Position

I have a container DIV on my webpage, inside that another content DIV with loads of text information.

My first DIV is position relative. Second was absolute and a help icon DIV was fixed on the top right corner.

However I want the help icon to slide down the page, so I moved it to position fixed.

However it now gets placed top right of the page not the parent container and moves down the page. Is this normal?

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumApr 02.2021 — This can be achieved by a few lines of javascript:
``<i>
</i> window.addEventListener('scroll', event =&gt; {
const rect = document.getElementById('container').getBoundingClientRect();
// if the help icon has disappeard to the top:
// shift it down:
if (rect.top &lt; 0) {
document.getElementById('help-icon').style.transform = 'translateY(' + (-rect.top) + 'px)';
} else {
document.getElementById('help-icon').style.transform = 'translateY(0px)';
}
});<i>

</i>
`</CODE>
'container' ist the ID of the second container 'help-icon' is the ID of the help icon.

This can be done by CSS and <C>
position: sticky;</C> either:
<CODE>
`<i>
</i> #help-icon {
float: right;
position: sticky;
top: 0;
}<i>
</i>
``

but as I'm more of a javascript guy I implemented the other solution first.
Copy linkTweet thisAlerts:
@VITSUSAApr 03.2021 — @kiwis80#1629871 Hope you got the solution?
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...