CSS Text Overlap

By  on  

One of the important functions of CSS is to position elements. Margin, padding, top, left, right, bottom, position, and z-index are just a few of the major players in CSS positioning. By using the above spacing properties and the z-index "layering" property, we can make your elements overlap each other.

The XHTML

<div id="element-1" class="element">David</div>
<div id="element-2" class="element">Walsh</div>
<div id="element-3" class="element">Blog</div>
<div id="element-4" class="element">Showing</div>
<div id="element-5" class="element">CSS</div>
<div id="element-6" class="element">Overlap</div>

Just a bunch of elements with text only. Simple.

The CSS

.element		{ font-size:75px; position:absolute; }
#element-1		{ color:lightblue; z-index:1; }
#element-2		{ color:lightgreen; margin:30px 0 0 30px; z-index:2; }
#element-3		{ color:pink; margin:60px 0 0 60px; z-index:3; }
#element-4		{ color:yellow; margin:90px 0 0 90px; z-index:4; }
#element-5		{ color:orange; margin:120px 0 0 120px; z-index:5; }
#element-6		{ color:gray; margin:150px 0 0 150px; z-index:6; }

/* worded colors? awful! */

The major players in the CSS are the absolute positioning, the margin property, and the z-index.

As lovely as the overlap is, CSS is still limited to the "safe" fonts, so text-only imagery is still necessary when we want to use "fun" fonts.

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    Ana Tudor&#8217;s Favorite CodePen Demos

    Cocoon I love canvas, I love interactive demos and I don't think I have ever been more impressed by somebody's work than when I discovered what Tiffany Rayside has created on CodePen. So I had to start off with one of her interactive canvas pens, even though...

  • By
    Implement jQuery&#8217;s hover() Method in MooTools

    jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here's how to implement that for MooTools Elements. The MooTools JavaScript We implement hover() which accepts to functions; one will be called on mouseenter and the other...

Discussion

  1. I’m pretty sure this isn’t using z-index, since you aren’t setting it anywhere. And since you’re not setting it, the elements are just being stacked according to document flow. If this is what you want then the position absolute is unnecessary. You can just use negative top margins to overlap the elements and get the added benefit of maintaining flow in case you want to put something else after these divs.

  2. Ditto,

    I was expecting to see something using Z-index and was going to see how compatible it is across various browsers.

  3. I created the original file and then forgot to update a few text items when I had concluded z-index wasn’t necessary. Don’t worry though — I have some Moo and jQuery that will really accentuate the z-index.

  4. Jeff Hartman

    # /* worded colors? awful! */

    Why are worded colors awful?

  5. Thank you so much for this code. I spent hours on working on something like this and came up with issues with text all over the place.

    This helped a lot.

  6. Using for a CANCELLED display. Thanks!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!