CSS Border Corners only Hover

CSS Border Corners only Hover
Code Snippet:Corner only with cool hover effect
Author: Temani Afif
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 2,506
License: MIT
Edit Code online: View on CodePen
Read More

This simple CSS code snippet helps you to draw corners only border of an HTML element on hover event. It uses CSS conic-gradient function for the background-image property to set the corner border of an object. The object maybe an image, text, iframe, or any other HTML element.

How to Create CSS Border Corners only on Hover

1. In order to create corners only border, create an HTML element that you want to apply a border to. Here we are placing two images, you can place any element you want.

<img src="https://picsum.photos/id/133/200/200" alt="two old cars in front of a garage">
<img src="https://picsum.photos/id/183/200/200" alt="a very old volkswagen van" class="alt" style="--g: 20px;--b:5px;--s: 80px;--c:#6A4A3C;">

2. Style the corners border using the following CSS. If you want to apply this corner style border to your own element, use the class name of that element instead of img selector.

img {
  --b: 8px;  /* border thickness*/
  --s: 60px; /* size of the corner*/
  --g: 14px; /* the gap*/
  --c: #EDC951; 
  
  padding: calc(var(--b) + var(--g));
  background-image:
    conic-gradient(from  90deg at top    var(--b) left  var(--b),#0000 25%,var(--c) 0),
    conic-gradient(from -90deg at bottom var(--b) right var(--b),#0000 25%,var(--c) 0);
  background-position:
    var(--_p,0%) var(--_p,0%),
    calc(100% - var(--_p,0%)) calc(100% - var(--_p,0%));
  background-size: var(--s) var(--s);
  background-repeat: no-repeat;
  cursor: pointer;
  transition: 
    background-position .3s var(--_i,.3s), 
    background-size .3s calc(.3s - var(--_i,.3s));
}
img.alt {
  background-image:
    conic-gradient(from 180deg at top    var(--b) right var(--b),#0000 25%,var(--c) 0),
    conic-gradient(from   0deg at bottom var(--b) left  var(--b),#0000 25%,var(--c) 0);
  background-position:
    calc(100% - var(--_p,0%)) var(--_p,0%),
    var(--_p,0%) calc(100% - var(--_p,0%));
}
img:hover {
  background-size: calc(100% - var(--g)) calc(100% - var(--g));
  --_p: calc(var(--g)/2);
  --_i: 0s;
}



body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-auto-flow: column;
  gap: 20px;
  place-content: center;
  place-items: center;
  background: #99B2B7;
}

That’s all! hopefully, you have successfully created a border that show on corners only. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X