CSS Change Text

CSS Change Text On Hover

CSS is known for styling, but it can be used to emulate a complex programming language. For example, the pseudo-selector hover can be manipulated to replace text. This works with CSS 2.1 and up and all browsers that support the hover function.

The entire text is present on the screen. The text is broken up into parts to be hidden until required and parts that are shown by default. When a user hovers over the text, the hidden part is shown and the default text is hidden.

This tutorial uses the HTML and CSS hover function. The CSS hover function is used to show hidden text.

    Tools are required:

  • Text editor.
  • HTML.
  • CSS.
  • Browser to view output.

Optional Download and install suitable text editor

Any text editor with regular expression support will work. To select a reviewed lightweight programming editor read the Ojambo.com Lightweight Programming Editors.

CSS-Change-Text.html file

<!--
  CSS-Change-Text.html
  
  Copyright 2011 Edward <http://ojambo.com>
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  MA 02110-1301, USA.
        
        
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>Ojambo.com CSS Change Text On Hover</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<style type="text/css">
		p span.show {
			display:none;
			}
		p:hover span.show {
			display:inline;
			}
		p:hover span.noshow {
			display:none;
			}
	</style>
</head>

<body>
	<p>
		<span class="noshow">Normal Text</span><span class="show">Hover Text</span>
	</p>

</body>

</html>

The CSS used in an internal style sheet inside the head tags.

How to Use:

Run the CSS-Change-Text.html file in your favourite browser.
Hover over the text to see the new (hidden) text.

Demonstration:

Ojambo.com CSS Change Text Tutorial

Image Missing
Ojambo.com CSS Change Text Tutorial

Conclusion:

CSS is not just for styling content. With some imagination, the hover function can be used to emulate changing text. It works by diving text is two parts, one is the default and the other is hidden. The hidden text will be shown during the hover event.

    Recommendations:

  1. Use CSS to eliminate the need to run server-side code.
  2. Use CSS to eliminate the need to run JavaScript code.
  3. Do not assume users want or have enable JavaScript.