DEV Community

Shshank
Shshank

Posted on

2 1

#CSSExplainer: image() function, explained

The CSS image() function is similar to the url() function, but with extra features that let us specify an image with fallback options and annotations.

Key features:

  • Uses media fragments to clip out a portion
  • Specifies fallback images
  • Uses a solid color as an image
  • Specifies the directionality of image, ltr for left to right or rtl for right to left
/* Syntax of image() function */

/*
image(<image-tags> [ <image-src> , <color> ] )

<image-tags> = ltr | rtl
 ltr - ltr for left-to-right & rtl - right-to-left. 

<image-src> = <url> | <string>
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color>
*/


/* Example-1: Cropping portion of an image*/
.logo {
   background-image: image('myimage.webp#xywh=338,324,360,390');
}

/* Example-2: Putting color on top of a background image */
.logo {
  background-image:
    image(rgba(0, 0, 0, 0.25)),
    url("https://mdn.mozillademos.org/files/12053/firefox.png");
  background-size: 25%;
  background-repeat: no-repeat;
}
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay