How to Linearly Transition the removal of a DOM element using CSS and JavaScript ? Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report For a particular HTML/DOM element, our job is to remove it and add a transition to the process of removal. Eventually, as soon as the trigger button is clicked, the DOM element should translate towards the right and finally be removed from sight as well as from the DOM.Transitioning elements using CSS often confuses developers. There can be many variations for this property, practically any CSS property can be transitioned, be it opacity or position. In this tutorial, we will be transitioning the horizontal position of a text element, which will be finally be removed.From now the DOM element to be removed will be called the target.Approach:Three “h2” tags and a button are wrapped inside a container section. The second tag is unique with an id “section”.The target is having an id (“second”) to transition the motion. This is done by using the transition attribute in CSS.Add a click event listener to the button, which adds a class(“removed”) to the target, as a result of which the target should start translating.In CSS, this transition for the element with id (“second”) is set to transform 1s 0s.Now, set the “transform” attribute of the heading to be removed, equal to translateX(len), here len refers to the length through which the element has to be translated. We set it to “100vw”.Lastly, an event listener is added to the target which listens for the Transition-End. This removes the target with the help of JavaScript's remove() method.Example: This example shows the use of the above-described approach. HTML <style> .removed{ transform: translateX(100vw); } #second{ transition: transform 1s 0s; } .cntnr{ overflow-x: hidden; } </style> <section class="cntnr"> <h2>Geeksforgeeks 1</h2> <h2 id="second">Geeksforgeeks 2</h2> <h2>Geeksforgeeks 3</h2> <button>Remove Second</button> </section> <!-- Functionality with JavaScript --> <script> var btn = document.querySelector("button"); var secondText = document.querySelector("#second"); btn.addEventListener("click",() => { secondText.classList.add("removed"); }); secondText.addEventListener("transitionend",() => { secondText.remove(); }) </script> Output: Comment More infoAdvertise with us Next Article How to Linearly Transition the removal of a DOM element using CSS and JavaScript ? sarvjot Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to fade the removal of a DOM Element using CSS and JavaScript ? In this article, we will fade the removal of a DOM Element using CSS and JavaScript.CSS has a vast domain, while basic CSS is essentially copy-pasted, it can become highly complicated when merged with front-end technologies like JavaScript and related frameworks. Therefore it is noticed that some hi 2 min read How to remove a Class name from an Element using JavaScript ? Removing a class name from an HTML element using JavaScript is often used to dynamically modify the styling or behavior of an element based on user interactions, events, or other conditions. The operation can be achieved using classList property, that allows you to add, remove, or toggle classes. Sy 3 min read How to Select and Manipulate CSS pseudo-elements using JavaScript / jQuery ? In this article, we will learn how to select and manipulate CSS pseudo-elements using JavaScript (or jQuery). CSS pseudo-elements allow developers to style and enhance specific parts of an HTML document with the help of selectors like::before and::after, which provide the flexibility to add decorati 2 min read How to remove CSS style of <style> tag using JavaScript/jQuery ? Given an HTML document containing inline and internal CSS and the task is to remove the style of <style> tag. The internal or embedded CSS is used within the head section of the HTML document. It is enclosed within <style> tag. Approach: The jQuery remove() and empty() methods are used t 2 min read How to create multiple transitions on an element using CSS ? In this article, we are going to learn how we can have multiple transitions on an element. Generally, we apply or create more than one transition to create some effects in our design. In CSS there are certain properties that can be transitioned.Approach: To have multiple transitions on an element, b 3 min read Remove all the child elements of a DOM node in JavaScript In HTML, child elements are nested within parent elements, and there are times when you may need to remove these child elements from the DOM. This can be done using JavaScript DOM methods. In this article, we'll explore two common methods for removing child elements from a DOM node.Why Remove Child 3 min read How to add and remove CSS classes to an element using jQuery ? In this article, we will see how to add or remove the CSS classes to an element using jQuery. To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method. Syntax: The syntax for adding CSS classes to an element: $('selector').addClass(clas 2 min read How to remove an HTML element using JavaScript ? Removing an HTML element using JavaScript involves deleting it from the DOM, typically using methods like element.remove() or parentNode.removeChild(). This approach updates the webpage dynamically, allowing for responsive interactions by removing unwanted or outdated elements based on user actions 3 min read How to remove specific div element by using JavaScript? Removing a specific `<div>` element with JavaScript is crucial for dynamic web content management. Whether updating information, responding to user actions, or optimizing performance by cleaning the DOM, efficient DOM manipulation is essential. JavaScript offers diverse methods such as `remove 3 min read How to Update Properties of Pseudo Element :after and :before using Javascript ? In this article, we will learn how to select and update the CSS properties of pseudo-elements ::after and ::before using JavaScript. The HTML elements like <div>, <h1>, <p>, etc. can be easily selected using document.getElementById() and other related JavaScript methods, selecting 4 min read Like