What causes the sudden jump in width during a transition?

I'm having trouble with setting transitions. Everything seems to be in order and almost working, but I'm experiencing a "jumping" effect: instead of smoothly transitioning the width from 100% to 100px, it jumps from 100% to "min-content" to 100px.

Here is my JSFiddle for reference: https://jsfiddle.net/oth8k35q/27/

The main parts of my code are as follows:

<div style="display:inline-flex; width: 400px; border: 1px dotted black;">
  <div id="c1" class="active">
    <div> 1 </div> <div id="d1"> 1 </div>
  </div>
  <div id ="c2">
    <div> 1 </div> <div id="d2"> 1 </div>
  </div>
</div>
#d1, #d2 {
  width: 100px;
}

.active, .active #d1, .active #d2 {
  width: 100%;
}

#d1, #d2 {
  transition: width 1s;
}

Answer №1

It appears that the sudden movement you are experiencing is due to the behavior of the flex-grow property in your flex-box setup. One solution could be to animate the flex-grow and flex-shrink properties to create a smoother transition. Please note that this solution has been tested in Chrome only.

To adjust the pseudo min-width, you can modify the width of the container class, for example:

.container{
  display: inline-flex;
  width: 125px;
  flex-shrink: 100;
  flex-grow: 1;
  transition: flex-grow 1s cubic-bezier(0.4, 0, 0.2, 1), flex-shrink 1s cubic-bezier(0.4, 0, 0.2, 1);
}

function toggleClasses(){
    document.getElementById('container-1').classList.toggle('active');
    document.getElementById('container-2').classList.toggle('active');
}

document.getElementById('container-1').addEventListener('click', () => {
    if(document.getElementById('container-1').classList.contains('active')) return;
    toggleClasses();
});

document.getElementById('container-2').addEventListener('click', () => {
  if(document.getElementById('container-2').classList.contains('active')) return;
    toggleClasses();
});
.wrapper{
  display:inline-flex; 
  width: 400px; 
  border: 1px dotted black;
  box-sizing: border-box;
  overflow: auto;
}

.wrapper .container:nth-child(1){
  background: lightblue;
}
.wrapper .container:nth-child(2){
  background: blue;
}

.wrapper .container:nth-child(1) div:last-child{
  background: rgba(255,0,0,0.33);
  cursor: pointer;
}
.wrapper .container:nth-child(2) div:last-child{
  background: rgba(0,255,0,0.33);
   cursor: pointer;
}

.container{
  display: inline-flex;
  width: 100%;
  flex-shrink: 100;
  flex-grow: 1;
  transition: flex-grow 1s cubic-bezier(0.4, 0, 0.2, 1), flex-shrink 1s cubic-bezier(0.4, 0, 0.2, 1);
}

.container.active{
  flex-grow: 100;
  flex-shrink: 1;
}

.element{
  width: 100%;
}
<div class="wrapper">
    <div id="container-1" class="container active">
        <div>1</div>
        <div class="element">Content#1</div>
    </div>
    <div id="container-2" class="container">
        <div>1</div>
        <div class="element">Content#2</div>
    </div>
</div>

Answer №2

I recently incorporated the transition: width 1s; property within the styling of #c1.active #d1, #c2.active #d2.

var change = function(selector){
  $(".active").removeClass('active');
   $(selector).addClass('active');
}
$('#d1').click(function(){
  change('#c1');
});

$('#d2').click(function(){
  change('#c2')
});
#d1 {
  background-color: green;
  width: 100px;
}
#d2 {
  background-color: red;
  width: 100px;
}
.active{
  width: 100%;
}
#c1, #c2 {
  display: inline-flex;
}
#c1.active #d1, #c2.active #d2 {
  width: 100%;
  transition: width 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="display: flex; width: 400px; border: 1px dotted black;">
  <div id="c1" class="active">
    <div>1</div>
    <div id="d1">d1</div>
  </div>
  <div id="c2">
    <div>1</div>
    <div id="d2">d2</div>
  </div>
</div>

Answer №3

Hello there! Give this a shot, using a 100% width may lead to some problems with the transition effect.

    .active, .active #d1, .active #d2 {
      width: calc(400px - 100px);
    }

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 preventing the question div and buttons from expanding according to their content?

After experimenting with setting the height of the question div to auto, I found that it would stretch to fit its content. However, I am looking for a solution without overflow: auto or scroll. Is there a limit to how responsive it can be once a certain ...

CSS error with contrasting colors, the reason behind it is unknown

Here is the code snippet I am working on: I have set the background color to #f1f5ff and the font color to #181818. Despite thinking that the contrast is good, I am still encountering an error where the text is not visible. I have tried changing 'c ...

What could be causing my fixed header to disappear in Safari 5?

I am experiencing a strange issue with my fixed header on Safari 5 for iPad. The header disappears once it scrolls down to a certain point, even though it works fine in IE, Firefox, Chrome, and the latest version of Safari. Has anyone else encountered this ...

What is the method for displaying text and icons as black in a sticky header design?

Having trouble making the menu text and icons in the sticky header black on my website. I've been searching for the correct class in the CSS styles, but haven't been able to find it. I tried using this CSS: .header-wrapper .stuck { color: #000 ...

The cube mesh is failing to render inside the designated div

I created a Torus and Cube Mesh in my scene, but I want the cube to be positioned at the bottom right corner of the screen. Here's what I tried: <div style="position: absolute; bottom: 0px; right:0px; width: 100px; text-align: center; "> & ...

What is the best way to insert a scroll into a section of a circular shape seamlessly?

Currently, my layout looks like this: https://ibb.co/iqaOXS However, I would like it to look more like this: https://ibb.co/eDkCRn The issue I'm facing is that the buttons are extending beyond the circular border. How can I prevent this from happeni ...

Applying CSS Filters can sometimes interfere with the functionality of 3D

While playing around with CSS transforms, I discovered that filters flatten the transforms just like setting transform-style: flat. var toggleFilter = function() { var div = document.getElementById("cube") if (div.className == "cube") { d ...

Is it best to stick with a static page size, or

While I typically design my webpages dynamically by determining the screen size and creating divs accordingly, I'm curious about the advantages of using a 'static' sizing approach (such as using pixels). Any insights on this contrasting meth ...

Utilizing the CSS Flex property to position one element in the center and align the other element to either the left or right side

What is the best approach to center one element inside a container with CSS flex properties and align another element to the left or right? Even when I set ChatGPT's recommendations of margin-left and margin-right properties to auto for the right-ali ...

Should we consider using extra url query parameters as a legitimate method to avoid caching or enforce the updating of css/js files?

Is it acceptable to include extra URL query parameters in order to avoid caching or enforce the updating of CSS/JS files? /style.css?v=1 Or would it be preferable to rename the file/directory instead? /style.1.css I've heard that this could potent ...

Using CSS to Create a Gradient Background with an Image

Is it possible to add a URL background image to a gradient and position it in a specific way? Since the gradient is considered an image on its own. CSS background: linear-gradient(to bottom, #98cb01 2%,#60a822 100%)!important; ...

tips for concealing the sorting icon in the DataTables header upon initial load

Is there a way to initially hide the datatable sorting icon upon loading, but have it display automatically when clicking on the header? I've been searching for a solution without success. https://i.sstatic.net/o2Yqa.png ...

Styling with react-jss based on intricate conditionals

After experimenting with react-jss for a few weeks, I am faced with the challenge of styling components efficiently. With numerous conditionals in these components, I am striving to avoid creating an excess of new components that essentially share the same ...

JavaScript function that adjusts the top position when toggling

When the user clicks on a div, it expands or closes. I also want to adjust the top position of another fixed div based on the state of the original div. For instance: If the original div is expanded, I want the second div to have top:10px If the original ...

Lines running horizontally on either side of the text

I recently found a helpful solution on Stack Overflow, but it's not exactly what I'm looking for due to two main reasons: I am unsure how to adjust the width of the lines to make them shorter. The solution uses H2, however, I have already d ...

The height of each list item should expand based on the size of the div contained within it

The HTML markup I am working with looks like this: <ul data-role="listview" data-inset="true" class="stuff site-content lists"> <li> <div class="nearby"> 20 </div> <h1> name</h1> </li> ...

Tips for modifying multiple divs with the same class using querySelector

I am curious about how to change the class on multiple divs at once using JavaScript. In the example below, only the first use of the class is recognized, while all subsequent ones are ignored. querySelectorAll does not seem to work. Thank you in advan ...

Troubleshooting problems with the width of a UI Bootstrap accordion container

Check out this Plunker for help! Having some trouble with ng-Grid inside a ui.bootstrap accordion. Visit my Plunker to see the issue firsthand. Essentially, when I place a grid inside an accordion with the child accordion-group closed upon page load, the ...

Paged Content: Turning a Lengthy Text into a Book-Like Format

I have a unique idea for displaying text like a flipping book on a web page. Users can use arrow keys to navigate through the content, giving it an interactive and engaging feel. Instead of focusing solely on page transitions, I am looking into implementi ...

Like button for Facebook located at the bottom of a static webpage

I am facing an issue with my web application where it does not scroll properly, especially when there is a Facebook button at the bottom. The behavior in Chrome and Firefox is inconsistent and causing some trouble. In Chrome, the div repositions correctly ...