What is the best way to create a vertical sliding animation on a webpage using CSS?

I've been experimenting for hours trying to achieve a sliding animation from the bottom to the top using just CSS. The goal is to create a test tube with fluid filling it. (Here is the desired outcome), but during the animation, the fluid overflows at the bottom edges and doesn't stay flush (like this).

Here is the HTML & CSS code snippet:

.testTube #border {
  width: 40px;
  height: 150px;
  margin-top: 15px;
  background-color: white;
  border: 2px solid black;
  border-radius: 5px 5px 25px 25px;
  position: absolute;
}

.testTube #fluidLeft {
  width: 20px;
  height: 100px;
  margin-left: 2px;
  background-color: #ff007f;
  border-radius: 0% 0% 0% 25px;
  position: absolute;
  animation-name: fluid;
  animation-direction: normal;
  animation-duration: 3s;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

.testTube #fluidRight {
  width: 20px;
  height: 0px;
  margin-left: 22px;
  background-color: #ff1a8c;
  border-radius: 0% 0% 25px 0%;
  position: absolute;
  animation-name: fluid;
  animation-direction: normal;
  animation-duration: 3s;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

@keyframes fluid {
  from {
    height: 0px;
    margin-top: 167px;
  }
  to {
    height: 100px;
    margin-top: 67px;
  }
}
<div class="testTube">
  <div id="border"></div>
  <div id="fluidLeft"></div>
  <div id="fluidRight"></div>
</div>

Answer №1

To prevent the fluid divs from overlapping outside of the border, you can move them (#fluidLeft and #fluidRight) into the #border div. Then, apply "overflow: hidden" to the border div (now the parent). I also adjusted the "margin-left" on the fluid divs by 2px to center them in the tube.

.testTube #border {
  width: 40px;
  height: 150px;
  margin-top: 15px;
  background-color: white;
  border: 2px solid black;
  border-radius: 5px 5px 25px 25px;
  position: absolute;
  overflow: hidden;
}

.testTube #fluidLeft {
  width: 20px;
  height: 100px;
  background-color: #ff007f;
  border-radius: 0% 0% 0% 25px;
  position: absolute;
  animation-name: fluid;
  animation-direction: normal;
  animation-duration: 3s;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

.testTube #fluidRight {
  width: 20px;
  height: 0px;
  margin-left: 20px;
  background-color: #ff1a8c;
  border-radius: 0% 0% 25px 0%;
  position: absolute;
  animation-name: fluid;
  animation-direction: normal;
  animation-duration: 3s;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

@keyframes fluid {
  from {
    height: 0px;
    margin-top: 167px;
  }
  to {
    height: 100px;
    margin-top: 67px;
  }
}
<div class="testTube">
  <div id="border">
    <div id="fluidLeft"></div>
    <div id="fluidRight"></div>
  </div>
</div>

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

Utilize the .mat-column-name attributes to apply custom styles to a nested component within Angular Material

Within the Child component, an Angular material table is created with columns passed as input: <table mat-table> <ng-container *ngFor="let col of columns" [matColumnDef]="col"> </table> @Input() columns: string[] T ...

How to adjust the size of the text on a button in jQuery mobile using custom

I am facing an issue with my jQuery mobile buttons that are placed inside a fieldset. Specifically, I need to adjust the font-size of one of the buttons. Despite attempting to add an inline style, it did not have the desired effect. Furthermore, I tried u ...

The page does not respond to scrolling

I am currently modifying a Wordpress theme and I am looking to increase the size of the main content area. However, when I exceed the screen limits, the content does not scroll. I have identified that the issue is commonly caused by the position: fixed pro ...

Utilizing PHP Code within Inline CSS in HTML guides

Is it possible to integrate PHP code into Inline CSS? Here is an example: Check out this code snippet: <div class="col-auto"> <div class="h5 mb-0 mr-3 font-weight-bold text-gray-800">%22</div> </div> <div ...

What causes the delay in CSS animations?

Is there a way to trigger an "updating" image to spin while a long JavaScript function is running? I am currently using JQuery to add a class called "spinning", defined in my CSS for the animation. The problem is that when I add the class and then call a l ...

How to create a vibrant and colourful full background

How can I make the background color fill the entire width of the page? I am new to HTML and CSS, so below is the code I have used for setting the background. Please provide clear instructions on what changes I need to make in my code to achieve this. I kno ...

How can I show hidden column names in the Sencha Touch 2 date picker component with CSS?

I am currently utilizing a date and time picker that has the ability to display ['month', 'day', 'year','hour','minute','ampm']. You can find the source code here. Although everything is function ...

<ul><li>issue with indentation

Is there a way to eliminate the indent from my unordered list? While inspecting the element, I noticed what appears to be padding between the width of the box and the content. However, setting padding to 0px and margin to 0px doesn't seem to work as t ...

Applying hover effect to material-ui IconButton component

As stated in the React Material-UI documentation, I have access to a prop called hoveredStyle: http://www.material-ui.com/#/components/icon-button I intend to utilize the IconButton for two specific purposes: Make use of its tooltip prop for improved ac ...

Saving HTML files can cause formatting issues

After creating a website with the web design tool "Nicepage", I noticed something strange. When visiting the site through this link: It appears to be working perfectly fine (please let me know if it isn't). The layout should resemble this image: htt ...

Curved edges optimized for Safari and Chrome browsers

After exhausting all recommendations to fix the rounded corners problem, I am seeking help. The issue is that it displays correctly on Firefox but not on Safari or Chrome. Below is my code: #sidebar4_content{ margin: 0 auto; float: right; widt ...

Spin rectangular image within boundary using angular 6

I am trying to achieve a rotating effect on a rectangular image within a frame of the same dimensions, as shown below: <div style="border:1px solid #d3d3d3;width:208px;height:250px"> <img style="width:208px;height:250px" src="https://www.webslak ...

Using Selenium to extract and manipulate text content enclosed within specific HTML tags

Imagine you have this snippet of HTML code <tag1> "hello" <tag2></tag2> "world" </tag1> If we use driver.find_element(By.CSS_SELECTOR,"tag1").text, it will display the string "helloworld". ...

choose to display on mobile devices as a dropdown menu rather than a modal window

On mobile devices, my select elements are currently displayed as modals with options. However, I need to change this appearance to resemble the dropdown list view observed on desktop devices. Your assistance is greatly appreciated. ...

Scaling images using jQuery for enhanced visual appeal

I implemented a jQuery hover effect on my image gallery where the images have a default size of 265 x 172 and are displayed horizontally. My goal is to have the images swap out for larger ones sized at 449 x 223 when a user hovers over them. However, I wan ...

Incorporate a Fadein effect into the current CSS for mouseover link interaction

Is there a way to enhance my current css code for a mouseover link with a background image by adding a fade in and out effect? What's the most effective method to achieve this using jquery? .sendB { width: 100%; height: 125px; background: ...

The feature for choosing rows is not functioning properly when using materializecss in Tabulator

While working on my project, I encountered an issue with selecting rows. After some debugging, it was revealed that the culprit behind this behavior is the tabulator_materialize.min.css file. Interestingly, when I don't use this CSS, everything functi ...

Prevent the parent from adjusting to fit the child's content

Recently, I've been working on creating a horizontal menu bar and I have condensed the CSS as much as possible. However, I am facing an issue where I do not want the ul li element to adjust its size based on the content of the ul ul. Any suggestions o ...

Resizable Bootstrap column-md-4

I need to make a bootstrap column fixed position (col-md-4). This is for a shop layout with 2 columns: Shop content (long & scrollable) col-md-8 Cart content (short & fixed so it stays visible while scrolling the shop content) col-md-4 I'm ...

Tips for making a slide-in animation that doesn't require adjusting the browser size

As I work on an animation where an img object and text slide in from outside the frame of the webpage upon page load, everything seems to be running smoothly. But there's one glaring issue: right at the start of the page load, the webpage resizes for ...