demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my projects

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

CSS Positioning: fixed

css / positioning

Estimated reading time: 1 minute, 35 seconds

LEVI'S

This is an example of an element with position: fixed; applied to it, for this blog's article on the CSS property.

As background-attachment: fixed is to background images, position: fixed is to elements: it allows an element to be stuck in position relative to the page, allowing the rest of the content to scroll past it. It is usually (but not exclusively) applied to container elements such as div, and is part of the “Web 2.0” aesthetic: fixed headers and footers being a popular example. As with elements positioned absolute, ordinary static content will scroll underneath any fixed content.

One example is a “pull tab” on the side of this page, which keeps content out of the way until needed. (I've added a CSS3 effect for modern browsers.)

While I will leave the interactivity of such a feature to another article, positioning the tab itself is very easy. The markup would be:

<div id=fixed-pull-tab>
<img src="red-tab.png" style=”float: right” alt=”LEVI’S”>
<p>This is an example of an element with <code>position: fixed;</code> applied to it, for this blog’s article on the CSS property.</p>
</div>

And the associated CSS – I have used box-shadow on the image to emphasize the “layer above” nature of the fixed position div:

div#fixed-pull-tab { width: 300px; border: 1px solid #000; padding-left: 1em; background-color: rgba(255, 255, 37, 0.7); position: fixed; left: -265px; top: 200px; }
div#fixed-pull-tab p { margin-right: 70px; }
div#fixed-pull-tab img { box-shadow: 4px 4px 4px rgba(0, 0, 0, 0.33); }
div#fixed-pull-tab:hover { left: 0; }

As with position: absolute, position: fixed should be used carefully: the property makes it all too easy to create web page features that overlap and obscure page content.

comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.