What is the best way to incorporate a transition effect into a flex item with a specified max-width, where the width adjustments are managed by a child div?

The example below demonstrates a situation where the child's width grows with a smooth transition, but the parent's width abruptly changes!

What I aim for is to have both the child's width and the parent's width transition smoothly.

It's important to note that the parent has a max-width specified, but its width is determined by the content, which is the child.

I attempted to add a transition ease property to the parent div, hoping that the width change caused by the child would also transition smoothly.

Feel free to visit https://codepen.io/abstrekt/pen/qBKdPZe for reference.

<div class="parent">
    <div class="child">this is the child</div>
</div>

<style>
    .parent {
        padding: 8px;
        display: inline-flex;
        border: 2px solid red;
        max-width: 300px;
        transition: width 1s ease;
    }

    .child {
        white-space: nowrap;
        overflow: hidden;
        border: 2px solid green;
        transition: width 1s ease;
        width: 20px;
    }

    .parent:hover>.child {
        width: 100%
    }
</style>

I have been searching for a solution to my specific case but have yet to find one.

Answer №1

Dealing with transitions to auto widths in the parent element has been a persistent challenge as highlighted in this discussion.

An alternative approach would be to implement the transition on the max-width property instead. In this case, you would need to estimate the content size and set a value larger than the content width for the transition to work effectively. However, excessively large values may quicken the animation.

.parent:hover>.child {
  max-width: 150px;
}

.child {
  white-space: nowrap;
  overflow: hidden;
  border: 2px solid green;
  transition: max-width 1s ease;
  max-width: 50px;
}

.parent:hover>.child {
  max-width: 150px;
}

.parent.to-small:hover>.child {
  max-width: 70px;
}

.parent.to-large:hover>.child {
  max-width: 100vw;
}

.parent {
  padding: 8px;
  display: inline-flex;
  border: 2px solid red;
  max-width: 300px;
}

.child {
  white-space: nowrap;
  overflow: hidden;
  border: 2px solid green;
  transition: max-width 1s ease;
  max-width: 50px;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
}
<div class="parent">
  <div class="child">this is the child</div>
</div>

<div class="parent to-small">
  <div class="child">to small: this is cut off</div>
</div>

<div class="parent to-large">
  <div class="child">to large: this is too fast</div>
</div>

Answer №2

.container {
    margin: 12px;
    display: flex;
    border: 3px solid blue;
    width: 70px;
    max-width:250px;
    transition: width 2s ease;
}

.container:hover{
width:90%
}

**Simply include the initial width attribute to the parent container div** codepen - https://codepen.io/narut-o/pen/VwdLMEK

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

Jumping transitions in thumbnail images

Could you please visit I've noticed that when hovering over the thumbnails in the last column, the images jump a bit after transition, especially in Firefox. Do you have any suggestions on how to resolve this issue? ...

How can I access a store getter in Vue after a dispatch action has finished?

I am currently utilizing Laravel 5.7 in combination with Vue2 and Vuex. While working on my project, I have encountered an issue where Vue is not returning a store value after the dispatch call completes. The workflow of my application is as follows: Wh ...

How to Spin an Image in the Canvas Using HTML5

I am having trouble rotating an image inside a canvas after researching similar questions on Stack Overflow. Here's what I've learned: It seems I cannot rotate a single object inside the canvas. I can only rotate the entire canvas itself. ...

Creating a dynamic nested form in AngularJS by binding data to a model

Creating a nested form from a JSON object called formObject, I bind the values within the object itself. By recursively parsing the values, I extract the actual data, referred to as dataObject, upon submission. To view the dataObject in a linear format, r ...

Responsive issue identified with Bootstrap navbar hamburger menu upon clicking

My website has an issue on mobile where the hamburger menu appears but doesn't respond when clicked. I'm unsure what's causing this problem in my code. <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a ...

When a directive is passed a string with HTML tags, the tags are not displayed/rendered as expected

When it comes to passing a string with HTML content to my Angular directive, I've encountered an issue where the rendered output treats the HTML as text. While researching possible solutions, most of them seem to rely on using ng-bind-html directive, ...

What could be the reason for a particular product edit page showing up completely blank?

In my ongoing project, I am developing an admin panel that allows administrators to add new products to their website. These products are then stored in a Firestore database and managed using Redux Toolkit. The added products can be viewed and edited in th ...

The HTML dropdown menu is malfunctioning

I've been struggling to create a website with a dropdown menu. Despite following various guides and searching for solutions, the menu is behaving strangely. I've tried numerous tactics, so some lines of code may be unnecessary. The submenu is ap ...

There seems to be an issue with the package not running at xxx:yy in React

As I work on developing a standalone Android app using React Native, the installation of react-native-fcm has led to a persistent error message appearing: The Gradle file in my project appears as follows: // Top-level build file where you can add configu ...

What could be causing the error "insertOne is not a valid function" in my MongoDB database?

When the code attempts to connect to the mongodb database: const dbConnect = async () => { try { await client.connect(); const db = client.db('smile-tracker'); const users = db.collection('users'); retu ...

Tips for creating a conditional confirm dialog box in Asp.net using JQuery/Javascript

I have some jQuery and traditional JavaScript mixed together to validate a textbox on my ASP sample page. There is a button, a textbox, and a button_click event in the code behind. When the button is clicked, I want to show an alert if the textbox is empty ...

reveal.js: Upon a fresh installation, an error has been encountered: ReferenceError - 'navigator'

Currently, I am attempting to integrate reveal.js into my React.js/Next.js website using the instructions provided at However, upon implementation, I encountered the following error: Server Error ReferenceError: navigator is not defined This error occurr ...

The correct usage of && and || operators in javascript

I've developed a small web application and I'm facing an issue where I want to trigger an alert when the 'enter' key is pressed if there is an empty or space value. Currently, the alert appears not only on hitting 'enter' but ...

What is the best way to centralize the storage of my website's header and footer?

Constantly updating headers and footers requires me to manually transfer the code to each web page every time, which is not the most efficient method. I'm considering using a "header" and "footer" div, and using jQuery's $(document).ready functio ...

What is the best way to ensure that my text and clickable URLs can be effectively sent through a Teams chat webhook without any truncation issues?

My ultimate aim is to achieve in Teams what can easily be accomplished with Slack Webhooks, like this: await fetch(slackWebhook, { method: 'POST', headers: { 'Content-Type': 'application/json' ...

Access your login page with a nodejs mongoDB connection

After successfully sending username, password, and email through postman, the information gets added to the MongoDB schema. However, on the login page in React, it doesn't seem to work correctly. Any assistance would be greatly appreciated. Thank you. ...

Difficulty executing for loop due to multiple buttons, resulting in malfunctioning buttons in JavaScript code

Currently encountering an issue while trying to implement modal windows on my first website. Everything works fine without the for loop, but I wanted to have multiple buttons and windows, so I decided to use a for loop to handle each button. Strangely, the ...

Manipulate the presence of THREE.Points in a three.r84 scene by adding or removing them

I recently upgraded from three.js version 71 to version 84 and encountered a problem with updating points in the scene. Previously, with THREE.PointCloud, it was simple to add and remove points as needed like so: function updatePoints(newData) { geom ...

How can we bring in prop array values in React?

I've been working on developing a small music player program in React. Is there a way to import all these values together with a single import statement? I noticed that manually inputting the values into the props array doesn't work because the ...

How to retrieve a refined selection of items

In my scenario, there are two important objects - dropdownOptions and totalItem https://i.sstatic.net/VreXd.png The requirement is as follows: If 12 < totalItems < 24, then display "Show 12, Show 24" If 24 < totalItems < 36, only show "Show ...