The only property I am unable to transition is anything other than the width

I have been diligently studying the concepts of transforming, animating, and transitioning. I've completed crash courses, tried several solutions to related problems, but unfortunately, nothing seems to be working as expected.

#tr-w {
  transition: width 1s ease-in-out;
}

#tr-w:hover {
  width: 50%;
}

#tr-h {
  transition: height 2s ease-in-out;
}

#tr-h:hover {
  height: 40vh;
}

#tr-r {
  transition: width 1s ease-in-out, transform 2s ease-in-out;
}

#tr-r:hover {
  transform: rotateZ(180);
  width: 30vh;
}

JSFiddle: https://jsfiddle.net/q976oc0h/1/
CodeSandbox: https://codesandbox.io/s/dark-pine-i0ut5?file=/index.html
CodePen: https://codepen.io/ssssss12518/pen/rNMMZoL

The code doesn't seem to work on any text editor or IDE that I have tried, including Atom (my main text editor).

Answer №1

There are a couple of things happening here.

The transform functions also require a unit: transform: rotateZ(180); -> transform:rotateZ(180deg);

The transition from height:auto; is not straightforwardly supported.
There are several workarounds available. You can refer to examples in this question.

Sidenote: Generally, using transitions on width/height is not recommended for performance reasons. It can trigger reflows/recalculations of the document structure, which can be costly. You may observe that text inside divs gets compressed into multiple lines or shifts significantly.
A common approach is to use transform to resize/grow/unfold elements, similar to how you used it for rotation.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Arial", sans-serif;
}

.container {
  padding: 10px;
  margin-left: 20px;
}

.container > *[class*="bg"] {
  margin-left: 10px;
}

.bg-gray {
  margin: 10px 0px;
  background-color: #cbd5e0;
  border: 3px solid black;
  padding: 20px;
  margin-bottom: 20px;
  width: 30%;
}

#tr-w {
  transition: width 1s ease-in-out;
}

#tr-w:hover {
  width: 50%;
}

#tr-h {
  transition: height 1s ease-in-out;
  height:100px; /*I've added a base heigth so the browser can calculate a starting value */
}

#tr-h:hover {
  height: 40vh;
}

#tr-r {
  transition: transform 1s ease-in-out;
}

#tr-r:hover {
  transform: rotateZ(180deg); /*i've added a 'deg' as unit*/
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8" />
    <title>Transition Animation CC</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">

      <div class="bg-gray" id="tr-w">
        <p>
          Hover over me
        </p>
      </div>
      <div class="bg-gray" id="tr-h">
        <p>
          Hover over me
        </p>
      </div>

      <div class="bg-gray" style="height: 30vh;" id="tr-r">
        <p>
            Hover over me
        </p>
      </div>
    </div>
    <script src="main.js" charset="utf-8"></script>
  </body>
</html>

Answer №2

The reason for the issue is due to the missing default height setting on the #tr-h element.

#tr-h {
  transition: height 2s ease-in-out;
  height:100px;
}

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

Ways to prevent scrolling in Angular 6 when no content is available

I am developing an angular 6 application where I have scrollable divs containing: HTML: <button class="lefty paddle" id="left-button"> PREVIOUS </button> <div class="container"> <div class="inner" style="background:red">< ...

PHP include does not bring in the CSS file

My project has a main.css file stored in the styles folder at the root: #header { background-color: aqua; height: 120px; } Within header.php, there is the following code: <!DOCTYPE html> <html> <head> <link rel="styleshe ...

What is the best way to resolve the error message: "__init__() received an unexpected keyword argument 'book_category'"?

I'm currently working on inputting book data and storing it in my database using Django to create a web library. However, I keep encountering the following error: init() got an unexpected keyword argument 'book_category' The issue seems t ...

Tips for utilizing anchor links in Angular without altering the URL within the Angular framework

I am attempting to navigate from a element with the class "class1" to a div with the class "class2". However, when the link is clicked, the URL changes. This causes an issue where trying to go back results in returning to the current page at a different ...

Creating a personalized validation function for an HTML 5 element

Creating form validation for a custom image selection tool based on html 5 form validation is my goal. The form includes the following elements: <form class="cms-form" action=""> <table width="800"> <tr> <td w ...

Using Javascript, you can create a function that can disable or enable a text link based on

Here is the code snippet showcasing CSS styles and HTML elements: CSS: .enableLink{ font-family:Verdana, Geneva, sans-serif; font-size:12px; color:#069; padding-right:20px; text-decoration:underline; cursor:pointer; } .disableLink{ display:none; fon ...

Create a vertical scrollable feature for the <ul> element when it is set to flex, limiting its height within the specified dimensions

In order to enhance user experience, I have implemented a live search bar that is only visible on mobile screens. For the CSS styling: .search-results { z-index: 9999; width: 80vw; position: absolute; top: 80%; left: 10%; } .search-results li ...

What are the steps to create a "gooey glide" animation using React and MUI?

I am looking to create a unique animation for my list of items on a web page. My goal is to have the items slide in one by one with rapid succession and then slightly shrink once they reach their final position, similar to how pillows might fall or like a ...

Ways to display a block within the visible window by simply clicking on another block

I'm in need of assistance with this issue. Please take a look at the fiddle link below to understand the specific requirement. $(document).ready(function(){ $('a.n-stand-b, a.n-stand-a, a.e-stand-b, a.e-stand-a, a.w-stand-b, a.w-stand-a, a.s-s ...

Modify the sticky menu color upon scrolling

While working on a client's website, I've stumbled upon an issue that has left me scratching my head. I'm looking to modify the color of the sticky menu once it's scrolled. Can anyone assist me in creating a custom CSS code to implemen ...

Enhancing webpage styles with AngularJS

Just starting out with AngularJS and eager to get the best approach to tackle this challenge. Describing my dilemma: I have an anchor element that, when clicked, needs to toggle between classes "show-all" and "hide-all" while also updating the styling of ...

Tips for accessing distinct ng-model values within ng-include within a single HTML document

Consider the following scenario with two HTML files: index1.html <input type="text" ng-model="model.Name"> <!-- Some complex form !--> index.html <div>Journalist's Details: Name:<div ng-include="index1.html"></div> ...

Vertically center the container

I am struggling to center my container div vertically, similar to how it is horizontally aligning with margin: auto;. Despite searching online for solutions, I have not been successful in finding a method that works for me. It is surprising to me that in t ...

How is it possible for a parameter sent via GET to persist through multiple requests?

Within my JSP file, I have implemented an HTML form that utilizes the GET method to send data to my servlet. <form method="GET"> <input name="cmd" type="hidden" value="firstValue"/> ..... </form> Upon triggering another form ...

Navigation Bar Dropdown Menu Not Responding to Clicks

I'm having trouble implementing a dropdown menu in my navigation bar that should appear when the user clicks and disappear when they click anywhere outside of it, similar to Facebook's dropdown menu with options like logout. However, for some rea ...

The input tag led to a twofold increase in output

I'm facing an issue with my output that I'd like to address. I need to halt a specific refresh loop in a particular field text tag, where my search text bar duplicates when a specific div is refreshed. I have implemented AJAX as a refresher in ...

Slider Footer Player for Rdio

I am currently working on replicating the interactive player footer design used by rdio (). When you click on the footer, a panel elegantly slides up to reveal additional content. Another example of this functionality can be seen on the website teehan lax; ...

Tips for defining conditions within the style attribute in JSP

Below is the code that I attempted: <div style="width:85%;"> <input class="formbutt" value="AddNew" title="AddNew" type="button" style="{(${projectEnvironmentBean.divStyle}=='dipslay:none') ? 'display:block' :'display: ...

Utilizing form data to dynamically create HTML content

Seeking a solution to extract data from a form similar to this one: <form id="rendered-form" name="rendered-form"> <div class="rendered-form"> <div class="fb-text form-group field-text-1534368808722"> <label class="fb-text ...

Adjusting webpage zoom based on different screen sizes

Hey there, I ran into an issue with my web page design. It looks great on my desktop monitors, but when I checked it on my laptop, things went haywire. Strangely, adjusting the zoom to 67% seemed to fix the problem. Both screens have a resolution of 1920 ...