惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
F
Full Disclosure
B
Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
Lohrmann on Cybersecurity
月光博客
月光博客
I
Intezer
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
P
Proofpoint News Feed
C
Check Point Blog
N
News | PayPal Newsroom
H
Heimdal Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
G
GRAHAM CLULEY
WordPress大学
WordPress大学
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
爱范儿
爱范儿
Martin Fowler
Martin Fowler
U
Unit 42
T
Troy Hunt's Blog
S
Securelist
V
V2EX
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
CSS effects on images II
2023-07-22 · via CSS Articles by Temani Afif

CSS effects on images II

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

In a previous article, we made a lot of cool and fancy effects for images. We considered modern CSS tricks and only the <img> tag to reach for stunning results. In this article, we will do the same!

We will create even more effects with the same requirements: Only the <img> and a few lines of CSS. No extra element and no pseudo-element.

Overlay Effect

Previously, we did an overlay effect where we have a semi-transparent layer above the image that disappears on hover.

We are going to improve this effect by adding some radius to the image and a circular reveal effect on hover.

Cool right? I am using a solid color this time to hide the image but you can also use a semi-transparent color like the previous demo.

The effect relies on two tricks. First I am going to rely on the outline property to create the overlay and then animate the outline-offset to make it disappear:

img {
  --s: 300px; /* size of the image*/
  
  width: var(--s);
  aspect-ratio: 1;
  border-radius: 50%;
  outline: var(--s) solid #FFC48C;
  outline-offset: calc(var(--s)/-2);
}
img:hover {
  outline-offset: 0;
}

Note how I am using border-radius: 50% to create a rounded image which is also what will make the outline rounded and get the circular animation on hover.

See the Pen by Temani Afif () on .

It looks a bit ugly because the outline is too big but don’t worry, it will get fixed with the second trick when we will introduce clip-path.

Using clip-path allows us to hide the non-need outline and at the same time define the radius of the image. Don’t forget that the main idea is to have an image with a specific radius and not a rounded image.

clip-path: inset(14.65% round 20px);

Here is a step-by-step illustration to understand what the clip-path is doing

Overview of the CSS clip-path

We start with the rounded image without clip-path (the dotted box illustrates the area of the image). Then we apply the clip-path in a way to remove the rounded part. In other words, we are going to extract the inscribed square from the image. We will only see what is inside the red square. Finally, we consider the round option of clip-path to round the clipped area and get the radius we want.

The use of clip-path will also hide the outline outside the image and our effect is perfect. The only drawback of this effect is that we are going to hide some parts of the image so it may not be suitable for all the images.

Overlapping Frame

Let’s move to some fancy frames. In the previous article, we added a frame behind the image that moves on hover to create a full border around it.

This time we are going to make the frame above the image and create a slightly different animation:

We don’t need an extra element or a pseudo-element to create that frame. An outline trick combined with padding can do the job.

img {
  --c: #F9CDAD; /* the main color */
  --b: 8px; /* thickness of the border */
  --o: 25px; /* control the offset */
  
  width: 200px; /* the image size */
  --_p: calc(2*var(--o) + var(--b));
  padding: var(--_p) var(--_p) 0 0;
  outline: var(--b) solid var(--c);
  outline-offset: calc(var(--o) - var(--_p));
}
img:hover {
  padding: calc(var(--_p)/2);
}

The code may look complex but if we add a background coloration the trick will be easier to understand:

See the Pen by Temani Afif () on .

We create an outline with a negative offset which will add that frame above the image in the middle. The padding is creating the illusion of an offset frame but with the background coloration, we can notice that the frame is always in the center of the image area.

Then, by adjusting the padding, we create the moving animation while keeping the frame fixed. The total padding remains the same so the overall size of the image won’t change and the frame won’t move.

Overview of the outline combined with padding

Initially, we define padding on two sides only (the left figure) then on hover we define half the padding on all the sides (the right figure).

This configuration allows us to have 4 variations for the same effect. I already show 2 of them so I let you try the others by adjusting the padding configuration.

What about adding corner-only borders to your image? Yes, it’s possible and you don’t need any extra element to do it. Only a few lines of CSS are needed and we can also get a nice animation on hover.

First, we will add an outline. Yes, outline again. This underrated property is powerful and allows us to create a lot of fancy decorations.

img {
  --t: 5px;  /* the thickness of the border */
  --g: 20px; /* the gap between the border and image */
  
  padding: calc(var(--g) + var(--t));
  outline: var(--t) solid #B38184; /* the color here */
  outline-offset: calc(-1*var(--t));
}
img:hover {
  outline-offset: calc(var(--g)/-1);
}

Nothing complex so far. We are animating the offset of the outline on hover to get the following result:

See the Pen by Temani Afif () on .

I am adding a background coloration to illustrate the area of the image. You can see that the outline is within that area which is important for the next step where will introduce the mask property. The idea is to hide parts of the image area to keep only the corner visible (and the image itself).

I will be using two gradients to achieve this:

img {
  --s: 50px;
  mask:
   conic-gradient(at var(--s) var(--s),#0000 75%,#000 0)
    0 0/calc(100% - var(--s)) calc(100% - var(--s)),
   linear-gradient(#000 0 0) content-box;
}

The linear-gradient covers only the content area (thanks to content-box). This will make sure the image is visible. The conic-gradient will cover the corners. Here is an illustration to better see each gradient:

Overview of the gradient layers

The red and blue areas are the visible part of the image. The outline will move within the red area and the illusion is perfect! We have corner-only borders:

img {
  --s: 50px; /* the size on the corner */
  --t: 5px;  /* the thickness of the border */
  --g: 20px; /* the gap between the border and image */
  
  padding: calc(var(--g) + var(--t));
  outline: var(--t) solid #B38184; /* the color here */
  outline-offset: calc(-1*var(--t));
  -webkit-mask:
    conic-gradient(at var(--s) var(--s),#0000 75%,#000 0)
    0 0/calc(100% - var(--s)) calc(100% - var(--s)),
    linear-gradient(#000 0 0) content-box;
  transition: .4s;
  cursor: pointer;
}
img:hover {
  outline-offset: calc(var(--g)/-1);
}

See the Pen by Temani Afif () on .

If you are not familiar with gradients, I highly recommend reading one of my previous articles where I am detailing how to use conic-gradient to create different CSS patterns.

Zoom Effect

I will end this article with a simple zoom effect on hover.

Such an effect is pretty easy. Only two properties are required.

img {
  --f: 1.15; /* the scale factor */
  
  clip-path: inset(0);
}
img:hover {
  clip-path: inset(calc((1 - 1/var(--f)) * 50%));
  scale: var(--f)
}

We apply a scale transformation which will make the image bigger. Then we will consider clip-path to trim the image and make it looks like the image is growing within a fixed area.

Here is a demo to illustrate the trick. Hover the screen to see the effect:

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif