Adjust and resize external content through transformation scaling

I have a div element that I want to scale from 0 to 1, while keeping the content around it in place. How can I make the div grow and adjust the surrounding content?
Access JSFiddle example here

.transform {
        background-color: slategray;
      }

      .transform:hover~.div1 {
        transform: scaleY(0);
      }

      .div1 {
        transform: scaleY(1);
        background-color: royalblue;
        padding: 15px;
        transition: transform 2s;
        transform-origin: top center;
      }

      .div2 {
        background-color: springgreen;
        font-size: 20px;
      }

      img {
        height: 200px;
      }
      
    
<div class="transform">TRANSFORM</div>
      <div class="div1">
        <img src="https://via.placeholder.com/500">
      </div>
      <div class="div2">
        <span>TEXT</span>
      </div>
      
    

Answer №1

By adjusting the height of .div1 instead of simply scaling it, you can ensure that the rest of the page adjusts accordingly.

.transform {
  background-color: slategray;
}

.transform:hover~.div1 {
  height:0; padding-top:0; padding-bottom:0; /* make necessary adjustments to height and padding */
}

.div1 {
  background-color: royalblue;
  padding: 15px;
  height: 200px;
  transition: padding-top 2s, padding-bottom 2s, height 2s; /* apply animation to padding as well */
  transform-origin: top center;
}

.div2 {
  background-color: springgreen;
  font-size: 20px;
}

img {
  width:200px; height: 100%; vertical-align:top; /* additional adjustments here */
}
<div class="transform">TRANSFORM</div>
<div class="div1">
  <img src="https://via.placeholder.com/500">
</div>
<div class="div2">
  <span>TEXT</span>
</div>

I hope this solution is helpful!


Update: The problem has evolved based on comments from the OP. Here is a new snippet addressing the revised issue.

.transform {
  background-color: slategray;
}

.transform:hover~.div1 {
  transform: scaleY(0);
}

.transform:hover~.div2 {
  top:-230px;             /* Adjust div2 by moving it up by the combined height and padding of div1 */
}

.div1 {
  transform: scaleY(1);
  background-color: royalblue;
  padding: 15px;
  transform: scaleY(1);
  transition: transform 2s;
  transform-origin: top center;
}

.div2 {
  background-color: springgreen;
  font-size: 20px;
  position: relative;  /* using relative positioning */
  top: 0;              /* Initial position remains the same */
  transition: top 2s;
}

img {
  height: 200px;
  vertical-align:top;
}
<div style="margin-top: 300px">TEXT</div>
<div class="transform">TRANSFORM</div>
<div class="div1">
  <img src="https://via.placeholder.com/500">
</div>
<div class="div2">
  <span>TEXT</span>
</div>

Answer №2

To solve this issue, you can consider utilizing the max-height property to modify the dimensions of your div instead of using the transform property.

.adjust-size {
  background-color: slategray;
}

.adjust-size:hover~.container {
  max-height: 0;
}

.adjust-size:hover~.container img {
  max-height: 0;
}

.container {
  max-height: 600px;
  background-color: royalblue;
  padding: 15px;
  transition: max-height 0.5s;
}

.content {
  background-color: springgreen;
  font-size: 20px;
}

img {
 max-height: 200px;
 transition: max-height 0.6s;
}
<div class="adjust-size">ADJUST SIZE</div>
<div class="container">
  <img src="https://via.placeholder.com/500">
</div>
<div class="content">
  <span>CONTENT</span>
</div>

Answer №3

If you swap out transform: scaleY(0); with height: 0px;, you should achieve the same result while also affecting the div below. I've included an example below to illustrate this.

.container {
  display: flex;
  flex-direction: column;
}

.transform {
  background-color: slategray;
}

.div1 {
  background-color: royalblue;
  padding: 15px;
  transition: all 2s;
  height: 200px;
}

.transform:hover~.div1 {
  height: 0px;
  padding: 0px;
}

.div2 {
  background-color: springgreen;
  font-size: 20px;
}

img {
  height: 100%;
}
<div style="margin-top: 150px">

</div>

<div class="container">
  <div class="transform">TRANSFORM</div>
  <div class="div1">
    <img src="https://via.placeholder.com/500">
  </div>
  <div class="div2">
    <span>TEXT</span>
  </div>
</div>

<div style="margin-top: 200px">

</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

Handle all link clicks on the webpage

My challenge is that some users are required to utilize a virtual desktop in order to access specific information on my website. However, within the virtual environment, there are links leading to external content that do not function properly. I am seekin ...

Launching an HTML document with checkboxes already selected

I am attempting to load my HTML page with checkboxes already checked if they were selected on the previous page. The code I currently have is shown below: {% if job.activism %} checkbox logic goes here {% endif %} <br><input type="checkbox ...

Notify the ajax success if the returned data is null

Is there a way to display an alert message in the AJAX return request if no data is present? I have attempted this in the AJAX success function, but it seems to not be working! This is my current script: <script> $(document).ready(function () ...

What is the best approach for addressing this problem using Mobile HTML5?

I am currently in the process of developing a Cordova application utilizing jQuery Mobile. In my application, there is a functionality that displays an array of words to the user using a for loop. <div id=page1div></div> <script> $pa ...

How come my initial heading isn't aligned in the middle like the subsequent one?

I am having trouble centering my title Learn About Us correctly, similar to the title What we do. It seems there is an issue with the alignment of the picture, and I'm not sure how to fix it. https://i.sstatic.net/grWdK.png I need help understanding ...

How can I center an image within another image in a two-column layout?

Hey there, I'm working on a 2-column layout where I need to position an image in the center of another one in the second column. However, my attempts at styling seem to be failing as the image keeps showing up in the top left corner of my page... Her ...

Add a class by utilizing the :class attribute

I reviewed the documentation, but I am still struggling to implement this in my project. Initially, I simply want to display a specific class using vue.js that I can later modify dynamically. I just need to establish the default behavior of that class, so ...

Tips for eliminating the border on a Twitter widget

Looking to integrate a Twitter widget into my website. The site is built using Smarty and I want to customize the widget. Currently, there is a scroll bar on the right section that I would like to remove by adding a style overflow:hidden to the class strea ...

ClickAwayListener in MUI has the potential to impact all the elements within a popper when attempting

As a new react developer, I have encountered a small problem with my ClickAwayListener. It should close the Popper component when clicking 'x' or outside of it, which it does successfully. However, I have another component inside my Paper, and wi ...

Is there a way to link to a specific section within a webpage using its ID, landing halfway down the page instead of at the top?

Can someone help me out with this issue? The header on my page is fixed at the top and has a height of 75px. When I click on a link (<a href="/company/#contact/">contact</a>), it takes me to the right section but the top part is hidden behind t ...

Issues with jQueryUI sortable causing flickering and erratic movement while attempting to reorder and position items within the list

When using jQueryUI sortable with a long list of items, I encounter an issue where the items flicker and jump around the screen as I try to change their order by dragging them. This makes it nearly impossible to organize the items effectively. It seems li ...

Guide on exporting Excel data using Angular and TypeScript

My project involves managing a table of information that includes fields for FirstName, LastName, PhoneNumber, Age, and Date. I've created a function that allows me to export the data to an Excel file, but I only want to export specific fields like Fi ...

Footer that sticks in a vuejs application

I am facing a challenge in implementing a sticky footer in my vuejs application. Vuejs and similar frameworks require the presence of a root element in the template. This requirement is causing complications when trying to use Flex to create a sticky foo ...

JavaScript math validations not functioning correctly

This new project I've been working on involves presenting a multiplication problem and notifying the user if their answer is correct through an alert. The issue arises when the program incorrectly verifies the answers. Take a look at the code: < ...

Margin Snow Stripe

Having trouble troubleshooting a margin error that won't go away? I've attempted adjusting the margin and padding values to 0, but the line persists. Should I consider resizing the video? I placed the background video within a div tag. Is the vi ...

Tips and Tricks for Resizing Images within an Angular iframe

Can anyone help me figure out how to make the image content inside my iframe fit perfectly within the frame? I noticed that when displaying a PDF it scales correctly, but when displaying a PNG image it does not scale properly and looks distorted. Any ass ...

To create space between the input field and button, utilize the input-group class for

Is there a way to insert a space between the input field and the button while using input-group in Bootstrap 3? Check out this jsFiddle for more information. <div class="container"> <div class="row"> <div class="col-md-12"> ...

What is the best way to extract the frameset from a frame window?

Here is a code snippet to consider: function conceal(frameElem) { var frameSet = frameElem.frameSet; //<-- this doesn't seem to be working $(frameSet).attr('cols', '0,*'); } //conceal the parent frame conceal(window.pa ...

embedding HTML code directly into an iframe

Can HTML be directly inserted into an iframe within my HTML template? Would the following code be considered valid? <iframe> <html> <head> </head> <body> <p>Hello world</p> </body> ...

Issues with CSS animation functionality have been detected in the Edge browser

In this particular div, I have two spans enclosed. I've created a loader which features a circle filling up with red color repeatedly. While everything appears to be functioning smoothly in Google Chrome, there's a noticeable glitch when it comes ...