docs.rodeo

MDN Web Docs mirror

rect()

{{CSSRef}} 

The rect() CSS function creates a rectangle at the specified distance from the top and left edges of the containing block. It is a basic shape function of the {{cssxref("<basic-shape>")}}  data type. You can use the rect() function in CSS properties such as {{cssxref("offset-path")}}  to create the rectangular path along which an element moves and in {{cssxref("clip-path")}}  to define the shape of the clipping region.

Syntax

offset-path: rect(0 1% auto 3% round 0 1px);
clip-path: rect(50px 70px 80% 20%);

Values

The inset rectangle is defined by specifying four offset values, starting with the top edge offset and going clockwise, and an optional round keyword with the border-radius parameter to add rounded corners to the rectangle. Each offset value can be either a <length>, a <percentage>, or the keyword auto.

Formal syntax

{{CSSSyntax}} 

Examples

Creating offset-path using rect()

In this example, the {{cssxref("offset-path")}}  property uses the rect() function to define the shape of the path on which the element, a red box in this case, moves. Three different scenarios are shown, each using different values for the rect() function. The arrow inside the boxes points to the right edge of the box.

<div class="container">
  Rectangular path 1
  <div class="path rect-path-1"></div>
</div>
<div class="container">
  Rectangular path 2
  <div class="path rect-path-2"></div>
</div>
<div class="container">
  Rectangular path 3
  <div class="path rect-path-3"></div>
</div>
.container {
  position: relative;
  display: inline-block;
  width: 200px;
  height: 200px;
  border: 1px solid black;
  margin: 15px;
  text-align: center;
}

.path {
  width: 40px;
  height: 40px;
  background-color: red;
  position: absolute;
  animation: move 10s linear infinite;
}

.rect-path-1 {
  offset-path: rect(50px 150px 200px 50px round 20%);
}

.rect-path-2 {
  offset-path: rect(50px auto 200px 50px round 20%);
}

.rect-path-3 {
  offset-path: rect(50px auto 200px auto);
}

@keyframes move {
  0% {
    offset-distance: 0%;
  }
  100% {
    offset-distance: 100%;
  }
}

Result

{{EmbedLiveSample("Creating an offset-path using rect", "100%", 400)}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN