Utilizing exclusively CSS for parent div, achieve dynamic resizing animation

I want to create a transition effect on the parent div when the child div disappears without using display:none

My goal is to have a dynamically centered div that animates using CSS transitions only.

Here is my current approach.

document.querySelector('#one').onclick = function () {
    document.querySelector('#two').classList.add("hidden")
}
#content {
    transition: all 0.5s ease;
}

.centered {
    position: fixed;
    top: 50%;
    left: 50%;
    margin-right: -50%;
    transform: translate(-50%, -50%);
}

#one, #two {
  width: 300px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

#one {
  background-color: cyan;
}

#two {
  background-color: magenta;
}

.hidden {
  display:none;
}
<div id="content" class="centered">
  <div id="one">PRESS ME</div>
  <div id="two">TO MAKE ME DISAPPEAR</div>
</div>

UPDATE: The solution does not have to use display:none

Answer №1

To achieve a transition effect using display:none, you need to try using height instead. The transition should be applied to the element you are hiding, causing the parent div to shrink as the element is no longer visible. Here's an example:

document.querySelector('#one').onclick = function () {
document.querySelector('#two').classList.add("hidden")
}
.centered {
position: fixed;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
}

#one, #two {
  width: 300px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

#one {
  background-color: cyan;
}

#two {
  background-color: magenta;
  transition: all 0.5s ease;
}

#two.hidden {
  height:0;
  overflow:hidden;
}
<div id="content" class="centered">
  <div id="one">PRESS ME</div>
  <div id="two">TO MAKE ME DISAPPEAR</div>
</div>

If you want the parent div to be animated, adjust the height of the parent to accommodate the hidden element. Set the #content div with a height of 300px and transition it to 150px. Add the hidden class to the #content div and set #two to have a display of none. Here's how you can do it:

document.querySelector('#one').onclick = function () {
document.querySelector('#content').classList.add("hidden")
}
#content {
transition: all 0.5s ease;
  border:5px solid #000;
  height:300px;
}

.centered {
position: fixed;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
}

#one, #two {
  width: 300px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

#one {
  background-color: cyan;
}

#two {
  background-color: magenta;
}

#content.hidden {
  height:150px;
  overflow:hidden;
}

#content.hidden #two{
  display:none;
}
<div id="content" class="centered">
  <div id="one">PRESS ME</div>
  <div id="two">TO MAKE ME DISAPPEAR</div>
</div>

Answer №2

Steve's insightful answer led me to discover a solution to the issue at hand. I've slightly modified his solution by setting the transition to height, causing opacity: 0 to take effect instantly.

By doing this, I was able to address the problem effectively. The height transition requires a predefined height (which we already have for the child element) to facilitate the transition animation. Additionally, utilizing opacity: 0 for instant invisibility of the content cleverly centers the parent content automatically.

document.querySelector('#one').onclick = function () {
document.querySelector('#two').classList.add("hidden")
}
.centered {
position: fixed;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
}

#one, #two {
  width: 300px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

#one {
  background-color: cyan;
}

#two {
  background-color: magenta;
  transition: height 0.5s ease;
}

#two.hidden {
  height:0;
  overflow:hidden;
  opacity: 0;
}
<div id="content" class="centered">
  <div id="one">PRESS ME</div>
  <div id="two">TO MAKE ME DISAPPEAR</div>
</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

Transferring data from a form to a function in JavaScript

Hey there! I've been working on a form that sends a value to a new window, but for some reason, the value val2 is showing up as null in the new window instead of being passed. Here's my code: function sendValue(value) { ViewImg = window.ope ...

Convert the MySQL DATE column from numerical month to full month name

I'm having trouble figuring out a simple solution. I'm using the code below to generate a tree grid from a MySQL database. $sql2 = mysql_query("SELECT DISTINCT YEAR(date) FROM blogs ORDER by YEAR(date) desc")or die(mysql_error()); $archive .= "& ...

The Bootstrap modal stubbornly refuses to close even after resetting the HTML body

I am having an issue with my bootstrap modal where the closeModal button is not functioning properly after the printModal button has been clicked. The modal does not close as expected. Step 1: Click on the printModal button after the modal pops up (this w ...

Having difficulty decoding audio streams in Flask Socket.IO due to a file received by the Python server

Thank you for taking the time to go through this post. I have been diligently working on a project that involves capturing audio input from the client side and then sending the recorded audio to a Python server for analysis. However, I have hit a roadblock ...

Having trouble getting the header div to span the entire screen

I'm currently working on a project where I have set the header div to 100% width and the main container to also be 100% width. However, I am facing an issue where the right and left sides of the header are not filling the entire space. I managed to fi ...

Developing a personalized file upload button in React

I have been working on creating a custom <input type="file"> upload button in React. My goal is to display the name of the uploaded file on the button itself after the upload. I encountered some challenges while trying to create a codepen demo, so I ...

What is the best way to crop a background image for optimal lazy loading?

Just a quick query here - what's the best way to cut a background image for optimal lazy loading? I have an image that's 1920px wide and 5100px tall. I'm trying to figure out the best approach to cutting and loading it efficiently. Is my i ...

Implementing hover effect triggered by keyboard input

Is there a way to create a hover effect on a div when a specific key is pressed on the keyboard using jQuery? <div id="d-up" class="button"></div> Appreciate any help with this! Thank you. ...

Illustration: Fixing a CSS Issue

After implementing Technique #8 from the Nine Techniques for CSS Image Replacement, I am not getting the desired results. Instead of correctly positioned images, the output is not what I anticipated. Here is a link to see for yourself: I made a modificati ...

How can I design unique navigation menus using HTML, CSS, JavaScript, and jQuery?

Is there a way to create menus where clicking on a holiday/seasonal category opens up all related lists automatically? For example: https://i.sstatic.net/Nctd1.png I've been searching online for submenu options but haven't found the right metho ...

Creating a dynamic height sticky sidebar and footer in Bootstrap 5 using CSS/HTML

Looking to implement a sticky sidebar feature with a header and footer that remain visible while the middle content scrolls. The overall height of the page will vary, so I plan to use Bootstrap 5 grid for structuring. <!DOCTYPE html> <html la ...

What could be preventing my HTML replacement assignment from functioning properly?

Whenever a choice is made, I want to substitute the current content with different content. My current attempt is to update the content from "HuckFinn" to "Tom Sawyer will be added later" when the user selects "Tom Sawyer" from the drop-down menu. However, ...

There seems to be a hitch in the functioning of the JavaScript codes

I'm having trouble calculating the amount using jQuery. Could someone please review my code and let me know what's wrong? Here are my Javascript and jQuery codes: After making changes: $(document).ready(function() { $('.home_banner&ap ...

Ensure that the CSS grid has a minimum span of 3 columns, and if a class object is the only item in a grid row, it

I am currently utilizing CSS grid to create the layout for my web application. I need to specify that a particular class should have a minimum grid-column span of 3 frames and extend across the entire grid row if it is the only object in that specific row. ...

View / Conceal Unordered List depending on link selected

How can I dynamically show or hide UL elements based on the clicked link? Here is a JS Fiddle example: http://jsfiddle.net/zangief007/rj8g0yh4/1/ Below is the HTML code: <ul class="category-1 group"> <li class="name">JOHN Smithe QC</l ...

What is the proper way to encode image URLs for use in CSS?

For the div element in my code, I want to allow users to input a URL that will be applied as a CSS background image, like this: background-image: url("/* user specified URL here*/") I'm concerned about security. How can I properly escape the URL to ...

Implement CSS to layer HTML 5 canvases while including a margin

I have a design conundrum in my app where I am using multiple stacked HTML canvas elements for drawing. My goal is to make the canvases fit perfectly within their containing div while also maintaining a left and bottom margin. This is how my HTML structur ...

Is there a way to seamlessly integrate a PDF file into a KnockoutJS template without triggering any warnings from PDF plugins?

I am attempting to embed a PDF document in an HTML view, with the assistance of a Knockout ViewModel that supplies the URL for the document. I understand that the appropriate HTML element for this task is <object>, so my view looks like this: <di ...

Tips for sorting through various elements or items

How can I improve my filtering function to select multiple items simultaneously, such as fruits and animals, or even 3+ items? Currently, it only allows selecting one item at a time. I attempted using , but it had bugs that displayed the text incorrectly. ...

What is the best way to center elements vertically within a table element?

I'm encountering some issues with my tables. I have created two tables, stacked one below the other, and I am trying to center the elements within them. However, I am facing difficulty in achieving vertical alignment and true centering, as shown in th ...