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:
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 pointoffset-path
: The shape along which the element can be movedoffset-distance
: A distance along the path on which the element is movedoffset-rotate
: The rotation angle of an element relative to its anchor point and offset pathoffset-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.
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి