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 13 Comments
Rate it    (Rated 3 by 37 people)
11,679 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>

Leave a Comment

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