Introduction To Fixed Positioning With CSS
Normally if we have a web page with long content web browser displays scroll bars to navigate to the content which is not displayed initially. Then if we scroll down, bottom content will be displayed and all the navigation menu bars and content on the top will not be displayed. But Once an element is fixed to the web page no matter how long we scroll, it will stay in the same position as before. Lets move onto creating a fixed positioned element.
Fixed Menus Demo
How to Create a Fixed Positioned Element
.fixed_elements{ position:fixed; top:100px; }
Creating a Fixed Navigation Menu Bar
CSS lets you position content on your page in a variety of ways, one of which is "fixed". In this mode a piece of content remains completely static without so much as a flinch when the rest of the page is scrolled.
Add more content to div with the id “container” until scroll appears in the web page. Then scroll down and you will see that navigation menu will displayed in a fixed position without affected by scrolling.
Creating a Fixed Footer Bar with CSS and Jquery
CSS lets you position content on your page in a variety of ways, CSS lets you position content on your page in a variety of ways, one of which is "fixed".
Common css code is not included in this code. If you are trying this example add the css code from the previous example or use the source files given in the download section.
- First I have called a js function on body onload method.
- We have to get the window height to calculate the top position for the footer.
- Then we have to reduce the footer height from window height.
- In this example footer height is 25px and top and bottom padding of 20px each which makes 65px of total footer height..
- Then we can assign the top value to the footer bar which makes the footer stay on the bottom of the screen on scroll.
Creating a Fixed Side Bar Navigation
CSS lets you position content on your page in a variety of ways, one of which is "fixed".
- First I have called a js function on body onload method.
- We have to get the window height to calculate the top position for the sidebar. Since sidebar should be positioned in the center of the browser window
we have to find the coordinates of center. - Then we have divide the window height by 2 and reduce x amount of pixels depending on your menu height.
- Then we can assign the top value to the sidebar bar which makes the sidebar stay on the center of the screen on scroll.
Positioning Fixed Elements on Browser Window Resize
$(window).scroll(function () { var height = $(window).height(); var top_start = (height/2) - 100; $('#menu-bar').css('top', top_start+"px"); $('#footer-bar').css('top', (height-65)+"px"); });
Now resize or change the resolution of browser window. Sidebar will not position properly and footer will get hidden. Then start scrolling and you will see the sidebar aligning properly and footer is displayed in the bottom again.
Fixed Position Examples Download
Download EBook Version
Download
Leave a Reply