Tips for adjusting the CSS % unit to indicate height rather than width

I am attempting to create a CSS animated dropdown/expandable component. At present, I have classes for expanded and collapsed states defined as shown:

.expanded {
  margin-top: 0;
}
.collapsed {
  margin-top: -100%;
}

However, when I apply these classes based on the dropdown state, it appears that -100% refers to -100% of the element's width, rather than its height as intended. This results in the dropdown not collapsing fully when the element's height exceeds its width. Is there a way to adjust the dimension to which the % unit points?

If not, what alternative approach would be more effective for animating the expansion/collapse of a container?

Here is an image showing the computed sizes of my collapsed element (with margin-top: -100%): https://i.sstatic.net/lybDS.png

Answer №1

Your approach seems a bit unconventional. You could consider using the standard max-height method, or even better, utilizing grid.

.dropdown {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.5s ease-out;
}

 .expanded {
    max-height: 500px; 
}

Although this technique is simple, it does have its limitations as you must determine the maximum height your content will require.

For a more flexible and organized solution, try implementing grid-template-rows

.container {
    display: grid;
    grid-template-rows: 0fr auto;
    transition: grid-template-rows 0.5s ease-out;    
}

.container.expanded {
    grid-template-rows: 1fr auto;
}

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 switch out an image every 5 seconds?

I am trying to make three images in my HTML code change every five seconds, but for some reason, it's not working. Can anyone help me figure out why? var images = []; images[0] = ['photoFromInternet']; images[1] = ['photoFromInternet2 ...

Switch between the table data elements in an .hta document

The response provided by Dr.Molle in a previous post here was accurate, but it only functioned with <div>. I am required to utilize <table>. Though I found a script that works flawlessly outside of my VBScript, it does not work when embedded in ...

How can I update the color of table rows after the ng-repeat has been implemented?

My current project involves Django, Python, and a bit of AngularJS. I have a dynamic history table that grows as data is added. I am looking to apply some simple CSS to this table - specifically, I want to give every even-numbered row a black background ...

How can I implement a search box on my HTML website without relying on Google Custom Search?

Greetings to all! I am currently managing a website filled with HTML content. I am looking to incorporate a search box into the site. After researching on Google, most of the results point towards using Google Custom Search. However, I require a search box ...

Connecting two distinct HTML pages to two disparate CSS files

Recently, I encountered an issue while working on my website in Dreamweaver. The problem arose when trying to link a CSS file to an HTML page. While the index.html page was successfully linked to style.css and displaying correctly, the Products.html page ...

How to make stylish block buttons with ReactJS and Bootstrap

I'm looking to make block buttons that fill the entire div. In React, I'm using the react-bootstrap package to achieve this. Below is the code snippet I have implemented: <Container> <Row className="justify-content-center"& ...

ASP Table extends into adjacent column

Having some trouble with the table layout here. In the screenshot, you'll notice a grid view within another gridview. There are currently 3 grid views - the main one and one in each table. I'm looking to move the description to the Price 2 column ...

Guide to implementing proportional height for an element using JQuery 3.3.1

I am currently working on dynamically setting the heights of elements with a specific class using JQuery in order to achieve a 16:10 ratio based on their width. However, I'm encountering an issue where the height doesn't seem to be applied and re ...

Setting the font size base for my npm package: A guide

In my current setup, I have two main projects with different base font sizes - one at 14px and the other at 10px. Both of these projects utilize Tailwind CSS. The issue arises when using classes like text-base and text-sm for font sizes within each project ...

What is the CSS background-color for the header of a nested table?

HTML: <table id="t1"> <th>test</th> <tr> <td> <table id="t2"> <th>test2</th> </table> </td> </tr> </table> CSS to style the background ...

Generate selectable values from JSON AJAX response using JavaScript library jQuery

Here is the data that needs to be transformed: data = [ {"localidad_cve":"1","localidad_nombre":"AZCAPOTZALCO"}, {"localidad_cve":"10","localidad_nombre":"CRUZ BLANCA"}, {"localidad_cve":"1","localidad_nombre":"VENUSTIANO CARRANZA"} ] The des ...

Issue with alignment of sidebar and main content in Bootstrap design

I'm currently in the process of transforming an existing website (and making it responsive) with the help of Bootstrap (4.3.1) - all while teaching myself how to use Bootstrap effectively. Firstly, there's a top navigation bar followed by the mi ...

Guide to Including Captions and Spans in a Table

In the given HTML code, nested tables are used by the developer which cannot be changed. However, there is a requirement to replace the table class and add a caption and span only to the main table. <table class="two_column_layout" align="center"> & ...

Utilize a single CSS class or theme to style multiple boxes within a stack - Material-UI

I'm currently diving into the world of React js and MUI components. I'm in the process of creating a stack with multiple boxes, each with its unique style that's consistent across all boxes in the stack. Is there a simpler way to define one ...

Tips for postponing an action until the webpage is fully loaded

Currently, I am using Selenium in R to perform a specific task. The script I have written enables the program to search for pizza restaurants near a designated geographical coordinate on Google Maps. The script continues to scroll until all restaurant inf ...

What is the best way to ensure that navigation takes up the full width of the screen in pixels when

Trying to use jQuery to set a specific pixel width for my Bootstrap navbar that spans the entire width of the browser window. However, encountering some bugs as the width needs to be recalculated whenever the browser is resized. Currently, I have this cod ...

Dropdown colorization

I want to create a dropdown menu in my black navbar, but the text color becomes blue and the background of the pop-up turns white, which clashes with my black background. I need it to be transparent while using Bootstrap v4.1.1 <nav className="navbar n ...

JavaScript functioning exclusively for specific list items within an unordered list

After implementing JavaScript on my website, I noticed that it only works when clicking on certain list items (li's) and not on others. Specifically, the functionalities for Specialties, Contact Us, and Referral Schemes are not working correctly. ...

Resetting a counter with a click event or deactivating it - Using JavaScript

I have a JavaScript function that creates a countdown until the download button can be clicked. I would like it to either disable again or start counting down once it's clicked, with preference for disabling. Below is the JavaScript code: <script ...

Insert data into a table without any formatting

I'm trying to enhance my table by adding inputs within the td tags, but I want them to appear as plain text cells. Below is the code snippet I have been working with: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js ...