Introduction to CSS offset
CSS offset property also termed as motion is defined as to dictate the content position part and allows an element to define in a path. And it supports four common values like the top, left, bottom, right. It defines a movement path for an element in the HTML during animation move. It makes use of Keyframes move for animating and the neglected values are set to their initial value. It helps in specifying how far the element is offset from the given position like containing box edges.
Syntax
The general syntax of CSS Offset is given below:
Offset: path | rotate | distance | position | anchor;
How to offset property work in CSS?
Here let us start with the offset property values where this makes the offset to do actions on the move. They specify the initial position of the path.
- Offset rotate: This property is used for implementing the angle of the box along with the offset-path direction. And the orientation of this is dependent on offset path and direction as well. And the offset rotate takes the values like auto, reverse and finally, <angle>. auto helps the element to face in a particular direction. To control the behavior of the defined path when the element rotates this property is been used.
- Offset path: This is the most working values in offset and considered to be the primary motion path module meanwhile this has some values like URL, path(), and none. This takes a path() function and it is positioned like :
offset-path: path('M 0 200 L 250 200 L 400 200');
To move the element on a path keyframes are been used for the animation.
- offset distance: Offset distance is specified along the path provided it should specify with a fixed length. It ranges from 0% to 100 %. The middle value is 50 % and the default value is 0%. All these values specify the total length of the path and their values are given in both the length and percentage.
Let’s take a sample code here
M300,0 A300,300 0 1,1 200,400 A200,200 0 1,1 200,0
Here a path is constructed first such that the starting point gives the top-most position of the circle. The first letter ‘M’ says it’s a Starting point to Move to (300, 0). Coming to the next part make an arc with 300, 300 as a radius.
There are few commands in the path they are
- M: To move a position
- L: to draw a line.
- A: To make an arc
- Z: The path is closed and returning to the starting point.
So, in all the below examples a motion is achieved in the elements. Every element is moved in the path by animating the offset-distance property which in turn is done by CSS transition.
- offset-anchor: This makes a spot on a specified element with that the position of the path is determined. In simple terms, it makes a point within the box and moves along with offset-path. It takes the value of auto and position.
Examples to Implement CSS offset
Below are the examples mentioned :
Example #1. Implementation of offset with Path property.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
@keyframes smove {
from {
offset-distance: 2%;
}
to {
offset-distance: 150%;
}
}
dzv {
width: 50px;
height: 50px;
background-color: yellow;
offset: path("M 150 150 L 210 120 L 230 300 z") auto;
animation: smove 5s linear infinite;
}
</style>
</head>
<body>
<h2> Demo on Offset property</h2>
<div></div>
</body>
</html>
Output:
Example #2. Implementation of offset using distance and stroke.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
body {
background: brown;
padding: 21px;
display: flex;
justify-content: center;
}
.run {
stroke: blue;
}
.demo {
motion-path: path('M 5 5 m -3, 0 a 3,5 0 1,0 8,0 a 4,4 0 1,0 -8,0');
offset-path: path('M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0');
animation: move 4s linear infinite;
}
@keyframes move {
100% {
motion-offset: 110%;
offset-distance: 110%;
}
}
</style>
</head>
<body>
<svg viewbox="0,0 20,20" width="150px" height="150px">
<path
class="run"
fill="none"
stroke-width="0.20"
d="M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0"
/>
<circle class="demo" r="2" fill="yellow"></circle>
</svg>
</body>
</html>
So in this example, the circle is being animated with the path value of offset property.
Output:
Example #3
Code:
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
svg {
width: 200px;
display: block;
position: relative;
}
.memo {
width: 12px;
height: 12px;
background-color: yellow;
border-radius: 60%;
offset-path: path('M20 90 Q 80.5 10, 150 90 T 300 80');
offset-distance: 0%;
animation: memoball 5s linear alternate infinite;
}
@keyframes memoball {
from {
offset-distance: 0%;
}
to {
offset-distance: 50%;
}
}
</style>
</head>
<body>
<svg width="200px" height="150px" version="1.1">
<path fill="transparent" stroke="red" stroke-width="3" d="M20 90 Q 80.5 10, 150 90 T 300 80" class="path"></path>
</svg><div class="memo"></div>
</body>
</html>
Output:
Example #4. Implementing offset rotate
Code – rotate.html
<!DOCTYPE html>
<html>
<head>
<title> Offset-Rotate Property</title>
<style>
body {
background-color: #3cb371;
}
.rotater {
width: 60px;
height: 60px;
background: linear-gradient(#ff6347 55%, #ee82ee 55%);
position: relative;
left: 60%;
top: 200px;
offset-path: path("M19.50,52.42s50.77-75.05,105.20-.65,105.70-7.20,105.75-7.20S250.36,6.50,150.33,8.08s-15,71.57-94.51,74.56S18.45,58.46,19.50,60.42Z");
offset-rotate: reverse;
animation: move 5s linear infinite;
}
@keyframes move {
100% {
offset-distance: 100%;
}
}
h2{
color : red;
}
</style>
<body>
<h2>CSS offset-Rotate Property</h2>
<div class="rotater"></div>
</body>
</html>
Here a path is defined by an offset and the element is made to rotate by making the value as the reverse.
Output:
Example #5. Implementation Using anchor property
Code:
<!DOCTYPE html>
<html>
<head>
<title> Offset-Rotate Property</title>
<style>
body {
background-color: pink;
padding: 0 5em;
width: 100%;
}
svg, .bbb {
position: absolute;
}
.bbb {
height: 60px;
width: 60px;
offset-path: path("M0,381 C8.21283455,261.121686 34.1400486,180.27218 75.575265,145.646712 C141.863248,92.2187469 265.92384,261.819412 334.586493,241.743905 C397.463236,219.698512 452.033427,1.38464317 452.012437,1.28454317");
offset-rotate: 0;
animation: move 5s 0ms infinite alternate ease-in-out;
}
.aa {
background-color: blue;
offset-anchor: center center;
}
.bb {
background-color: green;
offset-anchor: left top;
}
.cc {
background-color: rose;
offset-anchor: right bottom;
}
@keyframes move {
100% {
offset-distance: 100%;
}
}
</style>
<body>
<svg class="track" viewBox="0 0 450 376" width="450px" height="376px">
<path fill="none" stroke="yellow" stroke-width="2" d="M0,381 C8.21283455,261.121686 34.1400486,180.27218 75.575265,145.646712 C141.863248,92.2187469 265.92384,261.819412 334.586493,241.743905 C397.463236,219.698512 452.033427,1.38464317 452.012437,1.28454317"></path>
</svg>
<div class="bbb aa"></div>
<div class="bbb bb"></div>
<div class="bb cc"></div>
</body>
</html>
Output:
Conclusion
Therefore, in this article, we have seen the property using their work in animation frames and have gone through several properties values and implementing them in SVG elements using HTML and CSS. Since this is an animation usage it is mandatory to check the browser compatibility too.
Recommended Articles
This is a guide to CSS offset. Here we discuss an introduction to CSS offset, syntax, how does it work with programming examples. You can also go through our other related articles to learn more –