Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Optimization

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!

Text Swapping Without Page Jiggles

A common issue with swapping text within a div is that other page content jumps or jiggles.

Generally, the swaps have different amounts of text. They take up different amounts of space. When the text is swapped, other page content moves around to accomodate the different space requirement.

The jiggle can be annoying.

Some website owners restort to making an image of the various text presentations, each image with the same dimensions. Thus, when they swap there's no jiggle.

With that method, text editing can be a chore because a new image needs to be made and correctly sized.

There is another way.

The text swapping can occur within a div of dimensions required by the largest amount of text. The div size is determined automatically, at the time the page is loaded and whenever the browser screen dimensions change, for the browser and device the page is being viewed with.

In other words, the div both adjusts dimensions responsively and prevents page jiggles. No JavaScript required for that.

This article provides instructions for creating the responsive div the correct size to hold any of the swapped text without page jiggling. It should work with pretty much any text-swapping software.

Providing text-swapping software is outside the scope of this article. If you have none at hand, an option is the software at Click Replaces One Div With Another article.

An Illustration

The div below is large enough to accomodate two differently sized amounts of text, as they are swapped, without resizing the div.

Tap anywhere on the text to swap it for the other.

This is longer text, long enough to wrap within the div. The shorter text might or might not wrap (probably not), but in any case with less lines.
This is longer text, long enough to wrap within the div. The shorter text might or might not wrap (probably not), but in any case with less lines.

Another illustration is the Perspective of Trees page at Lightfocus. Above the first image, tap the "Tap for another perspective." link. Tap it two times to see all three "perspectives".

How it Works

The implementation consists of two divs. All the text swapping takes place within the two divs.

The first div — The code for the first of the two divs contains a copy of the largest amount of text that the text swaps will display. The copied text is transparent so it doesn't publish. The reason for the copy of the text is to establish the dimensions required to accomodate each text swap.

The second div — The code for the second of the two divs is within the code for the first div, inserted immediately below the transparent text. This second div nullifies the transparent text color so the CSS's normal text color is available. And it is within this second div that all the text swapping occurs.

Your visible text and text swapping functionality is entirely within the second div — except any JavaScript the swapping system uses may be placed elsewhere on the page.

The Code

The source code below is for the two divs. The div tags themselves don't need any editing, although you may add styling as necessary for your implementation.

<!-- This first div holds a copy of the largest text content for div sizing, font color transparent. -->
<div id="to-size-the-container-for-swapped-content" style="color:transparent; position:relative;">

[A COPY OF THE LARGEST SWAPPED TEXT GOES HERE]

<!-- This second div is to remove the transparent font color and position swapped content inside the sized container. -->
<div id="to-remove-transparency-and-hold-swapped-content" style="position:absolute; top:0; right:0; bottom:0; left:0; color:initial;">

[THE TEXT AND FUNCTIONALITY FOR SWAPPING GO HERE]

</div><!-- id="to-remove-transparency-and-hold-swapped-content" -->
</div><!-- id="to-size-the-container-for-swapped-content" -->

Implementing the code —

Paste the above code into a temporary web page.

When you have determined the text that will be swapped, copy the longest text that will be swapped and replace [A COPY OF THE LARGEST SWAPPED TEXT GOES HERE] with that text. If the text in the swap includes HTML markup, include the markup with the replacement.

Now, replace [THE TEXT AND FUNCTIONALITY FOR SWAPPING GO HERE] with your swapping functionality, whatever makes your text swap work. If, as is probably the case, your swapping functionality uses separate JavaScript, it may be included here or the JavaScript may be put elsewhere on the page.

What Is Good About It

Swapping text under certain conditions can be good, the conditions determined by the site owner.

Doing so without jiggling the web page is good, too, because it is best not to annoy wanted site visitors into abandoning your site.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article 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 Library 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.

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