Enlarging the width of one div to reduce the width of another

I am attempting to create an animation where a div's width expands while causing the div to the left of it to shrink and disappear.

Here is my progress so far: link

The challenge I'm facing is getting the width of the first (left) div to actually shrink. Additionally, I need the third div (on the right) to remain unaffected by the animation and maintain its size.

html

<div id='container'>
  <div id='one'>one</div>
  <div id='two'>two</div>
  <div id='three'>three</div>
</div>

css

#container {
  position: relative;
  border-style: dotted;
  height: 100px;
  width: 318px;
}

#one {
  border-style: dotted;
  border-color: red;
  left: 0;
  width: 200px;
  height: 100px;
  min-width: 0px;
  position: absolute;
}

#two {
  border-style: dotted;
  border-color: cadetblue;
  width: 0px;
  height: 100px;
  max-width: 200px;
  position: absolute;
  top: 0;
  right: 100px;
  animation: enter-right 20s linear infinite;
}
#three {
  border-style: dotted;
  border-color: goldenrod;
  width: 100px;
  right: 0;
  min-width: 100px;
  height: 100px;
  position: absolute;
}

@keyframes enter-right {
  0% {
    transform: translateX(0);
  }
  10%, 90% {
    transform: translateX(0);
  }
  98%, 100% {
    width: 100%;
  }
}

Answer №1

I implemented display: flex on the container and eliminated any positioning from the elements. I believe this achieves the desired result:

.App {
  font-family: Arial, sans-serif;
  text-align: center;
}

#container {
  display: flex;
  border-style: dashed;
  height: 120px;
  width: 320px;
}

#one {
  border-style: dashed;
  border-color: crimson;
  width: 190px;
  height: 120px;
  min-width: 10px;
}

#two {
  border-style: dashed;
  border-color: darkcyan;
  width: 10px;
  height: 120px;
  max-width: 190px;
  animation: slide-right 25s linear infinite;
}

#three {
  border-style: dashed;
  border-color: khaki;
  width: 90px;
  min-width: 90px;
  height: 120px;
}

@keyframes slide-right {
  0% {
    transform: translateX(0);
  }
  10%, 90% {
    transform: translateX(0);
  }
  98%, 100% {
    width: 100%;
    /* transform: translateX(100%); */
  }
}

Answer №2

To avoid using absolute positioning unnecessarily, consider utilizing an inline Flexbox container and applying an animation to the second element using the flex shorthand property.

main {
  display: inline-flex; }

main div {
  margin: 0 5px;
  box-sizing: border-box;
  overflow: hidden;
  border: 3px dashed #9bc;}

#one { 
  flex-basis: 200px; 
  width: 200px; }

#two { 
  flex: 1 0 0;
  animation: expand 5s linear 0s forwards; }

#three { 
  flex: 0 0 100px;
  width: 100px; }

@keyframes expand {
  100% { flex: 1 0 200px; }
}
<main>
  <div id="one">one</div>
  <div id="two">two</div>
  <div id="three">three</div>
</main>  

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

Choose the singular Element lacking both a class or an ID

I came across this and that question, however, they are both focused on Javascript. I am searching for a way to target the lone div that does not have an ID or Class assigned to it. For instance: <body> <div class="X" ... /> <div class="Z ...

Although React .map may seem like a function, it does not work for passing arrays in

My application root contains a lazyloaded component. App root : import Component from './app/components/component/component.lazy'; class App extends React.Component{ stuff: stuffType[] = [1, 2 , 3]; render() { return ( ...

Using jQuery to retrieve the value of a span element involves a few simple steps

Recently, I have started venturing into the world of jQuery. I have managed to create a form field with text input and included some text within a span element. <div class="input-group" style="width:40%"> <input type="text" name="emial" id= " ...

problem decoding json data from external server

I have a custom Grease Monkey script that is responsible for collecting data from a game and sending it to my server for tracking our team's progress. This process involves multiple Ajax requests between the GM script and the game, followed by sending ...

The Nodejs code snippet successfully allows adding items to the cart, however, a problem arises when adding multiple quantities of similar items individually

Cart functionality is working for initially adding new items, but then each item's quantity is added individually from other similar items? // Logic for handling cart addition // router.post("/", verifyToken, async (req, res) => { let o ...

Rails: How come my personalized stylesheet isn't taking priority over the font family?

I have included the Skeleton boilerplate css in my application from this source. It can be found within the directory app/assets/stylesheets. app/assets/stylesheets ├── application.css ├── custom.scss ├── normalize.css └── skeleton ...

Why is it that when I click outside of the <html> element, the click event bound to the <html> element is triggered?

const html = document.querySelector('html') const body = document.querySelector('body') body.onclick = () => { console.log('body clicked') } html.onclick = () => { console.log('html clicked') } document. ...

Breaking Down the Process of Exporting a Next.js Static Website into Manageable Parts

I am facing memory issues while building my website using Next.js's Static HTML Export. The site has a large number of static pages, approximately 10 million. Is it feasible to export these pages in batches, like exporting 100k pages in each build ite ...

Integrate ng-admin with ui-router for enhanced functionality

My AngularJS project (version 1.4.9) is built using ui-router and contains multiple states defined as follows: .state('overview', { url: '/overview', parent: 'dashboard', templateUrl: 'views/dashboard/overview.html ...

Synchronization-free API and callback functions

I am in need of utilizing an asynchronous service. My current approach involves sending data to this service using PHP and CURL, as well as receiving data from a URL provided by the service. How can I effectively respond or wait for feedback from this serv ...

Calendly is not defined in the window object of React

useEffect(() => { // Setting up Calendly in this effect }, []) console.log(window.Calendly) Printing window.Calendly gives undefined, however printing just the window object includes Calendly. ...

Is it possible to have a <small> element nested within an HTML5 heading such as h1, h2, h3, etc?

I am curious about the proper way to structure headings in HTML5. Can I include a <small> element within an <h3> tag? For example: <h3>Payment details <small>(this is your default card)</small></h3> ...

Using React Hook Form with the TipTap Editor: A Comprehensive Guide

I have a text input set up using the React Hook Form with the following code: <div className='mb-4'> <label htmlFor='desc'>Description</label> < ...

Is it possible to develop a function that can verify various input values and modify their appearance as needed?

I am currently working on creating a form and faced with the challenge of simplifying my code using a function. Specifically, I want to write JavaScript code that will check all input fields in the form to ensure they are filled. If any field is left emp ...

Scrolling to a specific anchor in a React webpage when the URL is opened in

Imagine having a component "Post" that contains multiple components "Comment". How can we make the application automatically scroll down to a specific comment when entering a URL like this: /post/:postId/#commentId The route for postId /post/:postId is a ...

Unlocking the power of vscode webview with acquireVsCodeApi in a React project

Currently, I am in the process of creating a new extension for Visual Studio Code using Webview. However, I am encountering difficulties with sending messages from the client to the extension. To kickstart the project, I decided to utilize this awesome rep ...

What strategies can I use to "link" the gap in React DOM?

Currently in the process of developing my initial React application using Next.js, I have encountered a particular challenge. In my page layout, there is a map component which is quite intricate and requires the insertion of React components into it. The c ...

Tips for creating a button that maintains consistency across different screen sizes

One of the images linked below shows a button displayed on two different sized screens, one 24 inches and the other 13 inches. The button on the 24-inch display appears normal, while the "Add to Cart" button on the 13-inch display is broken. Check out th ...

Title of Flickr API

Hey everyone, I'm working on setting up a gallery on my website using images and sets from Flickr. I've successfully loaded all the sets with the following code: $flickr = simplexml_load_file('http://api.flickr.com/services/rest/?method=fli ...

Encountered an issue while setting up ng-circle-progress in Angular: Unable to find an exported member in

I am currently working on incorporating the ng-circle-progress functionality by referring to the documentation at https://www.npmjs.com/package/ng-circle-progress. Here is a snippet from the .ts file: import { Component } from '@angular/core'; i ...