When incorporating a CSS transition, a div element gracefully descends from the top of the HTML body

I am relatively new to html/css and have encountered a slight issue while trying to add transitions to a div element. When I hover over the div element, I would like it to smoothly transition its background-color from red to black.

Here is the code snippet for my html and css:

body {
  margin: 0;
}

.example1 {
  margin: auto;
  margin-top: 100px;
  width: 200px;
  height: 200px;
  background-color: red;
  transition: all 1s linear;
  -moz-transition: all 1s linear;
  -o-transition: all 1s linear;
  -webkit-transition: all 1s linear;
}

.example1:hover {
  background-color: black;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>Test Page</title>
  <link rel="stylesheet" href="transitions.css" />
  <link rel="stylesheet" href="normalize.css" />
</head>

<body>
  <div class="example1"></div>
</body>

</html>

This demonstrates the outcome: https://youtu.be/5whZpt0YR68

The color transition functions as intended, but when the html file is opened, the div element slides down from the top of the body until it reaches its final position (unanticipated movement). This behavior is observed in Google Chrome, Opera, and Microsoft Edge, but not Firefox.

Any insights on why this anomaly might be occurring?

Answer №1

It is recommended to update this section in your CSS:

transition: all ...

to

transition: background-color ...

For instance:

body {
  margin: 0;
}

.sample1 {
  margin: auto;
  margin-top: 100px;
  width: 200px;
  height: 200px;
  background-color: red;
}

.sample1:hover {
  background-color: black;  
  transition: background-color 1s linear;
  -moz-transition: background-color 1s linear;
  -o-transition: background-color 1s linear;
  -webkit-transition: background-color 1s linear;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>Test Page</title>
  <link rel="stylesheet" href="transitions.css" />
  <link rel="stylesheet" href="normalize.css" />
</head>

<body>
  <div class="sample1"></div>
</body>

</html>

To enhance the visual effect further, you can also utilize a small amount of Javascript. For example:

let sample = document.querySelector('.sample1');

sample.addEventListener('mouseover', (e) => {
e.target.classList.remove('out_sample1');
});

sample.addEventListener('mouseout', (e) => {
e.target.classList.add('out_sample1');
});
body {
  margin: 0;
}
.out_sample1 {
  margin: auto;
  margin-top: 100px;
  width: 200px;
  height: 200px;
  transition: background-color 1s linear;
  -moz-transition: background-color 1s linear;
  -o-transition: background-color 1s linear;
  -webkit-transition: background-color 1s linear;
  background-color: red;
}
.sample1 {
  margin: auto;
  margin-top: 100px;
  width: 200px;
  height: 200px;
  background-color: red;
}

.sample1:hover {
  background-color: black;  
  transition: background-color 1s linear;
  -moz-transition: background-color 1s linear;
  -o-transition: background-color 1s linear;
  -webkit-transition: background-color 1s linear;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>Test Page</title>
  <link rel="stylesheet" href="transitions.css" />
  <link rel="stylesheet" href="normalize.css" />
</head>

<body>
  <div class="sample1"></div>
</body>

</html>

Answer №2

Make sure to eliminate any unnecessary CSS files that may be impacting the design.

body {
    margin: 0;
}

.example2 {
    margin: auto;
    margin-top: 50px;
    width: 150px;
    height: 150px;
    background-color: blue;
    transition: all 0.5s linear;
    -moz-transition: all 0.5s linear;
    -o-transition: all 0.5s linear;
    -webkit-transition: all 0.5s linear;
}

.example2:hover {
    background-color: green;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Test Page</title>
</head>
<body>
    <div class="example2"></div>
</body> 
</html>

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

Modifying properties through JavaScript is not possible

I created an HTML form and I'm trying to change the attributes of a div and a button but for some reason, it's not working <form> <button type='button' id='other'>Sub</button> <select id="prop"> &l ...

Struggling to connect HTML elements with AngularJS and TinyMCE?

My objective is to edit text using tinymce, persist it in a database, and display it within a div by incorporating angularJS for the same styling and formatting. I am utilizing tinymce 3.5.8 with an angularUI directive. I have successfully saved the wysiw ...

Is it possible to use HTML code as content in CSS?

How do I go about displaying the variable $text as HTML? Take a look at the code snippet provided below: <?php header('Content-type: text/css'); $background = "#ffafff"; $color = "#000000"; $green = "#16a86f"; $text= '<h1&g ...

How come my web page is not displaying the guages from guage.js?

I am utilizing guage.js to create a graph based on this Fiddle. However, upon adding the same code to my website, the rendering does not appear as expected: https://i.sstatic.net/fIkQr.png Upon inspecting in Chrome developer tools, I noticed that the ele ...

javascriptretrieve the second class from the element

HTML Code : <span class="button" onclick="javascript:buttonClicked();">Log In</span> <div class="modal-bg" style="display: none;"> <div class="modal"> <span>Log In<a href="#close" class="close">&# ...

What is the best way to ensure a jQuery function runs on duplicated elements?

I have been attempting to construct a webpage featuring cascading dropdowns using jQuery. The goal is to generate a duplicate set of dropdowns when the last dropdown in the current set is altered. I aim for this process to repeat up to 10 times, but I cons ...

Utilizing JavaScript XHR to Organize Data Retrieved from an API

I have successfully connected FDA's Drug API with XMLHttpRequest() and now I need help sorting the fetched data in ascending order based on its GENERIC NAME (results["open_fda"].generic_name). Importing all arrays from the API seems impractical, even ...

Adding gradient to the background cover photo

I am currently working with a JavaScript fiddle that has a background image that I am using as a cover without distorting the image. My goal is to add a gradient on top of this image, but despite finding similar questions, I can't seem to make it wor ...

What could be the reason why this LESS CSS is not taking effect?

Why won't this stylesheet load properly? The desired background color is supposed to be similar to cadetblue. You can view my page with the linked home.less.css at: ...

What is the best way to extract HTML elements during the process of web scraping?

Currently, I am attempting to extract data from a webpage using Python. Here is my existing Python code... from lxml import html import requests if __name__ == "__main__": page = requests.get('https://www.example.com/example') tree = ht ...

Steps to eliminate the x icon from the text box that appears automatically in Internet Explorer

I'm currently working with a UI5 text box that includes a built-in option for clearing the text using a 'X' button. However, I've noticed that in Internet Explorer, an additional default cross mark is being added alongside the UI5 cros ...

What is the secret behind positioning the tooltip correctly using 'relative' placement?

Check out this example of a CSS tooltip. The author has positioned the tooltip relatively. .tooltip{ display: inline; position: relative; } However, in this guide, it explains, Relative. This type of positioning can be confusing and often misund ...

Creating a seamless video texture loop using PixiJS

I have successfully loaded a video texture and rendered it using pixi. // initialize a new pixi stage var stage = new PIXI.Stage(0x66FF99); // create a renderer instance var renderer = PIXI.autoDetectRenderer(window.innerWidth, window.inner ...

Is there a way for me to remove an uploaded image from the system

Here is an example of my HTML code: <input type='file' multiple/> <?php for($i=0;$i<5; $i++) { ?> <div class="img-container" id="box<?php echo $i ?>"> <button style="display: none;" type="submit" cl ...

Showcasing a dynamic image as a task is being completed within a JavaScript function

Is there a way to show a loading image while waiting for a JavaScript function to finish? <script type="text/javascript"> function create() { //Perform operation } </script> I am looking for a solution to display a loading image until ...

Having trouble with your Bootstrap Accordion panels?

I am in the process of creating a unique accordion-style panel for a client's website. However, I am facing difficulties as this is my first attempt at setting up such panels and it doesn't seem to be functioning correctly. I'm not sure if I ...

Hiding the C3 tooltip after engaging with it

I'm currently expanding my knowledge on utilizing C3.js for creating charts, and one aspect I'm focusing on is enhancing the tooltip functionality. Typically, C3 tooltips only appear when you hover over data points as demonstrated in this example ...

Creating a PDF document using HTML code

I am interested in creating a PDF file from an HTML code that includes the stylesheet rules using PHP. Currently, I am attempting to achieve this with the MPDF library. However, the generated PDF does not resemble the original HTML page. Many elements app ...

Switching background images using a JavaFX Button click

I am attempting to modify the background image of a scene using CSS. Here is the code snippet from the CSS file: .SettingsGrid { -fx-background-image: url('Background.quiz.jpg'); /*-fx-background: #ffffff;*/ -fx-background-size: cove ...

Is it possible to rotate just the camera in ThreeJS by dragging the mouse within the scene?

Currently, I am involved in a project using ThreeJS and I am looking to implement camera rotation using the mouse. Although I have come across OrbitControls that allow me to rotate the camera around a point or object, I am unable to achieve the camera rota ...