Smoothly reveal a border at the moment of transition

I currently have images set up as checkboxes that display a yellow border upon clicking with a transition effect. However, the transition of the border causes the images to shake and shutter slightly due to margin adjustments.

To resolve this issue, I tried setting a default margin of 3px which gets overridden when the border appears (also 3px). This led to an awkward shaking effect as the border grows during the transition.

My goal now is to have the border instantly appear as 3px upon clicking without causing any immediate visual disturbance. Here is my current CSS code:

Unfortunately, I'm unable to provide the full CSS code here due to restrictions, but I have included a snippet below:

img{
    width: 100%;
    max-width: 380px;
    border: 3px solid rgba(255,0,0,0);
    background-color: black;
    height: 100%;
    max-height: 250px;
    margin: 3px;
    border-radius: 15px;
    transition: opacity 2s ease, border 1s ease;
    opacity: 0.5;
}
<img src="http://placehold.it/380x250" />

Answer №1

Instead of having a solid border, why not use a transparent border and then transition the border-color?

   img {
     width: 100%;
     max-width: 380px;
     border: 3px solid transparent;
     height: 100%;
     max-height: 250px;
     margin: 3px;
     border-radius: 15px;
     transition: opacity 2s ease, border-color 1s ease;
     opacity: 0.5;
   }
   img:hover {
     border-color: red;
   }
<img src="http://placehold.it/200x200" />

Answer №2

To avoid disrupting the layout when adding a border, you might want to consider using

box-shadow

instead of the border property, and then transition it.

$('.box').on('click', function() {
  $(this).addClass('box--checked');
});
.box {
  width: 120px;
  height: 120px;
  transition: box-shadow .3s linear;
  margin-bottom: 12px;
  background-color: #DDD;
  box-shadow: 0px 0px 0px 0px darkorange;
}
.box--checked {
  box-shadow: 0px 0px 0px 6px darkorange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div class="box box--one">A</div>
<div class="box box--two">B</div>
<div class="box box--three">C</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Preventing the use of double-click on Grooveshark

I am currently in the process of creating a webpage to serve as a jukebox for parties. My goal is to have the page load Grooveshark with a transparent overlay (or any other necessary method) that can prevent users from forcefully adding their song to the f ...

Steer clear of using grey backgrounds for anchors/links in IE 10

What is the best way to prevent the irritating grey background that appears on anchors in IE 10 when they are clicked? ...

Problems arise when using AngularJS' .run function after navigating to a different page

I have encountered an issue with ngRoute while navigating between pages in my web application. The main login page is called index.html, and the routing is controlled by the main js file. However, I face a problem when trying to use a .run block on a speci ...

Enabling auto-expansion for textareas with every keystroke

Currently, I have implemented the following script: jQuery.each(jQuery('textarea[data-autoresize]'), function() { var offset = this.offsetHeight - this.clientHeight; var resizeTextarea = function(el) { jQuery(el).css('h ...

Reset a form input using jQuery

There is an ajax method in place that reloads only a specific div upon receiving a service response. success: function (response) { $( "#list" ).load(window.location.href + " #list" ); $( "#badge" ).load(window.location.href + " #badge" ); $ ...

What accounts for the different behavior of line-height when using units vs percentage or em in this instance?

I'm puzzled by the behavior of the CSS code provided, which is also demonstrated in this fiddle. <style type="text/css"> p { font-size: 14px; } .percentage { line-height: 150%; } .em-and-a-half { line-height: 1.5em; } .decimal { ...

showcasing pictures on a .handlebars/html interface

Having some trouble linking images to my .handlebars/html files in the views directory. I've already set up a public folder as per the requirements, but the linked images still aren't showing up when I view my webpage. Below is the node.js code ...

Tips for extracting text from a textarea while retaining newline characters:

When using jquery, my goal is to retrieve the text from a textarea element with new lines represented as \n instead of br tags. However, I encountered an issue where Firefox debugger does not display the \n or br characters when I select it and r ...

Issue with border-color in CSS on Chrome tables

Here is a table style I have defined for selected elements: tr[selected] { background: #FFF; border-color: #000000; color: #000000; } To apply this style, I use JavaScript: $this.unbind().change(function () { element = $(this).parent ...

Tips for Embedding External JavaScript and URLs in Joomla Protostar Template

I'm interested in incorporating a hamburger menu into my Joomla protostar template, similar to the one featured on this URL: ['https://codepen.io/PaulVanO/pen/GgGeyE'] This will provide me with a fullscreen hamburger menu for both desktop ...

increasing the size of the input thumb compared to the others

Hello fellow React developers, I'm currently learning by coding and I have a question about a slider. I'm trying to make the thumb of the slider bigger, but when I increase its size it doesn't fully display within the input area (as you can ...

Encapsulate every list item with a <ul> tag using jQuery

Not going into specifics, I've written some jQuery code like this: $("nav ul ul").each(function () { var navitems = $(this).html(); alert(navitems); }); I understand that .html uses InnerHTML so the alerts display as follows: <li>xxxx ...

Guide to positioning an image absolutely within a div

I am struggling to position a small close image within a div without success. The goal is for the image to always appear on the left side of the div, regardless of the content. The content consists of a text label with a maximum length of 8 characters. Iss ...

Manipulating Images with jQuery: Adding and Removing Images Without Affecting Text

Below is the code I'm working with: <input type = checkbox id = "purple" name = "box" > Purple </input> <div id = birdcage></div> This is the JavaScript section: $("#purple").click(function(){ if ($("#purple").is(":chec ...

Discovering hidden elements using Overflow:hidden

I need help figuring out how to display the cropped part of my text without using scroll bars in css. For example: td { overflow: hidden; } The hidden content is important, but the table property is fixed. table { table-layout: fixed; } I want the ...

Tips for sending php data to javascript efficiently

I'm wondering about the best way to retrieve HTML table rows and display the values on another page upon clicking a button. Ideally, I would like to store these variables in a session for future use. Any advice on how to proceed? ...

When I employ ngroute, the Angular section in the html page does not display

I am looking to implement a fixed menu named "fixed_admin.html" on my website: <!DOCTYPE html> <!-- saved from url=(0069)https://hackerstribe.com/guide/IT-bootstrap-3.1.1/examples/dashboard/ --> <html lang="en"><head><meta htt ...

How can I achieve a full-width stepper in Bootstrap?

.row { background: #f8f9fa; margin-top: 20px; } .col { border: solid 1px #6c757d; padding: 10px; } <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="https://useloope ...

Sending the most recent result to each dynamically created div

In my WordPress project, I have an event panel that displays upcoming event details and shows the remaining time until the next event. The countdown dynamically gets values from the database and calculates the time left based on the user's input in th ...

Shape with smoothed corners

Is there a way to create rectangles with rounded corners using CSS, SVG, or other methods while maintaining straight horizontal and vertical sides? I attempted to use border-radius for the rounded corners, but found that it created curved edges on both ho ...