Trouble with CSS 3 animations arises if dimensions such as width or height are specified

When I apply a width-transition to a div element, the animation works perfectly. However, if I provide both height and width to the same div in CSS, the animation stops working. What could be the issue?

On hover, neither color nor animation changes. How can I apply common styling for all divs:

  1. Default styling
  2. Styling on hover

Check out this JS Fiddle example, and see snippet below:

#div2 {

/*Un-commenting the below part, fails the animation assigned to this div
    width:100px;
    height:50px;
*/
    -webkit-transition:width 3s;
    background-color:blue;
}

div {
    width:100px;
    height:50px;
    background:red;
    color:white;
    font-weight:bold;
    -webkit-transition:width 2s;
}

 #div1 {
    -webkit-transition-timing-function: cubic-bezier(0, 0, 0.25, 1);
}
#div2 {
    -webkit-transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
}
div:hover {
    width:300px;
    color:black;
}

Answer №1

Your issue is related to the specificity of selectors, rather than just the presence of a width or height property. In this case, a more specific rule takes priority over a less specific one. In the example provided, a rule with an ID will take precedence over a rule with an element (DIV) and pseudo-selector (:hover). For instance, when considering the scenario where the ID width in #div2 is commented out, the width for div2 would be determined by the rules for div and div:hover. When not hovering, only the rule for div applies, resulting in a width of 100px. However, when hovering, the rule for div:hover, which is more specific, overrides the previous value with a width of 300px. When uncommenting the width in #div2, there are now three rules to consider: div, #div2, and div:hover. Even though both div and #div2 have a width of 100px, #div2 wins due to its higher specificity. This means that even when hovering, #div2 retains a width of 100px, resulting in no visible animation. To resolve this issue, you can create a rule for #div2:hover to ensure a different width on hover, or alternatively, use a .blueDiv rule instead of #div2 to maintain specificity without conflicting with the hover rule.

Answer №2

When the styling specified in the ID takes precedence over the previous tag styling, it also overrides any hover effects.

If you are setting different dimensions for the ID, you must also define its hover width specifically for that ID.

In your scenario, it would look something like this:

#div2{
width:100px;
height:50px;
-webkit-transition:width 3s;
background-color:blue;
}

#div2:hover{
    width:300px;
}

Check out the example in your updated jsfiddle

Answer №3

While it may seem like a mistake, I found that using classes instead of IDs to assign properties actually worked for the animation and various style attributes such as font color, width, and height.

It appears that using IDs to provide properties can be overridden, so it's generally safer to stick with classes.

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

I'm looking for a solution to implement a vertical carousel in Google's Materialize CSS without the need for custom development

Looking to create a unique vertical scrolling "carousel" using Google's Materialize CSS library. I have a good understanding of the steps required to construct a carousel. # haml %ul.carousel %li.carousel-item Some Content %li.carousel-item ...

Is there a way to add a gradient of colors to the background?

I'm looking for instructions on how to achieve a multi-color background screen similar to the image I have provided. Can anyone help with this? ...

Experience the captivating effect of jQuery's fade in animation as one image gracefully

I have an 800x500 image where the bottom two quadrants feature edits to the graphics that I want to fade in gradually. If we divide the whole image into AB CD Sections C & D are what will be covered up after 10 seconds with two images fitting in the bott ...

Making sure to correctly implement email input fields in HTML5: Surprising behaviors observed with the email input type in the Chrome browser

Within the upcoming code snippet, a basic email validation is implemented. An input field's background color will display as white for valid emails and yellow for incorrect values. This functionality operates seamlessly in Firefox; however, in Chrome, ...

Choosing Between EMs and Pixels for Font Sizes: A 2011 Perspective

In the past, EMs were favored over pixels for font size because they could scale with IE6 while pixels could not. Nowadays, modern browsers can handle pixel-sized font scaling correctly. Another advantage of EMs is that they cascade, unlike pixels. But if ...

Jquery loader for loading html and php files

My html and php pages contain a significant amount of data which causes them to take a few extra seconds to load from the server. I was wondering if there is a way to implement a "loader animation" for these pages using jquery. The idea is for the loader a ...

Part II: Material-UI - Finding the Right Source for CSS Classes

I have recently started diving into Material UI - Grid and this question is a continuation of my previous query about defining CSS classes for Material UI components, specifically the classes.root Sunny day with a chance of rain I made some changes b ...

Is there a way to modify a component's CSS by using a global CSS class name in Angular?

We have implemented a system where a class on the html element determines whether the user is in dark or light mode of the application. <html class="dark-mode"></html> This class is dynamically added using Renderer2 in a service that detects ...

The background banner is failing to display within the divbox

Having some trouble with my top banner in the HTML and CSS code I'm using to create a master page for ASP.NET. Oddly, the banner does show up on the design tab in Visual Studio, but it's not displaying properly. Here's the relevant code, can ...

I continue to encounter an "Anticipated bracket" and "Surprising symbol" malfunction

I am currently working on a website using Wordpress and I am facing a problem with making the header full-screen while maintaining responsiveness. I want the code to only apply to desktop PCs and tablets, but I am encountering errors such as expected bra ...

How to customize TextField error color in Material-UI using conditional logic in React

Currently, I am incorporating the React Material-UI library into my project and faced with a challenge of conditionally changing the error color of a TextField. My goal is to alter the color of the helper text, border, text, and required marker to yellow ...

Experience unique website functionalities across Windows/Android and Apple ecosystems

Apologies for any language errors in advance. I'm facing an issue where my website appears differently on Safari or Chrome on iOS/MacOS compared to Windows 10 or Android devices. If you observe the List of Receiving Countries in the images provided: ...

What are effective strategies to stop the generic * { … } style from overriding its parent tag?

How can I prevent my general * { ... } style from overriding the parent tag? In the given scenario, I want the text "sssssssss" to have the style text-huge. .text-huge { font-size: 28px !important; color: green !important; } * { font-size: 12px; ...

Ways to align grid components at the center in MUI frameworks?

I am brand new to using MUI and currently experimenting with the grid functionality. <Grid className={classes.grid} container spacing={{ xs: 2, md: 3 }} columns={{ xs: 4, sm: 8, md: 12 }}> {data.data.map((item, index) => { ...

Having trouble implementing a circular progress bar with a custom Tailwind CSS library, the desired effect is not being achieved

I am on a mission to create a circular progress indicator. Recently, I stumbled upon the daisyui library and its component called radial progress - The documentation mentions that: "Radial progress requires the use of the --value CSS variable." ...

Ways to obtain the <a> using the title attribute

Is there a way to remove 3 classes from a specific <a> element that is dynamically generated? The only constant is the title. How can I locate an element based on its title? I've searched online but haven't found exactly what I'm looki ...

Align the content of the list items to the center and position the list in the center of

I am struggling to get the List centered beneath "Contact Me" and the hr tag. Both of those elements are perfectly centered in the column, but the list and icons are aligned to the left. Ideally, I would like them to be in the center like everything else. ...

Experiencing Difficulty Retaining Checkbox State Using LocalStorage Upon Page Reload

At the moment, I have a script that automatically clicks on a random checkbox whenever the page loads or refreshes. Through localStorage, I can also view the value of the input assigned to the randomly selected checkbox. However, I'm facing an issue ...

Consistently showcasing images of varying dimensions that are unfamiliar

Currently, I'm in the process of developing a small web application that uses the Spotify API to fetch and showcase top tracks and artists to users. Each track or artist is presented within a Bootstrap panel, with the title displayed in the header an ...

The Bootstrap Dynamic Navbar is not adjusting for mobile devices because of the addition of a logo

Struggling to incorporate a logo into the navbar of my website. It looks good on larger screens, but causes the navbar to break on smaller screens. View without the logo: Click Here View with the logo and resizing issues: Click Here I think I can make th ...