Software, your way.
burger menu icon
WillMaster

WillMasterBlog > CSS

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!

Show/Hide a Content Layer

This post shows how to implement layers that will display content over existing content at the location specified by you.

This is one of three related posts. Each has different features. See this comparison table:

Title/Link Displays on top of or within existing content Location where layer will display
On top Within At natural location Specified in div tag Bottom-right of cursor
Floating Layer At Cursor Position Yes Yes
Show/Hide a Content Layer Yes Yes
Quick 'N Easy Div Show/Hide Yes Yes
Title/Link Displays on top of or within existing content
On top Within
Floating Layer At Cursor Position Yes
Show/Hide a Content Layer Yes
Quick 'N Easy Div Show/Hide Yes
 
Title/Link Location where layer will display
At natural location Specified in div tag Bottom-right of cursor
Floating Layer At Cursor Position Yes
Show/Hide a Content Layer Yes
Quick 'N Easy Div Show/Hide Yes

This is the "Show/Hide a Content Layer" article.

When a link is clicked or a mouse hovers over the link, content will display on top of existing content in a position and size specified in the div's style. A second click or hover will hide the div layer.

Here is the JavaScript that will be used to show/hide the floating div layer. The JavaScript is copy 'n paste ready.

<script type="text/javascript" language="JavaScript"><!--
function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}
//--></script>

Prepare the content of the floating div layer like this:

<div 
   id="uniquename" 
   style="display:none; 
      position:absolute; 
      left:200px; 
      top:100px; 
      border-style: solid; 
      background-color: white; 
      padding: 5px;">
Content goes here.
</div>

The "uniquename" for the id needs to be different than any other id on the web page. We'll assume an id of "uniquename" for the rest of the examples.

The style elements display:none; and position:absolute; are required, as are values for the left: and top: elements.

The other style elements in the example are optional. Additional elements may be added. The W3schools CSS tutorial is a great place to learn about what is possible.

Three different links are available. The links can be located anywhere on the web page, although near the content to be shown/hidden is the usual.

  1. Use this link to show content:

    <a 
       onmouseover="ShowContent('uniquename'); return true;"
       href="javascript:ShowContent('uniquename')">
    [show]
    </a>
    
    
  2. Use this link to hide content:

    <a 
       onmouseover="HideContent('uniquename'); return true;"
       href="javascript:HideContent('uniquename')">
    [hide]
    </a>
    
    
  3. Use this link to hide content if it is currently showing, or show content if currently hidden:

    <a 
       onmouseover="ReverseContentDisplay('uniquename'); return true;"
       href="javascript:ReverseContentDisplay('uniquename')">
    [show/hide]
    </a>
    
    

Notice that the id's name is specified in each of the above links. Change 'uniquename' to be the id name of the content div you wish to effect.

Customizations:

The following applies to any of the above 3 link examples.

  1. To remove the mouse-over functionality, remove the onmouseover attribute from the link.

  2. To remove the click functionality, replace 'uniquename' with '' (two single quotes/apostrophes) from the JavaScript call in the href attribute. Example:
    href="javascript:ShowContent('')"

  3. To link to another web page on the Internet when the link is clicked, replace the entire href attribute value with the URL of the page to link to. Example:
    href="http://example.com/page.html"

One more thing:

The floating div layer content itself may have a "hide" link. Example:

<div 
   id="uniquename" 
   style="display:none; 
      position:absolute; 
      left:200px; 
      top:100px; 
      border-style: solid; 
      background-color: white; 
      padding: 5px;">
<a href="javascript:HideContent('uniquename')">
[click to hide]
</a>
<p>
Content goes here.
</p>
</div>

Will Bontrager

Was this blog post 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 Blog 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.

Recent Articles in the Library

Tap to Select

Find out how to make a link or button to select the content in a div or other HTML element.

Capitalizing the First Letter of Words

Here, find both PHP code and JavaScript code to capitalize the first letter of a word.

Fixed-position Table Header

During a vertical scroll, the table header scrolls out of view -- unless the header is fixed in position.

Simple Floating Menu

When not needed, the menu on the left side of a web page retracts to be out of the way. Tap it and it expands.

CSS Drop Shadows for Images

Images can be given drop shadows using pure CSS.

Ajax File Upload

Learn how to upload a file as soon as a file upload form field is given a file to upload.

Another Form Spam Prevention Technique

This technique should stop much or most of the form spam you are experiencing, perhaps even all of it.

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