How to Set Transition Height in CSS

Alex Anie

Posted On: November 14, 2023

view count199169 Views

Read time17 Min Read

Web design revolves around the central focus of user experience. Websites that offer a positive user experience captivate visitors during their initial interaction, increasing the likelihood of these users returning to the site.

To achieve this, a lot has to go into designing, developing, best SEO practices, and, most importantly, testing, as this ensures your website runs seamlessly on multiple browsers, devices, and platforms.

Considering this, developing an engaging UI can significantly enhance the overall user experience. Interactive user interfaces leverage visual elements such as typography in CSS, images, and videos to grab users’ attention effectively.

Moreover, incorporating good animations and transitions for web elements can also capture users’ interest. For instance, applying transitions to web forms can indicate to users which form element is being clicked, facilitating an interactive and focused user experience during their tasks.

Transition height in CSS helps developers create engaging, smooth animation to element height using the transition property. This property gives the ability to control the looks and feel of element transition heights. A good transition on element height draws users attention, makes them feel a sense of engagement as they interact with elements on the web page and making websites perform better, drive more engagement and improve interactivity.

By the end of this blog, you should understand what are CSS transitions, how to set transition height in CSS, and other CSS properties that correspond to the height and vertical coordinates in element size.

We also look at best practices to follow when using transition height in CSS for your web project and how other CSS properties can be used on the vertical axis to improve web performance for optimal result.

Working With CSS Transitions

Setting transitions to CSS properties are very common, from changing background color from its initial state to a specific state to rotating web elements, changing the width, and other forms of web elements. Transitions help with smooth animations to CSS properties using the transition shorthand property.

Working With CSS Transitions

However, another form of setting transition height in CSS is applying the transition to the height of a specific element. This can help improve user interaction with web elements. Increasing the height of an element when a user clicks on or hover on a particular element can give a user a sense of interaction that the user is interacting with that specific element.

card displayed on a webpage

For example, if we have a card displayed on a webpage with links pointing to an external source, we can simply apply a transition height or scale to increase or make the card bigger when a user interacts with it. This will help give the user a good sense of interactivity.

How to Set Transition Height in CSS?

Setting transition height in CSS using the height property will increase the size of the specific web element on the vertical axis.

HTML:

CSS:

Preview:

From the browser preview, the web element is increased from height of 250px to 500px on transition. So when the element is hovered on, it transitions to 500px height. This makes it more interactive when a user hovers on it.

See the Pen
Transition-height[1]
by Ocxigin (@ocxigin)
on CodePen.

Specifying the height Property

By default, block elements are set to width auto, which takes the entire browser viewport when a fixed width value has not yet been set. Also, the element height will take the content size in the box. If the element has no content, it will not display on the browser even if a background-color is applied.

To make an element display on the web, sizing is essential. The height of the element has to be set, or the element should have text content that will determine the height of the element.

HTML:

CSS:

Preview:

Specifying the height Property

For example, element A has a defined height of 220px set on the element, while element B takes the element content as height. And as a result, element A has more space than element B, which helps with the readability of the content.

However, CSS transition height can take different forms; we shouldn’t explicitly set it to the page. We can apply the CSS transition height to an element so that it increases in size vertically when users interact with it, which leads to more interactivity.

See the Pen
height and content box
by Ocxigin (@ocxigin)
on CodePen.

Specifying the min-height Property

The min-height CSS property is used to set the minimum height of an element. The min-height takes a length property as a value, and this value determines the minimum height of the element.

When an element is set to a minimum height that is smaller than the content of the element, then the minimum height will not be applied, whereas if the minimum height is larger than the continent of the element, the minimum height is applied.

This means that your min-height value should be greater than the content of the element. If not, the min-height value will not affect the element.

HTML:

CSS:

Preview:

browser preview

From the browser preview, the two elements have the same content with a height of 180.656px. The first element is set to a min-height of 300px, which is larger than the content of the element, and as a result, the min-height property takes effect. On the contrary, the second element is set to a min-height of 100px, which is smaller than the content of the element. As a result, the min-height does not take effect.

The two elements grow on hover because the min-height is 400px, which is bigger than the content of the element.

See the Pen
min-height[1]
by Ocxigin (@ocxigin)
on CodePen.

Specifying the max-height Property

The max-height property sets the transition height in CSS where the element’s height is maximum. The max-height takes a length property as a value, and this value determines the maximum height of the element.

However, when an element is set to a maximum height and the content is larger than the maximum height, the content will overflow without displaying the rest of the content. Still, when the content is smaller than the maximum height value, the maximum height will not take effect.

HTML:

CSS:

Preview:

LambdaTest Enterprise

From the browser output, the content of the < div > tag with a class if the wrapper has a computed height of 207.969.

However, when the value of the maximum height is smaller than the content of the element, it overflows, but when the value of the maximum height is larger than the content, no effect is applied.

The element grows when we hover over it. This is because the element has been set to a max-height of 100px, which overflows the element. However, overflow auto is also set so the content overflows with the element.

Lastly, the setting transition in CSS using the max-height property moves the element from 100px to 500px in a smooth, easy manner in 0.5 seconds.

See the Pen
max-height[1]
by Ocxigin (@ocxigin)
on CodePen.

Specifying the flex-grow Property

The flex-grow sub-property of the flexbox model can be used to set transition height in CSS. The flex-grow property takes a value that determines the amount of space that should be distributed among each flex-items minus the size of the flex container. The flex-grow property expands based on the direction of the flex-direction property.

HTML:

CSS:

From the code example above, each flex-items takes a flex-grow property value of 1, as a result each flex-item has equal space distributed among them and stretched to the viewport width.

However, on hover, the flex-grow, which has a value of 2, takes the largest space, and the remaining flex-item takes the remaining space in an equal distribution.

Desktop Preview:

Desktop Preview

From the desktop preview above, the flex-grow moves to the row direction. That means it grows horizontally. To see how the effect grows, hover on individual image items.

Mobile Preview:

Mobile Preview

From the mobile preview above, the flex-grow moves to the column direction. This means it grows vertically. To see how the effect grows, resize your browser and hover on individual image items.

Using the flex-grow property effect can help bring a particular element to focus when users interact with it.

Specifying the translateY() Property

The translateY() is a sub-property of the CSS transform property. This can be used to set transition height in CSS. The translateY() is closely related to other transform properties such as translate(), translateX(), rotate(), skew(), scale(), and matrix(), which are used for positioning web elements around the web page.

The translateY() is a transitional property that can be useful when positioning elements with defined height.

In this blog section on setting transition height in CSS, we’ll only look at the translateY() sub-property used to move elements around the Y (vertical) coordinates.

With the help of pseudo-elements, we can use the translateY() to add effects to elements with a defined height. Depending on what you are working on, how you choose to implement translateY() may vary.

HTML:

CSS:

Preview:

Specifying the translateY() Property

From the browser preview, translateY() is used to nudge the image down a bit with a transition effect.

See the Pen
translate-card
by Ocxigin (@ocxigin)
on CodePen.

Let’s look at another example.

HTML:

CSS:

Preview:

real time browser viewer

From the browser preview, a transition all is added to the button to apply the transition to all the CSS properties assigned to the button.

The button background color created by the pseudo-element is offset by -40px by the translateY() property, and it is returned in place on hover by the translateY(0). Move over to the button to see the effect.

See the Pen
translate-buttion
by Ocxigin (@ocxigin)
on CodePen.

Info Note

Test your web applications on a real device cloud. Try LambdaTest Today!

Best Practices for Setting Transition Height in CSS

When working with transition heights in CSS, it’s important to know how they can be implemented properly to get the element to display properly on the web page.

Here are some ways you can consider when applying transition heights in CSS.

Keeping the CSS Transition Height Consistent

Consistency is crucial when styling web elements, especially when styling elements with the same class or are meant to do the same thing. For example, CTAs buttons (Call To Action), cards, etc, can have a consistent transitional height set on them. This will help improve as follows:

  • Keep transition height in CSS consistent across the website.
  • Contents are well organized.
  • Website becomes easy to navigate.
  • It improves readability.

HTML:

CSS:

Preview:

preview

preview gif

From the browser preview, we created four buttons with four different effects applied to each button.

The window slide effect is a box-shadow effect with inset value, which brings the shadow from the inside. The effect is split into two, coming from the left and right sides of the button. Note that the window slide effect value is repeated and separated with a comma, and to make the other value come from the opposite side, a negative (-8rem) is added to the last value.

The popup effect is a box-shadow effect that is applied on hover. The slide in effect, on the other hand, is also a box-shadow effect; an inset value is applied to bring the box-shadow from the inside while the burst effect, which is the last effect on the list, takes 1.1rem of blur radius (to reduce the intensity) and 1rem of spread radius (to increase the size of the box-shadow).

However, one thing the CTA buttons have in common is the translateY(-0.5rem) that is applied on hover. This demonstrates how consistency can be applied when creating a CTA button. These buttons are meant to draw attention when a user hovers over them.

Also, browser and device compatibility should be taken care of while trying to maintain consistency with design. Here is a cross-platform preview of how we can use CSS media queries to create different breakpoints for a website.

Desktop Preview:

Mobile Preview:

mobile preview

Tablet Preview:

tablet preview

Laptop Preview:

Laptop Preview

From the preview above, the website is set to 100vw (viewport width) of the browser width on the laptop device, which is the largest. The buttons on a large screen are set on a single column. To avoid the buttons colliding with each other when loaded on a smaller screen, we set the buttons to a two-column grid on a medium screen (tablet), while on an even smaller screen (mobile), we set the buttons to a single-column grid.

Cross-device compatibility and responsiveness in websites or web apps improve user experience, as seen in the image above. Also, since cross-device compatibility and responsiveness are essential for any website or web app that wants to reach a wide audience, performing responsive testing is crucial.

Built by LambdaTest, LT Browser is a dev-friendly browser that can help ensure your website or web app is cross-device compatible and responsive. It provides access to a cloud of over 50+ device viewports to test your website or web app on different screen resolutions for mobile, tablets, laptops, and desktops.

Also, with LambdaTest, an AI-powered test orchestration and execution cloud platform, you can test your websites and mobile apps on an online browser farm of 3000+ real browsers, devices, and platforms. It also provides a mobile device testing lab of real and virtual devices.

Catch up with the latest tutorials on mobile app testing, real device testing, and more — Subscribe to the LambdaTest YouTube Channel.

See the Pen
CTAs-buttons
by Ocxigin (@ocxigin)
on CodePen.

Setting the Height to Match the Content

There are cases where the specified height will overflow the text content and result in extra white space below the content of a box. This usually happens with block elements.

It is always best for the content to fit in with the height of the element to prevent extra whitespace, which can lead to unwanted vertical scrolling of the overdrawn page.

The fit-content value of CSS helps prevent this extra whitespace by fitting the content with the height of the box, which gives a better user experience.

HTML:

CSS:

JavaScript:

Preview:

Preview css

Here, from the browser preview, we have three cards displayed. Each card has a toggle button that displays the event information on click and closes the same information when we click it again.

The cards above have no extra whitespaces because we use fit-content CSS value on the height of the card. If the fit-content value is omitted, the cards and other card siblings will have extra white space because they are grid items.

The images below illustrate this point.

white space

However, the fit-content CSS value can be used to achieve a situation where all text content on the card is well-fitted.

See the Pen
drop-dow-card
by Ocxigin (@ocxigin)
on CodePen.

Avoiding Changes in Abrupt Heights

By default, block elements take the full width of the browser viewport and the height of the content in the box. If no content or height is set, the element does not display. And setting a transition height in CSS with a fixed height might cause the content to overflow if the content is greater than the specified height. This is where the fit-content CSS value can be helpful, as explained above.

However, if you are working with flex or grid items, things can get completed if spaces are not well distributed among flex items.

To fix this, you can specify different values to either flex or grid items to distribute spaces among flex items evenly. For example, we applied different CSS breakpoints using media queries to realign elements along different viewports.

Mobile Viewport:

Mobile Viewport

Tablet Viewport:

Tablet Viewport

Laptop Viewport:

Laptop Viewport

From the above preview, mobile view is set on a single column, tablet is set to a two-column grid, while laptop and other larger screen sizes are set to a three-column grid. Notice how spaces are evenly distributed between the grid items, that is, top to bottom and left to right.

This will help make the website responsive on different device screen resolutions.

See the Pen
drop-dow-card
by Ocxigin (@ocxigin)
on CodePen.

Transition height in CSS, as we’ve seen, can be handy to make elements move smoothly from one point to the other. However, ensuring text content fits the element’s height well can improve user experience.

Conclusion

We’ve just completed an article detailing the process of setting transition height in CSS. The topics covered include transitions, setting transitions for height, utilizing the min-height and max-height properties, employing the flex-grow property, incorporating the translateY() value, maintaining consistent transition height, aligning height with content, and preventing sudden height changes.

Frequently Asked Questions (FAQs)

How do you change the transition height in CSS?

To modify the transition height in CSS, follow these steps:

  1. Adjust the target height property in your CSS code.
  2. Update the transition duration to control the speed of the height change.
  3. Optionally, apply a different easing function for a specific transition effect.
Author Profile Author Profile Author Profile

Author’s Profile

Alex Anie

A Front End Technical Writer. I write blogs on Front End Technologies, such as HTML, CSS, JavaScript. When I am not coding, I enjoy watching tutorials and listening to twitter spaces. I live in Lagos, Nigeria

Blogs: 16



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free