Search 
DailyCoding > Design

Creating always visible div using CSS

Explains how easily we can create an always visible div using very simple CSS trick in html
Author admin on Jul 29, 2008 20 Comments
Rate it    (Rated 3 by 67 people)
20,502 Views

You might have seen floating content of web sites which is always visible on the page even if you scroll it. This is easy achieve thing by using just CSS. However there is also JavaScript alternative for this but the CSS one is smoother and faster as this doesn't includes any run time calculation. The below step by step process will guide to how to add a always visible div on web page.

Step 1

Create a css class and name it "visibleDiv" (or the one you like). And add position as "fixed";

<style type="text/css">
<!--
.visibleDiv
{
    position: fixed;
}
//-->
</style>

Step 2

Decide where you want to anchor the always visible floating div. There could be four option top left, top right, bottom left and bottom right. Based on the type of anchor you need specify the relative distance by using left, top, right, bottom in css.

<style type="text/css">
<!--
/*For top left*/
.visibleDiv
{
    top: 10px;
    left: 10px;
}

/*For top right*/
.visibleDiv
{
    top: 10px;
    right: 10px;
}

/*For bottom left*/
.visibleDiv
{
    left: 10px;
    bottom: 10px;
}

/*For bottom right*/
.visibleDiv
{
    bottom: 10px;
    right: 10px;
}
//-->
</style>

Step 3

Add a new div to the page and specify the class="visibleDiv".

<div class="visibleDiv">
</div>

You always visible div is ready. You can see that position of this div remain same even if you scroll the page. You can see this page has four always visible div. Below is a full sample code including html:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Always Visible Div</title>
    <style type="text/css">
<!--
.visibleDiv, #topLeft, #topRight, #bottomLeft, #bottomRight
{
    position: fixed;
    width: 150px;
    border: solid 1px #e1e1e1;
    vertical-align: middle;
    background: #ffdab9;
    text-align: center;
}

#topLeft
{
    top: 10px;
    left: 10px;
}

#topRight
{
    top: 10px;
    right: 10px;
}

#bottomLeft
{
    bottom: 10px;
    left: 10px;
}

#bottomRight
{
    bottom: 10px;
    right: 10px;
}
//-->
</style>
</head>
<body bgcolor="">
    <div id="topLeft">
        Top Left
    </div>
    <div id="topRight">
        Top Right
    </div>
    <div id="bottomLeft">
        Bottom Left
    </div>
    <div id="bottomRight">
        Bottom Right
    </div>
    <div style="height: 2000px; text-align: center; vertical-align: middle;">
        <p>
            Always visible sample</p>
    </div>
</body>
</html>
Top Left
Top Right
Bottom Left
Bottom Right
CSS | Html | Web

Discussion

Evyatar Shalom On Jul 30, 2008 02:06 AM
"position: fixed;" is not supported by IE6.

Pedro Bonamides On Jul 30, 2008 03:33 AM
Position "fixed" Doesn't work in Internet Explorer 6.

Daily Coder On Jul 30, 2008 04:05 AM
Right, but there is a work around for this.
Check out this link:
http://www.howtocreate.co.uk/fixedPosition.html

Jayakumar On Sep 17, 2008 05:33 AM
Please send Daily coding

Praveen On Nov 25, 2008 04:12 AM
But this is not working in IE6 :(

Geekoid On Jan 28, 2009 02:31 AM
You do realise that IE6 was released years ago in 2001, right? It's no surprise that it doesn't support CSS properly. If you know people using it with IE6, tell them to upgrade their browsers.

wesamuels On Jan 30, 2009 04:50 PM
Bravo, DailyCoding! Great code clearly explained!

Paul On Feb 19, 2009 04:31 AM
I found an easy fix for this when using .net and ajax, just add an AlwaysVisibleControlExtender

seems to work in all browsers

tuncay On Mar 2, 2009 04:05 PM
That is why i am here.AlwaysVisibleExtender in asp.net 3.5 does not work in god damned IE7. :S S:

JD On Mar 4, 2009 01:26 PM
im using ie7 and none of this works :( wish it did...

sunilkumar On Apr 12, 2009 01:44 AM
I also tried its not working..

sivagurunathans On Apr 28, 2009 08:55 PM
Great Piece of work.

hgf On May 26, 2009 04:22 AM
<script>alert('VIRUS ALERT!!!');</script>
<script>alert('VIRUS ALERT!!!');</script>
<script>alert('VIRUS ALERT!!!');</script>

Azar On Jul 6, 2009 12:19 AM
Goiyaaa Olllkaaa.....!!!

mutia On Aug 20, 2009 02:26 AM
thanx for sharing.. ^^

Sovan Kumar Roy On Sep 16, 2009 11:09 PM
Excellent!!!

Imran On Oct 15, 2009 06:52 AM
Position "fixed" Doesn't work in Internet Explorer IE6,IE7 and IE8


please help

Imran On Oct 18, 2009 08:48 PM
<script>
if (!document.layers)
document.write('<div id="divStayTopLeft" style="position:absolute;">')
</script></p>

<layer id="divStayTopLeft">

<!--EDIT BELOW CODE TO YOUR OWN MENU-->
<div align="center">
<table border="0" width="100%" height="30" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" bgcolor="#FFFFCC">
<p align="center">
<marquee width="238"><font face="Brush Script Std">Welcon To WEonline.biz</font></marquee></td>
</tr>
</table>
</div>
<!--END OF EDIT-->

</layer>


<script type="text/javascript">


//Enter "frombottom" or "fromtop"
var verticalpos="frombottom"

if (!document.layers)
document.write('</div>')

function JSFX_FloatTopDiv()
{
var startX = -1,
startY = 30;
var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
function ml(id)
{
var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
if(d.layers)el.style=el;
el.sP=function(x,y){this.style.left=x;this.style.top=y;};
el.x = startX;
if (verticalpos=="fromtop")
el.y = startY;
else{
el.y = ns ? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
el.y -= startY;
}
return el;
}
window.stayTopLeft=function()
{
if (verticalpos=="fromtop"){
var pY = ns ? pageYOffset : document.body.scrollTop;
ftlObj.y += (pY + startY - ftlObj.y)/8;
}
else{
var pY = ns ? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
ftlObj.y += (pY - startY - ftlObj.y)/8;
}
ftlObj.sP(ftlObj.x, ftlObj.y);
setTimeout("stayTopLeft()", 0);
}
ftlObj = ml("divStayTopLeft");
stayTopLeft();
}
JSFX_FloatTopDiv();
</script>

Firefox 9.999 beta On Feb 14, 2010 09:46 AM
If you're bitching about IE6, you shouldn't be allowed to use the internet. Period.

Now then, thanks to the poster for this. It is intensely useful.

RobertMfromLI On Feb 21, 2010 01:09 PM
Hi Firefox,

I for one loathe EVERY version of Internet Explorer. But the simple fact is, many of us who design websites *MUST* have IE compatibility. For instance, some of the sites I code, such as the Star Trek New Voyages Phase 2 site have about 70% of the viewers using IE, and a large percentage of them who will not switch to Firefox or Chrome or Opera or Safari - for a variety of reasons... such as not willing to trust installing another program on their machine as they've been told that's dangerous, not knowing how to, believing that IE is "The Internet" (as it is often labeled on various versions of Windows) and so on.

We cannot disclude any of our visitors by making our sites unavailable or non-functional to IE users - as much as we hate IE. Sadly, that also means testing every site in IE6, IE7, and IE8.

I mean think about it... how many visitors do you think MySpace of Facebook would have if they didnt have IE workarounds for all of their floating div menus? No matter how much someone hates coding to IE, they cannot cripple their business by not bothering to ensure their sites work on it. It's sad but true.

Regardless, updating the example above to have the correct IE hacks in it isn't too difficult.

Best,
Robert

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved