Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Sticky Menu Bar

Probably you've noticed menu bars that stick at the top of web pages. Sometimes they contain other information or ads instead of menus.

The sticky menu bar, or other content, is published somewhere on the page. Generally, it's near the top. Yet, it also works for content published further down.

When you scroll down and past that menu or other content, the content sticks at the top of the page. The rest of the page scrolls under it.

You may have seen a similar effect within a scrolling div. As the div scrolls, a section of it sticks to the top of the div while the rest scrolls under it.

It's done with the CSS position:sticky; declaration.

Compatibility Notes

The sticky value is a recent addition to the CSS position property.

My latest information tells me Edge desktop doesn't support position:sticky;. Safari desktop requires the -webkit-sticky value, position:-webkit-sticky; being the entire declaration.

Other popular browsers support it.

If not supported, position:sticky; degrades to position:relative; and behaves like a regular div. The div's content will publish in place, but won't be sticky.

Live Examples

The two examples illustrate two ways position:sticky; can work — sticky when a page scrolls and sticky when a div scrolls.

Page Scrolling Sticky

To see the effect of the page scrolling sticky example, scroll down past the example.

The bordered "[menu/other content]" text will be sticky 10 pixels from the top of the browser window while the rest of content scrolls under it.

[menu/other content]

The bordered "[menu/other content]" text is in a div with the CSS to make it sticky. Here is the source code of the entire div.

<div style="
    position:-webkit-sticky;
    position:sticky;
    top:10px;
    border:1px solid #ccc;
    padding:.5em;
    width:100%;
    text-align:center;
    font-size:1.5em;
    font-weight:bold;
    background-color:rgba(255,255,255,0.8);
    z-index:2;">
[menu/other content]
</div>

Only the first three CSS declarations are needed to make sticky content. The rest you see in the example code is optional design for the div.

The position:-webkit-sticky; declaration is for Safari desktop browsers and position:sticky; is for other browsers.

The top:10px; is needed to specify how far from the top edge of the viewing window that the sticky content shall stick at. Without the top property, the content won't be sticky. The top value may be 0 (zero amount) or even a negative amount, like -1em, but must be present.

Div Scrolling Sticky

For the scrolling div sticky example, scroll the div down past the part with the blue background. The part with the blue background will be sticky at the top of the scrolling div while the rest of div content scrolls under it.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. In auctor, odio id sodales.
The sticky content
Integer iaculis nec magna sit amet facilisis. Sed iaculis mauris vitae placerat ultricies. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam laoreet laoreet lobortis. Nunc cursus iaculis rutrum. Phasellus elementum hendrerit mauris, non efficitur quam bibendum ac. Etiam quis eros consectetur, euismod nisi quis, imperdiet risus. Pellentesque in iaculis nunc. Nulla volutpat velit malesuada ipsum lobortis, ut sodales quam eleifend. Morbi tristique sapien maximus magna feugiat ultricies. Cras elit ligula, fringilla id tempus a, gravida sodales leo. Nunc consequat magna massa, eu finibus felis mattis gravida. Aenean lacinia rhoncus sapien, a dapibus nunc congue quis.

Here is the source code for the sticky part.

<div style="
    position:-webkit-sticky;
    position:sticky;
    top:0;
    background-color:blue; 
    color:white;
    text-align:center;
    padding:.5em 0">
The sticky content
</div>

You'll notice the same position:-webkit-sticky; and position:sticky; declarations as were used for the page scrolling sticky.

The top:0; declaration has a value of 0 (zero amount) so it sticks at the very top of the div.

Sticky Content

As the illustrations show, the sticky content isn't restricted to menu bars. Any content can be made sticky.

Sticky content can be stuck to the top of the page or to the top of scrolling divs.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC