The art of transitioning CSS elements

I've tested this code in multiple browsers, but it still isn't functioning correctly. Can anyone help me identify the error?

<style>
.result{            
    width: 200px;
    height: 300px;
    background: #ff0000;
}           
.result #add{
    display:none;
}
.result:hover #add{
     -webkit-transition: top 1s ease-out 1.5s;
     -moz-transition: top 1s ease-out 1.5s;
     transition: top 1s ease-out 1.5s;
     display:block;
}
</style>

<div class="result">
    <div id="add">111</div>  
</div>

Answer №1

Preliminary steps for tidying up

  1. The transition from display: none to display: block is not possible. Instead, consider transitioning from opacity: 0 to opacity: 1 for a smooth fade-in effect.
  2. Your transition: top 1s ease-out 1.5s statement only animates the 'top' attribute. To target multiple attributes, use a comma-separated list such as
    top 1s ease-out, opacity 1s ease-out
    . Alternatively, using transition: 1s ease-out will animate all changes, albeit with potential minor performance implications.
  3. For CSS transitions to function correctly, both an initial and a destination state are required. Ensure your code includes both states like this example:


.result{            
    width: 200px;
    height: 300px;
    top: 0;
    background: #ff0000;
}           
.result #add{
    opacity: 0;
    position: relative;
}
.result:hover #add{
    -webkit-transition: 1s ease-out 1.5s;
    -moz-transition: 1s ease-out 1.5s;
    transition: 1s ease-out 1.5s;
    opacity: 1;
    top: 40px;
}

With these adjustments, your code should now function properly.

Answer №2

There are 3 key issues to address:

  • The top property is being passed into the transition (top 1s ease-out 1.5s;) without a specific reference to top.
  • No position property has been set for the #add element.
  • Instead of using display:block;, consider utilizing opacity as it triggers a smoother redraw process and allows for proper CSS calculations necessary for applying the top transition.

Here is an example incorporating these fixes:

.result{            
    width: 200px;
    height: 300px;
    background: #ff0000;
}           
.result #add{
  opacity: 0;
  position:relative;
  top:0;
}
.result:hover #add{
  opacity: 1;
   -webkit-transition: top 1s ease-out 1.5s;
      -moz-transition: top 1s ease-out 1.5s;
           transition: top 1s ease-out 1.5s;
  top:150px;
}
<div class="result">
    <div id="add">111</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

Web page containing information from IPv6 and IPv4 sources exclusively

Is it possible to have an HTML5 page accessible through both IPv4 and IPv6, while only allowing CSS style and JavaScript from other domains via IPv4? Unfortunately, it seems that this setup does not function properly for pure IPv6 connections. Will th ...

the division does not span the entire width

I am faced with a dilemma involving two div elements structured as follows: <div id="left"><p>.....</p><br/> <p>.....</p> </div> <div id="right"><img ..../></div> Accompanied by CSS styling: #l ...

Using AREA Coordinates to create a custom Image Map with Image-Based Rollovers, incorporating jQuery for enhanced functionality if desired

I am looking for a solution similar to this: http://plugins.jquery.com/project/maphilight However, instead of just adding a border or fill color to <AREA> tags on rollover, I need them to display a background image. The image should be clipped by t ...

The JSP page is unable to locate or access relative resources such as CSS stylesheets and images

I'm struggling to create a basic web page using JSP. The issue I'm facing is that no resources, such as images or CSS, are being found regardless of the URL I use. Here is my simple JSP code: <%@ page language="java" contentType="text/html; ...

"Exploring the relationship between UIWebView and CSS's fixed positioning

My UIWebView is set up with a fixed position header. Everything works seamlessly when the initial view of the ViewController containing the UIWebView is displayed. However, if I present that same ViewController using a Modal segue, an issue arises. When ...

Bring div button on top of the contenteditable field

I am working on an Angular app for a client and need to implement a clickable button at the bottom right of a contenteditable element, similar to the image shown below : https://i.sstatic.net/J6XdW.png The challenge is that the content needs to be scroll ...

Updating the background color using typescript

Before transitioning to angular development, I had experience working with vanilla Javascript. I encountered a challenge when trying to modify the css properties of specific elements using Typescript. Unfortunately, the traditional approach used in Javascr ...

What is the optimal backup plan for selecting rem as the font-size unit in a dynamic layout?

Recently, I started pondering the best fallback option to use when employing rem as the font-size unit. While pixels seem like a suitable choice, it becomes cumbersome to adjust every px-based font-size if you decide to modify the global font-size within a ...

Struggling to align Bootstrap toasts in the center horizontally

Currently utilizing Bootstrap 4.6.1 (due to compatibility issues with bootstrap-select and newer versions) and endeavoring to center some toasts horizontally within a page using the code below: <div class="d-flex flex-row justify-content-c ...

Make the bootstrap row stretch beyond the confines of the container

Our website is currently using Bootstrap 3, and one of our team members has proposed a new template design that deviates from the typical Bootstrap grid layout. I've attempted to implement this design but have faced some challenges along the way. I h ...

How come my content is overflowing beyond the edges of the page?

Hey there! Check out my webpage I'm working on: I've run into an issue where there is a lot of unnecessary whitespace after the footer, but oddly enough it only shows up in browsers other than Mozilla Firefox. I attempted to investigate using i ...

Circular loading indicator (border)

I stumbled upon this intriguing design concept on dribbble. Do you think it's feasible to replicate the circular progress bar (with the progress being shown as the outer border of the circle) within the first few seconds of the gif? Currently, I&apo ...

HTML Canvas Width Specification

Struggling with it is the initial width of my canvas. My setup consists of a canvas element with an image background set to cover. Using jQuery, the canvas width is set to window.innerWidth while the height is calculated as one-third of the width (to main ...

(Material UI version 5) Custom global style enhancements

One of the examples in MUI is the Global style overrides: const theme = createTheme({ components: { // Name of the component MuiButton: { styleOverrides: { // Name of the slot root: { // Some CSS fontSize ...

What prevents a click event from reaching the <body> element?

This slider runs in an animation loop until one of the dots at the top is clicked. Once clicked, the animated elements (the <span> dots and the <figure>s) receive a style that halts the animation. <section id="slider"> <span class ...

How to style material-ui input with outlined variant using css

Currently, I am attempting to replicate the material-ui outlined input. The background color and input styles are different, so simply setting the label position to absolute and pushing it up is not working for me. Any suggestions on how to achieve this? ...

Troubleshoot: Issue with Active Class in Bootstrap Carousel LI Item

I modified the bootstrap 4 carousel code by splitting it into two columns. This was necessary because I wanted to move the carousel indicators outside of the carousel itself, rather than having them inside as per the default setup. While the image-changin ...

Center align the text in a vertical group placed over an image

Here is my html page along with its corresponding css code: <div class="container"> <div class="row mt-4"> <div class="col-sm-8"> <h2>blah blah</h2> <p>blah blah&l ...

A feature of HTML5 and CSS3 is a modal window that automatically closes when the user

After following the steps outlined by Keenan Payne, I successfully created a modal window using HTML5 and CSS3. However, I am now seeking assistance with setting it up to close when users click on the darkened background outside of the modal window. Can an ...

ReactJS Material UI Horizontal Menu with CSS Styling

I am currently working on creating a horizontal menu with ReactJS and Material UI. However, I am facing a challenge where the menu is not responsive. Even when I resize the browser window, the menu size remains the same and only updates when I refresh th ...