Max-height is disregarding the CSS transition

My goal is to implement a CSS transition that triggers when a button is clicked, causing it to fade out and disappear while another element simultaneously takes its place.

I have almost achieved the desired effect, but there's an issue. When I add the .hide class to the button, it seems to "jump" (the height property should not change until the opacity animation completes). Additionally, the max-height property doesn't seem to respect the CSS transition properties I've defined in the stylesheet.

You can view the code example on Codepen: https://codepen.io/basickarl/pen/zwzNXM?editors=1111

Below is the HTML structure:

<div class="app-profession">
  <button id="one" class="add-profession" onclick="func()">
    <img/>
    Click me!
  </button>

  <div id="two" key="pickArea" class="pick-area">
    Here! 2
  </div>
</div>

This is the SCSS styling for the elements:

button.add-profession {
    width: 500px;
    padding-top: 16px;
    padding-bottom: 16px;

    display: flex;
    justify-content: center;
    align-items: center;

    font-family: 'Verdana';
    font-size: 18px;
    font-weight: 300;
    text-decoration: none;
    color: black;
    outline-style: none;

    border: none;
    border-radius: em(0);

    opacity: 1;
    background: lightblue;

    img {
        margin-right: em(5);
        content: url('static/images/add_cross_white.png');
        height: em(30);
    }

    transition: opacity 2s ease-in-out 0s, max-height 0s linear 2s, padding 0s ease-in-out 2s;
    &.hide {
        opacity: 0;
        max-height: 0; // <--- here
        padding: 0;
        overflow: hidden;
    }

    &:hover {
        background-color: red;
        cursor: pointer;
    }
}

.pick-area {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: green;

    opacity: 0;
    max-height: 0em;
    overflow: hidden;
    width: 500px;
    padding: 16px;

    transition: all 2s linear 2s;
    &.show {
        max-height: 500px;
        opacity: 1;
    }
}

And finally, the JavaScript function triggering the transitions:

function func() {
  document.getElementById('one').classList.add('hide');
  document.getElementById('two').classList.add('show');
}

Answer №1

To prevent the button from jumping, simply apply a max-height to the button.add-profession class.


button.add-profession {
    width: 500px;
    padding-top: 16px;
    padding-bottom: 16px;
    max-height: 60px;             /*  added this property  */
    ....

Check out the updated codepen here

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

What is the best way to align a box once another one has been placed?

I have integrated both Bootstrap and Masonry plugins into my website design. The issue I am facing is that the Masonry plugin box goes under the top Bootstrap bar. I tried adding a margin-top: 50, but this resulted in a horizontal scroll bar appearing. To ...

What is the method for choosing multiple IDs using CSS?

Is there a way to target multiple IDs in CSS? For instance: #test_id_*{ } <div id="test_id_10"></div> <div id="test_id_11"></div> ...

The CSS ::after selector is experiencing a decrease in animation speed

There is a dropdown menu set to fade in once a link is clicked. Everything works well, the menu fades in properly. However, when clicking off and triggering a function that fades out the dropdown, the triangle on top of the box fades out slightly slower th ...

Is there a way for me to determine if a user has engaged with a form?

I'm surprised by how difficult it was to find information on this topic through Google. Currently, my struggle lies in wanting my form fields to change color based on validity only after the user has interacted with the form. I don't want invali ...

CSS Flexbox: Achieving Perfect Alignment for a Responsive Hamburger Navbar

This is a specific inquiry that requires your attention To begin, kindly execute the code in fullscreen mode Inspect the code and adjust it to fit within a mobile width (there seems to be an issue with responsiveness) Next, open the hamburger menu As show ...

Only one div is revealed by Jquery show and hide, while the other remains hidden

I'm attempting to utilize a single href link to toggle the visibility of two other divs. The first div should be visible by default, while the second div remains hidden. <a href="#ben1" class="fa fa fa-times closer" > <p> clickab ...

Split into two lines, the Bootstrap carousel includes 28 indicators for easy navigation

I am currently using a Bootstrap 4 carousel with 28 pictures. However, I am facing an issue where the indicators on small and medium devices are not displaying properly (some indicators seem to be missing). They are not breaking into new lines as expected. ...

Can CSS modules be used in conjunction with outside libraries?

I've incorporated a Slider library () into my React project. While regular CSS allows me to style the slider properly, I am facing issues when using css modules. The tsx file looks like this: import styles from './styles.module.css'; . . . ...

How can I make tooltipster display tooltips properly?

I have been struggling to customize tooltips using a library called tooltipster. Here is what I currently have: Head of index.html: <head> <!--TOOLTIP CSS--> <link rel="stylesheet" type="type/css" href="node_modules/tooltipster-master ...

Styling with CSS and strategic placement

I'm having an issue with my form elements - specifically a text input and a submit button. I want them to display inline, but the submit button is appearing slightly above the input field. I tried placing both elements in a div and setting their maxim ...

Looking for a drum set with clickable buttons? Having trouble changing the background color in CSS/HTML? Wondering how to map keyboard keys to HTML buttons?

Behold my HTML creation: <H1> <center> EPIC GUITAR JAM </center> </H1> <img class="guitar" src="guitar.jpg" /> <button class="strum" onclick="Strum()"> Strum Chord </button> <button class="pluck" o ...

What could be causing this code to malfunction on jsfiddle?

Why doesn't this code from W3schools work on jsfiddle? I tried to implement it here: https://jsfiddle.net/u6qdfw5f/ <!DOCTYPE html> <html> <title>W3.CSS</title> <meta name="viewport" content="width=device-width, initial ...

Discovering the exact measurement of "remaining space" in absolute units with flexbox techniques

I am faced with a dilemma that is leaving me uncertain. Within a flexbox layout, specifically column-oriented, there are three children: top, middle, and bottom. The challenge arises in the middle child where I must embed a third-party component that requ ...

The custom CSS cursor appears to be displaying in a pixelated manner

Trying to experiment with custom cursors, but every attempt ends up pixelated. I created a ring asset to demonstrate the issue. Here is the cursor image file. cursor: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/272259/circle.png), auto; Although it ...

Having trouble with applying a custom class to React Bootstrap tabs?

When utilizing the react bootstrap tabs component, I encountered an issue with implementing a custom CSS style for the nav-link element within it using a customized parent class indicator. <Tabs defaultActiveKey="signup_renter" i ...

The backdrop takes precedence over all other elements

The issue I'm facing is that the other Div is not appearing above the background. My goal is to have the background fill the entire screen. <title>The Ultimate Cube World Resource!</title> <link href="style.css" rel="stylesheet" type=" ...

Chrome and Firefox browsers are not supporting HTML simple anchor links

Creating a portfolio page at this link (http://198.96.94.51/v2/) and encountering an issue with the navigationMenu. When rapidly clicking on the links on the side, they do not redirect to the correct anchor point (some don't move the page at all). I h ...

JavaScript or jQuery can be used to rearrange the position of child divs within parent divs with a single action

I am facing a dilemma with 5 identical divs, each containing text and an image. My goal is to rearrange the child divs so that the text comes after the image in each article. Put simply, I want the image to be displayed above the text in all instances. Al ...

Using the calc function to define input width may not yield the expected results

I am facing an issue where I have an input element with width: calc(100% - 100px); and no padding/margin/border. Next to this input, I want to place a div with position: inline-block; and width: 100px;. The problem is that the div is going to the next lin ...

Selectors in PHP DOMDocument that mimic jQuery's functionality

Currently, I am working with a DOMDocument and I am curious if there is a way to utilize CSS-like selectors to choose nodes similar to how it is done in jQuery. For instance, let's say I am analyzing an XML file and a portion of it appears as follows ...