Few weeks ago, I stumbled upon this cool pop-out effect by Mikael Ainalem. It showcases the clip-path: path() in CSS, which just got proper support in most modern browsers. I wanted to dig into it myself to get a better feel for how it works. But in the process, I found some issues with clip-path: path(); and wound up finding an alternative approach that I wanted to walk through with you in this article.
If you haven’t used clip-path or you are unfamiliar with it, it basically allows us to specify a display region for an element based on a clipping path and hide portions of the element that fall outside the clip path.
Possible values for clip-path include circle , ellipse and polygon which limit the use-case to just those specific shapes. This is where the new path value comes in?—?it allows us to use a more flexible SVG path to create various clipping paths that go beyond basic shapes.
Let’s take what we know about clip-path and start working on the hover effect. The basic idea of the is to make the foreground image of a person appear to pop-out from the colorful background and scale up in size when the element is hovered. An important detail is how the foreground image animation (scale up and move up) appears to be independent from the background image animation (scale up only).
This effect looks cool, but there are some issues with the path value. For starters, while we mentioned that support is generally good, it’s not great and hovers around 82% coverage at the time of writing. So, keep in mind that mobile support is currently limited to Chrome and Safari.
Besides support, the bigger and more bizarre issue with path is that it currently only works with pixel values, meaning that it is not responsive. For example, let’s say we zoom into the page. Right off the bat, the path shape starts to cut things off.
This severely limits the number of use cases for clip-path: path(), as it can only be used on fixed-sized elements. Responsive web design has been a widely-accepted standard for many years now, so it’s weird to see a new CSS property that doesn’t follow the principle and exclusively uses pixel units.
What we’re going to do is re-create this effect using standard, widely-supported CSS techniques so that it not only works, but is truly responsive as well.
The tricky part
We want anything that overflows the clip-path to be visible only on the top part of the image. We cannot use a standard CSS overflow property since it affects both the top and bottom.
So, what are our options besides overflow and clip-path? Well, let’s just use
SVG element
SVG
Let’s start by setting up our SVG element. I’ve used Inkscape to create the basic SVG markup and clipping paths, just to make it easy for myself. Once I did that, I updated the markup by adding my own class attributes.
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 -10 100 120"> <defs> <clippath clippathunits="userSpaceOnUse"> <path d="Erstellen wir einen Bild-Pop-out-Effekt mit SVG-Clip-Pfad"></path> </clippath> <clippath clippathunits="userSpaceOnUse"> <path d="Erstellen wir einen Bild-Pop-out-Effekt mit SVG-Clip-Pfad"></path> </clippath> </defs> <g clip-path="url(#maskImage)" transform="translate(0 -7)"> <!-- Background image --> <image clip-path="url(#maskBackground)" width="120" height="120" x="70" y="38" href="Erstellen%20wir%20einen%20Bild-Pop-out-Effekt%20mit%20SVG-Clip-Pfad" transform="translate(-90 -31)"></image> <!-- Foreground image --> <image width="120" height="144" x="-15" y="0" fill="none" href="Erstellen%20wir%20einen%20Bild-Pop-out-Effekt%20mit%20SVG-Clip-Pfad"></image> </g> </svg>
This markup can be easily reused for other background and foreground images. We just need to replace the URL in the href attribute inside image elements.
Now we can work on the hover animation in CSS. We can get by with transforms and transitions, making sure the foreground is nicely centered, then scaling and moving things when the hover takes place.
.image { transform: scale(0.9, 0.9); transition: transform 0.2s ease-in; } .image__foreground { transform-origin: 50% 50%; transform: translateY(4px) scale(1, 1); transition: transform 0.2s ease-in; } .image:hover { transform: scale(1, 1); } .image:hover .image__foreground { transform: translateY(-7px) scale(1.05, 1.05); }
Here is the result of the above HTML and CSS code. Try resizing the screen and changing the dimensions of the SVG element to see how the effect scales with the screen size.
This looks great! However, we’re not done. We still need to address some issues that we get now that we’ve changed the markup from an HTML image element to an SVG element.
SEO and accessibility
Inline SVG elements won’t get indexed by search crawlers. If the SVG elements are an important part of the content, your page SEO might take a hit because those images probably won’t get picked up.
We’ll need additional markup that uses a regular element that’s hidden with CSS. Images declared this way are automatically picked up by crawlers and we can provide links to those images in an image sitemap to make sure that the crawlers manage to find them. We’re using loading="lazy" which allows the browser to decide if loading the image should be deferred.
We’ll wrap both elements in a
<figure> <!-- SVG element --> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 -10 100 120"> <!-- Erstellen wir einen Bild-Pop-out-Effekt mit SVG-Clip-Pfad --> </svg> <!-- Fallback image --> <img src="Erstellen%20wir%20einen%20Bild-Pop-out-Effekt%20mit%20SVG-Clip-Pfad" alt="Erstellen wir einen Bild-Pop-out-Effekt mit SVG-Clip-Pfad" loading="lazy"> </figure>
We also need to address some accessibility concerns for this effect. More specifically, we need to make improvements for users who prefer browsing the web without animations and users who browse the web using screen readers.
Making SVG elements accessible takes a lot of additional markup. Additionally, if we want to remove transitions, we would have to override quite a few CSS properties which can cause issues if our selector specificities aren’t consistent. Luckily, our newly added regular image has great accessibility features baked right in and can easily serve as a replacement for users who browse the web without animations.
<figure> <!-- Animated SVG element --> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 -10 100 120" aria-hidden="true"> <!-- Erstellen wir einen Bild-Pop-out-Effekt mit SVG-Clip-Pfad --> </svg> <!-- Fallback SEO & a11y image --> <img src="Erstellen%20wir%20einen%20Bild-Pop-out-Effekt%20mit%20SVG-Clip-Pfad" alt="Erstellen wir einen Bild-Pop-out-Effekt mit SVG-Clip-Pfad" loading="lazy"> </figure>
We need to hide the SVG element from assistive devices, by adding aria-hidden="true", and we need to update our CSS to include the prefers-reduced-motion media query. We are inclusively hiding the fallback image for users without the reduced motion preference meanwhile keeping it available for assistive devices like screen readers.
@media (prefers-reduced-motion: no-preference) { .fallback-image { clip: rect(0 0 0 0); clip-path: inset(50%); height: 1px; overflow: hidden; position: absolute; white-space: nowrap; width: 1px; } } @media (prefers-reduced-motion) { .image { display: none; } }
Here is the result after the improvements:
Please note that these improvements won’t change how the effect looks and behaves for users who don’t have the prefers-reduced-motion preference set or who aren’t using screen readers.
That’s a wrap
Developers were excited about path option for clip-path CSS attribute and new styling possibilities, but many were displeased to find out that these values only support pixel values. Not only does that mean the feature is not responsive, but it severely limits the number of use cases where we’d want to use it.
We converted an interesting image pop-out hover effect that uses clip-path: path into an SVG element that utilizes the responsiveness of the
Thank you for taking the time to read this article! Let me know if this approach gave you an idea on how to implement your own effects and if you have any suggestions on how to approach this effect in a different way.
Das obige ist der detaillierte Inhalt vonErstellen wir einen Bild-Pop-out-Effekt mit SVG-Clip-Pfad. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Hei?e KI -Werkzeuge

Undress AI Tool
Ausziehbilder kostenlos

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem v?llig kostenlosen KI-Gesichtstausch-Tool aus!

Hei?er Artikel

Hei?e Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Hei?e Themen





AutoPrefixer ist ein Tool, das die Pr?fixe von Anbietern automatisch zu CSS -Attributen basierend auf dem Zielbrowserbereich hinzufügt. 1. Es l?st das Problem, die Pr?fixe mit Fehlern manuell aufrechtzuerhalten. 2. Arbeiten Sie das POSTCSS-Plug-in-Formular durch, analysieren Sie CSS, analysieren Sie Attribute, die vorangestellt werden müssen, und generieren Sie den Code gem?? Konfiguration. 3.. 4. Notizen enthalten nicht manuelles Hinzufügen von Pr?fixen, Konfigurationsaktualisierungen, Pr?fixe nicht alle Attribute, und es wird empfohlen, sie mit dem Pr?prozessor zu verwenden.

TocreatestickyHeadersandfooterswithcss, Anwendungseinstellung: Stickyforheaderswithtopvalueandz-Index, sicherstellen, dass ParentContainersdon'Trictit.1.ForstickyHaaders: Einstellungen: Kleber, Top: 0, Z-Index und BackgroundColor.2.

Es gibt drei M?glichkeiten, einen CSS -Laderotator zu erstellen: 1. Verwenden Sie den Basisrotator der Grenzen, um eine einfache Animation durch HTML und CSS zu erreichen. 2. Verwenden Sie einen benutzerdefinierten Rotator mit mehreren Punkten, um den Sprungeffekt durch verschiedene Verz?gerungszeiten zu erreichen. 3. Fügen Sie einen Rotator in die Taste hinzu und wechseln Sie den Klassen über JavaScript, um den Ladestatus anzuzeigen. Jeder Ansatz betont die Bedeutung von Entwurfsdetails wie Farbe, Gr??e, Zug?nglichkeit und Leistungsoptimierung, um die Benutzererfahrung zu verbessern.

Mobile-firstcsSdeSignRequiressettingTheviewPortMetatag, Verwenden von Relativen, StylingFromsMallScreensUp, Optimierungstypographie und touchtargets.First, addtocontrolscaling.second, verwenden Sie%, EM, orreminsteadofpixelforflexiblelayouts.thirds, thishirds

Um ein intrinsisches reaktionsschnelles Gitterlayout zu erstellen, besteht die Kernmethode darin, den Wiederholungsmodus von CSSGrid (automatisch, minmax ()) zu verwenden. 1. Setzen Sie Grid-Template-S?ulen: Wiederholen (automatisch, Minmax (200px, 1FR)), um den Browser automatisch die Anzahl der Spalten anzupassen und die minimalen und maximalen Breiten jeder Spalte zu begrenzen. 2. Verwenden Sie die Lücke, um den Rasterabstand zu steuern. 3. Der Container sollte auf relative Einheiten wie Breite eingestellt werden: 100%und verwenden Sie Box-Gr??en: Border-Box, um die Berechnungsfehler der Breite zu vermeiden und sie mit Rand: Auto zu zentrieren; 4. Setzen Sie optional die Zeilenh?he und die Ausrichtung des Inhalts, um die visuelle Konsistenz wie die Zeile zu verbessern

Um das gesamte Netzlayout im Ansichtsfenster zentriert zu machen, kann es mit den folgenden Methoden erreicht werden: 1. Rand: 0Auto, um horizontales Zentrieren zu erreichen, und der Container muss so eingestellt werden, dass die feste Breite festgelegt wird, die für festes Layout geeignet ist. 2. Verwenden Sie Flexbox, um die Eigenschaften der Rechtfertigung und Ausrichtung der Eigenschaften des Au?enbeh?lters einzustellen, und kombinieren Sie Min-H?he: 100 VH, um vertikale und horizontale Zentrierung zu erzielen, was für Szenarien mit Vollbild-Displays geeignet ist. 3. Verwenden Sie CSSGrid's Place-Items-Eigenschaft, um sich schnell auf den übergeordneten Container zu konzentrieren, der einfach ist und eine gute Unterstützung von modernen Browsern hat. Gleichzeitig ist es erforderlich, sicherzustellen, dass der Elternbeh?lter eine ausreichende H?he hat. Jede Methode hat anwendbare Szenarien und Einschr?nkungen. W?hlen Sie einfach die entsprechende L?sung gem?? den tats?chlichen Bedürfnissen aus.

FeaturedEtctionIncsusinus@SupportSchecksifabrowsersupportSaspecificfeatureboreAppying-relatedStyles.1.ituSconditionalcsblocksbasedonProperty-ValuePairs, solchas@Supports (Display: Grid)

Um mit CSS -Browser -Kompatibilit?t und Pr?fixproblemen umzugehen, müssen Sie die Unterschiede im Browser -Support verstehen und Anbieterpr?fixe vernünftigerweise verwenden. 1. Verstehen Sie gemeinsame Probleme wie Flexbox und Grid -Unterstützung, Position: Sticky Invaly und Animationsleistung ist unterschiedlich. 2. überprüfen Sie den Best?tigungsunterstützungsstatus von Caniuse. 3. Verwenden Sie korrekt -webkit-, -moz-, -ms-, -o- und andere Herstellerpr?fixe; 4. Es wird empfohlen, Autoprefixer zu verwenden, um automatisch Pr?fixe hinzuzufügen. 5. Postcss installieren und Browserlist konfigurieren, um den Zielbrowser anzugeben. 6. automatisch die Kompatibilit?t w?hrend des Baus bew?ltigen; 7. Modernizr -Erkennungsmerkmale k?nnen für alte Projekte verwendet werden; 8. Keine Notwendigkeit, die Konsistenz aller Browser zu verfolgen,
