Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 1, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 1, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 1, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 1, 2025

      7 MagSafe accessories that I recommend every iPhone user should have

      June 1, 2025

      I replaced my Kindle with an iPad Mini as my ebook reader – 8 reasons why I don’t regret it

      June 1, 2025

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Student Record Android App using SQLite

      June 1, 2025
      Recent

      Student Record Android App using SQLite

      June 1, 2025

      When Array uses less memory than Uint8Array (in V8)

      June 1, 2025

      Laravel 12 Starter Kits: Definite Guide Which to Choose

      June 1, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025
      Recent

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025

      Le notizie minori del mondo GNU/Linux e dintorni della settimana nr 22/2025

      June 1, 2025

      Rilasciata PorteuX 2.1: Novità e Approfondimenti sulla Distribuzione GNU/Linux Portatile Basata su Slackware

      June 1, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»Positioning Text Around Elements With CSS Offset

    Positioning Text Around Elements With CSS Offset

    January 24, 2025

    When it comes to positioning elements on a page, including text, there are many ways to go about it in CSS — the literal position property with corresponding inset-* properties, translate, margin, anchor() (limited browser support at the moment), and so forth. The offset property is another one that belongs in that list.

    The offset property is typically used for animating an element along a predetermined path. For instance, the square in the following example traverses a circular path:

    <div class="circle">
      <div class="square"></div>
    </div>
    @property --p {
      syntax: '<percentage>';
      inherits: false;
      initial-value: 0%;
    }
    .square {
      offset: top 50% right 50% circle(50%) var(--p);
      transition: --p 1s linear;
    
      /* Equivalent to:
        offset-position: top 50% right 50%;
        offset-path: circle(50%);
        offset-distance: var(--p); */
    
      /* etc. */
    }
    
    .circle:hover .square{ --p: 100%; }
    CodePen Embed Fallback

    A registered CSS custom property (--p) is used to set and animate the offset distance of the square element. The animation is possible because an element can be positioned at any point in a given path using offset. and maybe you didn’t know this, but offset is a shorthand property comprised of the following constituent properties:

    • offset-position: The path’s starting point
    • offset-path: The shape along which the element can be moved
    • offset-distance: A distance along the path on which the element is moved
    • offset-rotate: The rotation angle of an element relative to its anchor point and offset path
    • offset-anchor: A position within the element that’s aligned to the path

    The offset-path property is the one that’s important to what we’re trying to achieve. It accepts a shape value — including SVG shapes or CSS shape functions — as well as reference boxes of the containing element to create the path.

    Reference boxes? Those are an element’s dimensions according to the CSS Box Model, including content-box, padding-box, border-box, as well as SVG contexts, such as the view-box, fill-box, and stroke-box. These simplify how we position elements along the edges of their containing elements. Here’s an example: all the small squares below are placed in the default top-left corner of their containing elements’ content-box. In contrast, the small circles are positioned along the top-right corner (25% into their containing elements’ square perimeter) of the content-box, border-box, and padding-box, respectively.

    <div class="big">
      <div class="small circle"></div>
      <div class="small square"></div>
      <p>She sells sea shells by the seashore</p>
    </div>
    
    <div class="big">
      <div class="small circle"></div>
      <div class="small square"></div>
      <p>She sells sea shells by the seashore</p>
    </div>
    
    <div class="big">
      <div class="small circle"></div>
      <div class="small square"></div>
      <p>She sells sea shells by the seashore</p>
    </div>
    .small {
      /* etc. */
      position: absolute;
    
      &.square {
        offset: content-box;
        border-radius: 4px;
      }
    
      &.circle { border-radius: 50%; }
    }
    
    .big {
      /* etc. */
      contain: layout; /* (or position: relative) */
    
      &:nth-of-type(1) {
        .circle { offset: content-box 25%; }
      }
    
      &:nth-of-type(2) {
        border: 20px solid rgb(170 232 251);
        .circle { offset: border-box 25%; }
      }
    
      &:nth-of-type(3) {
        padding: 20px;
        .circle { offset: padding-box 25%; }
      }
    }
    CodePen Embed Fallback

    Note: You can separate the element’s offset-positioned layout context if you don’t want to allocated space for it inside its containing parent element. That’s how I’ve approached it in the example above so that the paragraph text inside can sit flush against the edges. As a result, the offset positioned elements (small squares and circles) are given their own contexts using position: absolute, which removes them from the normal document flow.

    This method, positioning relative to reference boxes, makes it easy to place elements like notification dots and ornamental ribbon tips along the periphery of some UI module. It further simplifies the placement of texts along a containing block’s edges, as offset can also rotate elements along the path, thanks to offset-rotate. A simple example shows the date of an article placed at a block’s right edge:

    <article>
      <h1>The Irreplaceable Value of Human Decision-Making in the Age of AI</h1>
      <!-- paragraphs -->
      <div class="date">Published on 11<sup>th</sup> Dec</div>
      <cite>An excerpt from the HBR article</cite>
    </article>
    article {
      container-type: inline-size;
      /* etc. */
    }
    
    .date {
      offset: padding-box 100cqw 90deg / left 0 bottom -10px;
      
      /*
        Equivalent to:
        offset-path: padding-box;
        offset-distance: 100cqw; (100% of the container element's width)
        offset-rotate: 90deg;
        offset-anchor: left 0 bottom -10px;
      */
    }
    CodePen Embed Fallback

    As we just saw, using the offset property with a reference box path and container units is even more efficient — you can easily set the offset distance based on the containing element’s width or height. I’ll include a reference for learning more about container queries and container query units in the “Further Reading” section at the end of this article.

    There’s also the offset-anchor property that’s used in that last example. It provides the anchor for the element’s displacement and rotation — for instance, the 90 degree rotation in the example happens from the element’s bottom-left corner. The offset-anchor property can also be used to move the element either inward or outward from the reference box by adjusting inset-* values — for instance, the bottom -10px arguments pull the element’s bottom edge outwards from its containing element’s padding-box. This enhances the precision of placements, also demonstrated below.

    <figure>
      <div class="big">4</div>
      <div class="small">number four</div>
    </figure>
    .small {
      width: max-content;
      offset: content-box 90% -54deg / center -3rem;
    
      /*
        Equivalent to:
        offset-path: content-box;
        offset-distance: 90%;
        offset-rotate: -54deg;
        offset-anchor: center -3rem;
      */
    
      font-size: 1.5rem;
      color: navy;
    }
    CodePen Embed Fallback

    As shown at the beginning of the article, offset positioning is animateable, which allows for dynamic design effects, like this:

    <article>
      <figure>
        <div class="small one">17<sup>th</sup> Jan. 2025</div>
        <span class="big">Seminar<br>on<br>Literature</span>
        <div class="small two">Tickets Available</div>
      </figure>
    </article>
    @property --d {
      syntax: "<percentage>";
      inherits: false;
      initial-value: 0%;
    }
    
    .small {
      /* other style rules */
      offset: content-box var(--d) 0deg / left center;
    
      /*
        Equivalent to:
        offset-path: content-box;
        offset-distance: var(--d);
        offset-rotate: 0deg;
        offset-anchor: left center;
      */
    
      transition: --d .2s linear;
    
      &.one { --d: 2%; }
      &.two { --d: 70%; }
    }
    
    article:hover figure {
      .one { --d: 15%;  }
      .two { --d: 80%;  }
    }
    CodePen Embed Fallback

    Wrapping up

    Whether for graphic designs like text along borders, textual annotations, or even dynamic texts like error messaging, CSS offset is an easy-to-use option to achieve all of that. We can position the elements along the reference boxes of their containing parent elements, rotate them, and even add animation if needed.

    Further reading

    • The CSS offset-path property: CSS-Tricks, MDN
    • The CSS offset-anchor property: CSS-Tricks, MDN
    • Container query length units: CSS-Tricks, MDN
    • The @property at-rule: CSS-Tricks, web.dev
    • The CSS Box Model: CSS-Tricks
    • SVG Reference Boxes: W3C

    Positioning Text Around Elements With CSS Offset originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleHow to change default save location for screenshots on Windows 11
    Next Article Pix – image viewer and browser

    Related Posts

    News & Updates

    7 MagSafe accessories that I recommend every iPhone user should have

    June 1, 2025
    News & Updates

    I replaced my Kindle with an iPad Mini as my ebook reader – 8 reasons why I don’t regret it

    June 1, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    How to make the Xfce desktop more user-friendly

    Development

    Qwen Open Sources the Powerful, Diverse, and Practical Qwen2.5-Coder Series (0.5B/1.5B/3B/7B/14B/32B)

    Development

    Anchoreum: a free game for learning CSS anchor positioning (Chrome & Edge only)

    Development

    CVE-2025-33024 – RUGGEDCOM ROX Command Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    aaronfrancis/solo

    December 7, 2024

    A Laravel package to run multiple commands at once, to aid in local development. Source:…

    AI21 Labs Introduces Jamba-Instruct Model: An Instruction-Tuned Version of Their Hybrid SSM-Transformer Jamba Model

    May 8, 2024

    Away From the Keyboard: Kyle Lai, Software Engineer 2

    April 17, 2025

    Learn Python for Data Science – Full Course for Beginners

    May 29, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.