Manually adjusting background shapes every time you change a line of text is one of the biggest time-wasters in motion design. Whether you create Lower Thirds, social media captions, or UI mockups, the goal is to build a "smart" container that reacts to your typing instantly.

In this guide, we won't just make a basic box. We will build a professional, bulletproof setup using the sourceRectAtTime() expression that includes built-in padding controls and fixes common alignment issues caused by letter descenders (like g, y, p).

1. Essential Setup and Layer Order

Clean organization prevents expression errors later. The hierarchy and anchor points are critical here.

  • Create Your Text: Use the Type Tool (T) and type your content. For the best results, set the paragraph alignment to Center in the Paragraph panel.
  • Center the Anchor Point: Select your text layer and press Ctrl+Alt+Home (Windows) or Cmd+Option+Home (Mac). This snaps the anchor point to the true center of your text. If you skip this, your box might drift off-center.
  • Add the Shape: With no layers selected, double-click the Rectangle Tool. This creates a shape layer that fills the composition. Change the fill color and move this layer below your text layer.
Pro Tip: Rename this layer to "Box" or "Background" immediately. It helps keep your timeline organized when you revisit the project months later.

2. The "Master" Expression for Size

Most basic tutorials simply link width and height. We are going to add variables directly into the code. This lets you control padding (margins) right from the expression editor without adding messy "Offset Paths" effects.

  1. Open the Shape Layer properties: Contents > Rectangle 1 > Rectangle Path 1.
  2. Alt-Click (Option-Click on Mac) the stopwatch next to Size.
  3. Paste the following code into the editor:
var s = thisComp.layer("YOUR_TEXT_LAYER_NAME");
var w = s.sourceRectAtTime().width;
var h = s.sourceRectAtTime().height;
var wPad = 100; // Horizontal Padding
var hPad = 60;  // Vertical Padding
[w + wPad, h + hPad]

Important: You must replace "YOUR_TEXT_LAYER_NAME" with the exact name of your text layer.

This code measures the text and adds your custom wPad and hPad values. If you need more breathing room around your text, simply change 100 to 150 in the code.

3. Fixing the Position (Crucial Step)

If you only link the size, the box might expand unevenly or drift when you add new lines. We need to lock the box's position to the "visual center" of the text.

  1. Remain inside Rectangle Path 1. Find the Position property (Note: Use the Shape's position, not the main Layer Position).
  2. Alt-Click the stopwatch and paste this code:
var s = thisComp.layer("YOUR_TEXT_LAYER_NAME");
var w = s.sourceRectAtTime().width / 2;
var h = s.sourceRectAtTime().height / 2;
var l = s.sourceRectAtTime().left;
var t = s.sourceRectAtTime().top;
[l + w, t + h]

This step is vital. The .top and .left attributes of sourceRectAtTime calculate the exact pixel boundaries of your font. This ensures the box stays perfectly centered, even if you use a font with tall ascenders or long descenders.

4. Refinement and Use Cases

Now you have a smart box. To make it look modern, increase the Roundness value under Rectangle Path 1. Finally, use the Parent & Link whip in the timeline to parent the Shape Layer to the Text Layer. Now, wherever the text moves, the box follows.

This technique is the foundation for many advanced motion graphics tasks:

  • Timers and Counters: If you have numbers that change rapidly (like a countdown), this method prevents the background from jittering. You can combine this with our guide on creating a dynamic timer in After Effects for a complete HUD look.
  • Logo Backgrounds: Working with logos that aren't vector-based? You can create dynamic containers for them as well. Check out our tips on animating logos without vectors in After Effects.
  • Specific Highlights: Sometimes you don't want a box around the whole sentence, but just a highlight effect on specific words. That requires a slightly different approach using text animators.

If you edit in Premiere Pro, open the Essential Graphics panel. Drag your "Source Text" and the Shape's "Color" property into the panel and export it as a .mogrt. This lets you use your auto-resizing box in your edits without opening After Effects again.