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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 2, 2025

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

      June 2, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 2, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 2, 2025

      How Red Hat just quietly, radically transformed enterprise server Linux

      June 2, 2025

      OpenAI wants ChatGPT to be your ‘super assistant’ – what that means

      June 2, 2025

      The best Linux VPNs of 2025: Expert tested and reviewed

      June 2, 2025

      One of my favorite gaming PCs is 60% off right now

      June 2, 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

      `document.currentScript` is more useful than I thought.

      June 2, 2025
      Recent

      `document.currentScript` is more useful than I thought.

      June 2, 2025

      Adobe Sensei and GenAI in Practice for Enterprise CMS

      June 2, 2025

      Over The Air Updates for React Native Apps

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

      You can now open ChatGPT on Windows 11 with Win+C (if you change the Settings)

      June 2, 2025
      Recent

      You can now open ChatGPT on Windows 11 with Win+C (if you change the Settings)

      June 2, 2025

      Microsoft says Copilot can use location to change Outlook’s UI on Android

      June 2, 2025

      TempoMail — Command Line Temporary Email in Linux

      June 2, 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 

    Hostinger
    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

    How Red Hat just quietly, radically transformed enterprise server Linux

    June 2, 2025
    News & Updates

    OpenAI wants ChatGPT to be your ‘super assistant’ – what that means

    June 2, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Everything you need to know about WWE 2K25 (so far) – Confirmed WWE Superstars’ roster, game modes, platforms, and more

    News & Updates

    How to Guide Your Clients to the Best WordPress Solutions

    Learning Resources

    CVE-2025-3859 – Focus URL Truncation Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-46560 – LLaMA LLM Multimodal Tokenizer Resource Exhaustion

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Development

    Error’d: Three Little Nyms

    November 22, 2024

    “Because 9.975 was just a *little* bit too small,” explains our first anonymous helper.  …

    Nine-Year-Old npm Packages Hijacked to Exfiltrate API Keys via Obfuscated Scripts

    March 28, 2025

    CVE-2025-36546 – F5OS SSH Key-Based Authentication Privilege Escalation

    May 7, 2025

    Scammers Promoted Fake Donald Trump Live Stream Urging Cryptocurrency Donations During Presidential Debate

    June 28, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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