Create a div that vanishes when hovered over

I want to create a div element that disappears on hover using CSS without reappearing when moving the mouse again. While I know this can be achieved using JavaScript, I'm looking for a solution that incorporates CSS transitions for a smoother effect:

function CloseRegister() {
    var regdiv = document.getElementById("register");
    register.style.display = "none";
}

I have been experimenting with CSS transitions and here is what I have tried so far:

<style type="text/css">
.register
{
    background-color:Purple;
    transition: 2s;
    -o-transition: 2s;
    -webkit-transition: 2s;
    -moz-transition: 2s;
}
.register:hover
{
    opacity:0;
}
</style>

<div id="register" class="register" style="position:fixed; width:50%; height:50%; top:25%; left:25%;"></div>

EDIT:

I was able to resolve the issue with the help of @GolezTrol:

function CloseRegister() {
    var regdiv = document.getElementById("register");
    register.onmouseup = function () {
        register.onmouseup = null;
        register.classList.add('hovered');
        setTimeout(function () { register.style.display = 'none'; }, 2000);
    }
}
.register
{
    background-color: Purple;
    transition: 2s;
    -o-transition: 2s;
    -webkit-transition: 2s;
    -moz-transition: 2s;
}
.register.hovered
{
    background-color: transparent;
}
<div onmousedown="CloseRegister()" id="register" class="register" style="position: fixed; width: 50%; height: 50%; top: 25%; left: 25%;"></div>

Answer №1

If you are utilizing JavaScript in your project, consider using the onmouseover event. By triggering this event for the first time, you can incorporate a class that activates a CSS transformation effect.

var regdiv = document.getElementById("register");

var hoverEvent = function(){
    // Remove the event listener to prevent multiple calls during the animation.
    register.removeEventListener('mouseover', hoverEvent);
    // Apply the class responsible for the desired animation.
    register.classList.add('hovered');
    
    // Optionally, hide the element from view after a set duration.
    setTimeout(2000, function(){register.style.display = 'none';})
};

register.addEventListener('mouseover', hoverEvent);
.register
{
    background-color:Purple;
    transition: 2s;
    -o-transition: 2s;
    -webkit-transition: 2s;
    -moz-transition: 2s;
}
.register.hovered
{
    opacity:0;
}
<div id="register" class="register" style="position:fixed; width:50%; height:50%; top:25%; left:25%;"></div>

Answer №2

One effective strategy is to pay close attention to the transitionend event and utilize the remove method

$("#register").hover(function(){ 
  $(this).addClass("delete");
  $(this,".delete").on('transitionend webkitTransitionEnd oTransitionEnd', function () {
    $(this).remove()
 });
})
.register
{
    background-color:Purple;
    transition: 2s;
    -o-transition: 2s;
    -webkit-transition: 2s;
    -moz-transition: 2s;
}
.register.delete
{
    opacity:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="register" class="register" style="position:fixed; width:50%; height:50%; top:25%; left:25%;"></div>

Answer №3

Another option is to adjust the following CSS code:

.register:hover {
opacity: 0;
}

You can also try changing it to display:none; or visibility:hidden; if you prefer not to use JavaScript to maintain the layout.

Answer №4

Initially misunderstood with the initial post. This method yields the same outcome using CSS3 animations, however, it may not be compatible with older versions of Internet Explorer.

.register
{
    background-color:Purple; 
}
.hovered-anim
{
    animation-name: example;
    animation-duration: 2s;
    -webkit-animation-delay: 0s !important;
    -webkit-animation-name: example; /* Chrome, Safari, Opera */
    -webkit-animation-duration: 2s; /* Chrome, Safari, Opera */
    -webkit-animation-fill-mode: forwards;
}
@keyframes example {
    0% {opacity: 1;}
    100% {opacity: 0;}
}
@-webkit-keyframes example {
    0% {opacity: 1;}
    100% {opacity: 0;}
}

jQuery:

 $(".register").hover(function(){ 
    $(this).addClass("hovered-anim");
});

For reference, you can access the fiddle via the following link:

https://jsfiddle.net/tc78axye/2/

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

Can I inspect the HTML source of GWT?

How can I access the HTML source code generated by GWT? Currently, I only see the DIV with a specific ID when viewing the source. Is there a way to view the entire HTML output? Is it possible to design my table in HTML using divs and lists, then wrap it i ...

Using Bootstrap and CSS to style a div element and fill it

I'm struggling with a div that splits in two, where I've placed an image on the left side. However, the image doesn't seem to fill the entire space and there are empty spaces on the sides. I'm using bootstrap, but any CSS solution would ...

Avoiding the appearance of a scrollbar when content extends beyond its containing block

I am struggling with writing markup and CSS to prevent a background image from creating a scrollbar unless the viewport is narrower than the inner content wrapper: The solution provided in this post didn't work for me: Absolutely positioned div on ri ...

Prevent Bootstrap 5 input fields from automatically adjusting to the height of a CSS grid cell

Below is some Bootstrap 5 markup that I need help with. The first example is incorrect. I don't want the Bootstrap input to be the same height as the grid cell. The second example is right, but I want to achieve the same result without using a wrapp ...

How can I incorporate dashed borders between images using CSS?

New to the world of HTML and CSS, I'm trying to replicate a design I came across online for some practice. The website layout includes images separated by borders, and I'm unsure if this particular design can be achieved using regular CSS alone o ...

Modify table row background color using PHP based on its position in the table with the use of a function

I am having trouble formatting my table to highlight different rows in distinct colors. The top row should be one color, the second row another, and the bottom two rows a different color each. This is to represent winners, teams getting promoted, and tho ...

The Bootstrap DateTimePicker is displaying on the screen in an inaccurate

I'm having an issue with the datetimepicker on my project. The calendar appears incorrectly in Chrome, but looks fine in Edge. Here's a screenshot from Chrome: https://i.stack.imgur.com/Hi0j4.png And here is how it looks in Edge: https://i.stac ...

PHP Linking Style Sheets

I am exploring the use of an HTML Diff Tool in PHP, specifically looking at this one: https://github.com/rashid2538/php-htmldiff. I am fetching the content of both pages using cURL and passing it to the HTML-Diff tool. It works perfectly fine, but there is ...

jQuery is great at adding a class, but struggles to remove it

When you click on an anchor with the class .extra, it adds the "extra-active" class and removes the "extra" class. However, when you click on an anchor with the extra-active class, it does not remove the extra-active class and replace it with extra. Here ...

How can I extract text from a website without retaining number, dot, and alphabet bullets in the list?

I am currently using Python 2.7.8 for a project involving a website with text displayed in an ordered list format using ol tags. I need to extract the specific text items listed below: Coffee Tea Milk This is an example of the HTML code used on my websit ...

When floating divs with varying heights are wrapped, they may become stuck in place

Hey there! Let's tackle a challenge: I've got a few divs, each with varying heights. These divs contain portlets that can expand in height. Here's a basic example: .clz { float: left; } <div class="container"> <div class="cl ...

decorating elements in a line with colored backgrounds

I am facing an issue where I want to keep three divs aligned horizontally on the same line, but their background colors are causing them to not align properly. Adding margin or padding causes the divs to wrap up instead of staying in line. #service_cont ...

Apply a coating of white paint and add color to the border

I was struggling to create a form that has a white background color and a green border at the same time. I am using Bootstrap and it seems like I cannot apply both styles simultaneously. Here is the link to the form design I am trying to achieve: . Below ...

When using onclick="location.href='page.html'", the page fails to load on Safari browser

I've been having trouble with getting onclick="location.href='link.html'" to work and load a new page in Safari (5.0.4). In my project, I am creating a drop-down navigation menu using the <select> and <option> HTML tags. I have ...

Creating a Bootstrap 4 DIV element with horizontal scrolling functionality

I've been struggling to implement a horizontal scroll for a DIV on my site, but none of the common solutions like overflow-x or white-space:nowrap are working for me. This is the section of my website where I need a horizontal scroll bar If you want ...

Is it possible to adjust table rows to match the height of the tallest row in the table?

I've been attempting to ensure that all table rows have the same height as the tallest element, without using a fixed value. Setting the height to auto results in each row having different heights, and specifying a fixed value is not ideal because the ...

How can we integrate this icon/font plugin in CSS/JavaScript?

Check out the live demonstration on Jsfiddle http://jsfiddle.net/hc046u9u/ <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materializ ...

What is the best way to achieve this Bootstrap design without copying the content?

Trying to achieve a specific Bootstrap layout without repeating content is my current challenge. Essentially, I aim to divide some content into 2 columns at the sm/md breakpoints and then switch to 3 columns for the lg breakpoint. The layout for sm/md bre ...

What is the best way to create a single HTML file that can showcase several different pages?

I am just starting out in web design, and I am currently working on a hobby project which is an e-commerce site. My issue is that I don't want to manually link every single product page. This would result in a large number of HTML files. Instead, I w ...

Learn how to apply CSS styles from one component to another in Angular

I am attempting to modify the background color of a wrapper from a different component. I tried using ::ng-deep but it's not functioning correctly. For the mauto component, the background should be a solid block color. For the vgud component, the back ...