Animating margins and padding using CSS transitions can result in a choppy and inconsistent animation effect

On my personal website, I have implemented the CSS3 Transition property on the top navigation to create an animation effect on an element with a border. This effect causes the border to swell when hovered over.

Here is the relevant markup:

<nav>
        <a class="email" href="mailto:notmyrealemailaddress">
          <div class="icon-border">
            <img src="images/mail_icon.png" width="14" height="12">
          </div>Email Me</a>

        <a class="phone" href="tel:4075555555">
          <div class="icon-border">
            <img src="images/phone_icon.png" width="11" height="18">
          </div>Call Me</a>

        <a class="behance" href="http://behance.net/dannymcgee" target="_blank">
          <div class="icon-border">
            <img src="images/behance_icon.png" width="21" height="13">
          </div>See My Work</a>
</nav>

CSS:

header nav .icon-border {
  display: inline-block;
  border: 2px solid #000;
  border-radius: 30px;
  padding: 5px;
  margin: 0 10px;
  transition: 0.15s padding ease-out, 0.15s margin ease-out, 0.15s border ease-out;
}

header nav a:hover .icon-border {
  padding: 10px;
  margin: -10px 5px;
  border: 2px solid #ddd;
  transition: 0.15s padding ease-out, 0.15s margin ease-out, 0.15s border ease-out;
}

By adjusting margins and padding on hover, the circular border of the element expands without affecting the position of the enclosed image. However, there is an issue when quickly moving the mouse between certain links, causing the navigation to "jump" slightly. This problem can likely be resolved. Any suggestions?

Answer №1

I think the problem arises from the transition of margins (particularly with the use of negative margins, which can be a bit tricky).

An alternative approach could involve using transform: scale(x)

Here's an example:

header nav .icon-border {
  display: inline-block;
  border: 2px solid #000;
  border-radius: 30px;
  padding: 5px;
  margin: 0 10px;
  transform: scale(1); /* a scale is necessary for transitioning in both directions */
  transition: 0.15s all ease;
}

header nav a:hover .icon-border {
  transform: scale(1.2);
  border: 2px solid #ddd;
}

Answer №2

div {
  transition: all 0.5s ease;
  padding: 13px;
}

div:hover {
  padding: 23px;
}

alternatively

* { transition: all 0.5s ease; }

div {
  padding: 13px;
}

div:hover {
  padding: 23px;
}

Answer №3

Possibly a solution:

header nav a {
  set as inline element;
}

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

Creating ngModel dynamically with *ngFor in Angular 2: A guide

I've been struggling with this for the past couple of days - how can I have a new ngModel for each iteration within an *ngFor loop? The idea is that I load a list of questions, and within each question there are 2 propositions. Here's the HTML: & ...

Guide to eliminating the arrow from a dropdown menu in Bootstrap 4

I am working with Bootstrap 4 and attempting to eliminate the dropdown arrow in my design. Unfortunately, the solutions that were effective for Bootstrap 3 no longer apply. You can read more about this issue here. To see the problem in action, check out ...

What is the best way to add animation to my `<div>` elements when my website is first loaded?

I am looking for a way to enhance the appearance of my <div> contents when my website loads. They should gradually appear one after the other as the website loads. Additionally, the background image should load slowly due to being filtered by the wea ...

Reveal concealed content when a responsive table becomes scrollable on a mobile device

I recently completed a project that was overloaded with tables. I made all the tables responsive, but they still take vertical scroll if they don't fit on certain devices due to their varying widths. For instance, Table A requires vertical scroll o ...

What could be the reason behind my image displaying correctly in the majority of browsers, yet not in Internet Explorer

The HAML code below is what I have: %header .grid %a{:name => "top"} .logo %a{:href => root_path} =image_tag("logo3.png") .tagline .clearfloat %p Fast Facts About Your Website, Your Competitors And Best ...

Positioning elements within a Bootstrap column: Fixed versus Absolute positioning

Striving to maintain the absolute positioning of the right column as users scroll through the left column has proven to be a challenging task. Despite exploring various solutions from stackoverflow and google, none seem to effectively address this issue. ...

The div element should stretch to occupy the entire height of the screen

I have structured my webpage with three different divs. The left one is defined with col-md-2, the middle one with col-md-8, and the remaining right div with col-md-2. I want the right div to take up 100% of the screen height, even when no controls are pre ...

Sticky positioning causing issue with Bootstrap dropdowns

I'm attempting to design a data table with a sticky column that includes a Bootstrap dropdown button. Unfortunately, the dropdown conflicts with the sticky position and ends up opening behind the fixed column. It looks fine when opened from the top en ...

Deciding whether to use Device WebView Kit or HTML for your project is

I am a bit confused about creating a website for mobile devices. If I have a specific webpage in mind that I want to target towards mobile devices, do I need to use the native webview kits provided by iOS, or can I simply code in HTML, PHP, Javascript, an ...

Tips for Including a Parallax Image Within a Parallax Section

Currently, I am working on implementing a parallax effect that involves having one image nested inside another, both of which will move at different speeds. My progress has been somewhat successful, however, the effect seems to only work on screens narrowe ...

I am looking for guidance on removing the bottom line from the ionic 4 segment indicator. Any advice or tips on

.segment-button-indicator { -ms-flex-item-align: end; align-self: flex-end; width: 100%; height: 2px; background-color: var(--indicator-color); opacity: 1; } I am a beginner in hybrid app development and ...

Measure with an "em" unit indicated by a dot

Could you clarify if there is a distinction between writing padding:.6em and padding:0.6em; and if they have the same value, why not just use padding:0.6em; ? Thank you ...

Use the mouse scroll to navigate and scroll a vertical-rl div from right to left

Here I have created a vertical-rl environment; (function() { function scrollHorizontally(e) { e = window.event || e; var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))); document.documentElement.scrollLeft -= (delta * 50); ...

JavaScript multiline variable declaration

In the context of a multi-lined variable in AJAX Chat, I am attempting to add an image before other elements are set. However, the issue I am encountering is that the image and text are always displayed on separate lines. It seems to be more of an issue wi ...

Attempting to integrate the Angular2 module text-mask-addon into the project

Currently, I am in the process of integrating text-mask-addons into my Angular application. You can find more information at https://github.com/text-mask/text-mask/tree/master/addons Despite attempting to follow the npm links suggestion, I am encountering ...

What steps can be taken to enhance the responsiveness of this Bootstrap 4 code?

I've been trying to make this code more responsive, but I haven't found a suitable solution yet. When the browser window shrinks to mobile sizes, the list becomes too narrow and the picture/video items on the right end up small and aesthetically ...

Is the "position: sticky" feature functioning correctly, or is there a slight vibrating or dancing effect when users scroll through the Ionic app? How can we eliminate this issue specifically on iOS

Looking for suggestions on this problem - the position sticky is functioning correctly, but there seems to be a slight vibration or dancing effect when users scroll in the Ionic app. Any ideas on how to eliminate this issue specifically on iOS devices? & ...

Exploring through a table using JavaScript

I am struggling to search a table within my HTML for a specific term. The code I have works to an extent, but it does not account for alternatives such as searching for "what is that" when I type in "What is that". Additionally, I need the script to ignor ...

How can I transform an HTML div into a video file like MP4 using Python and Django?

I'm looking to take a HTML page and target a specific <div> within it in order to convert it into video format. Purpose: I understand that HTML is typically static, but I have a requirement to transform it into a video. I'm seeking method ...

Can PHP retrieve data when the form submit function is overridden with AJAX?

I have customized the .submit function of a form on this webpage. It is being loaded inside the #mainContent in an "index.php" and I want the Submit button to only replace this #mainContent. My goal is to retrieve data from this form and send it to a .php ...