Animate a CSS transition to smoothly slide a hidden div into view from the edge to the center of the

I am looking to create an animated floating div. When the div is in the 'closed' state, most of it is hidden on the right side of the screen, with only 20px still visible.

Upon clicking the visible part, I want the div to move to the center of the screen and reveal itself (referred to as the 'open' state).

My challenges include:

  • I typically use margin:auto to center a div, but it behaves strangely when I animate it.
  • When the div is 'closed', the 'hidden' part causes overflow, resulting in unwanted scrolling.
  • The width of the div varies significantly depending on the case, making it difficult to hard code values in CSS.

Does anyone have any ideas on how to achieve this? Even a partial solution would be greatly appreciated.

Update: Here is the solution (thanks to @sonic)

.open{
  translateX(-50%);
  left:50%;
}

.close {
  translateX(-20px);
  left:100%;
}

Answer №1

This question is quite broad, making it difficult to provide assistance with just 2 or 3 points. It's challenging to determine the objective without any code provided.

To center a div like this:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

You can easily animate these properties.

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

Does Blazor offer a GetElementById method for C#? Alternatively, can you help me understand why my Checkbox remains checked?

I need to modify the "checked" attribute of a checkbox input element based on a model's property. The challenge arises when this "checked" attribute is part of a random list of li-elements generated by a list of models representing todo elements. This ...

PHP must be loaded before Javascript code will be executed

I am in the process of creating a high-tech "smart home" webpage, but I am facing an issue with the code execution order. Despite trying to use sleep() and other tactics, I can't seem to display the success message before running the script that reboo ...

Can @keyframes be iterated through CSS (particularly Stylus)?

Here is some Stylus code I'm working with: for i in (1..3) .l:nth-child({i}) opacity (i / 5) When executed, the output looks like this: .l:nth-child(1) { opacity: 0.2; } .l:nth-child(2) { opacity: 0.4; } .l:nth-child(3) { opacity: 0.6; } ...

Ways to retrieve the text of the chosen radio button label with the help of jQuery

There is a radio button on my webpage that looks like this: <div id="someId"> <label class="radio-inline"> <input name="x" type="radio" onchange="GetSelectedVal();">Yes</label> <label class="radio-inline"> ...

Is your 100% height not beginning at the top of the page?

The link provided showcases a tutorial that has a white column extending to the bottom of the browser. In this example, the white column begins from the very top, with a header in a different shade of grey covering it. In my scenario, I need the header to ...

Displaying Bootstrap tooltip on a Font Awesome icon within a modal box

I have a unique scenario where I have an input field with a Fontawesome icon added through the content property in my CSS file. This icon is supposed to display only on validation. Now, I'm trying to enhance this by adding a Bootstrap tooltip to the i ...

What steps must be taken to remove the icon located in the upper left corner?

https://i.sstatic.net/vQprs.png I am struggling to align the remove icon to the top. Here is what I have attempted: .skills ul { width: 100%; margin: 10px 0px 0px 0px; padding: 0px; float: left; } .skills li { border: 1px solid #a7dbf5; li ...

Is there a way to use JavaScript to retrieve a list of files that were loaded by the browser during the last request

I'm just starting out in the world of JavaScript and I have a query: is there a method to retrieve a list of files that were loaded by the browser during the last request? To clarify, when a browser loads a webpage, it not only downloads the HTML fil ...

Is there a way to align all the values in the center of the row, while ensuring that the div above the row does not have the same width as the row below it? If so, how

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src= ...

Concealed text hidden beneath a Sticky Navigation Bar (HTML/CSS)

I'm in the process of creating a simple website that includes a sticky Navigation Bar. However, I'm encountering an issue where text added to the website is being hidden behind the Navbar. Is there a way for me to adjust the positioning of the te ...

I am experiencing an issue with Safari where there is an unexpected rounded border appearing that

Upon inspecting the image, a slight border is visible behind the main border on Chrome and Firefox. However, Safari and iPhone display a rounded thin border instead. Can you help us understand why this discrepancy is happening and how we can remove it? P ...

CSS: Avoiding text overflow in a div container

Utilizing Bootstrap 4, I have a div containing lengthy text that causes overflow within the element. The issue is depicted in this image example. By introducing spaces to shorten words, the text displays correctly without overflowing. However, extended ...

How come the link function of my directive isn't being triggered?

I'm encountering a strange issue with this directive: hpDsat.directive('ngElementReady', [function() { return { restrict: "A", link: function($scope, $element, $attributes) { // put watche ...

Matching start and end of CSS selectors using regex

If I have elements with a title attribute like the examples below, how can I select the div with the title custom-marker-wqs-tailrace bbu-l2 using a CSS regex selector? <div title="custom-marker-awlr-tailrace bbu-l2"></div> <div ti ...

Exploring the theme color options in Angular Material

Despite extensive searching on SO and GitHub, I am seeking clarification: Is it impossible to access a theme color (or any other theme feature) in order to set a mat-card color? There is no way to retrieve a variable in scss. You cannot assign a class to ...

Tips for stopping a click from going through a fixed element to the one in the background

My website features a fixed positioned header with a search box, overlaying content below it (thanks to the higher z-index). When I click on the search box, an event handler is triggered. However, the click response also passes through the header to the ...

fuzzy edges on an HTML5 canvas

When attempting to draw vertical lines on a canvas with a 1px width, some lines may appear wider or blurry. Various solutions, such as subtracting 0.5px or translating coordinates, have been suggested but do not seem to work in this case. Attempts have be ...

When I click on .toggle-menu, I want the data-target to have a toggled class and the other divs to expand

Looking to achieve a functionality where clicking on .toggle-menu will toggle a class on the specified data-target element and expand other divs accordingly. jQuery(document).ready(function() { var windowWidth = jQuery(window).width(); if (win ...

Align bootstrap button to the center on xs screens

I've spent countless hours on this issue - I just can't seem to get the button centered properly in xs mode. It keeps aligning itself based on the left side, no matter what I try. I've searched through Bootstrap but couldn't find a clea ...

Are there any aesthetically pleasing CSS themes available for GWT integration?

This inquiry has been raised in the past... GWT 2.0 Themes? GWT Themes and Component Libraries ...however, two years have elapsed. Previously, the response was mainly negative unless utilizing a widget library. I am on the lookout for an appealing CSS ...