Strategies for creating smooth transitions for elements using CSS transformations

Is there a way to smoothly fade in edits to elements in CSS when hovered over? I am looking to change the border from solid to dotted, but want the transition to be gradual. How can this be achieved?

UPDATE:

To provide more context, here is the current code snippet I am working with:

li {
  font-family: 'Roboto', sans-serif;
  font-size: 20px;
  color: black;
  border-bottom: 1px solid black;
}

li:hover {
  border-bottom: 1px dotted black;
  opacity: 1;
  transition: opacity 1s ease-in-out;
}

Answer №1

Check out this CSS hack that creates a unique effect, although it has the limitation of not allowing transparency on the inside.

div {
  position: relative;
  width: 50px;
  height: 50px;
  display: block;
  border: 3px dotted red;
  background: red;
  transition: 1s ease-in-out all;
}

div:after {
  position: absolute;
  content: ' ';
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
}

div:hover {
  background: transparent;
}
<div></div>

Answer №2

You can overlay 2 divs with different stroke styles and then animate them disappearing.

* {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
  margin: 0;
}
body {
  display: flex;
}
div {
  width: 11rem;
  height: 11rem;
  margin: auto;
  background-color: #f2f2f2;
  border: 5px #bbb solid;
  border-radius: 2rem;
  opacity: 1;
  cursor: pointer;
}
.dotted {
  border: 5px #bbb dotted;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate( -50%, -50% );
  z-index: 0;
}
.solid {
  position: relative;
  z-index: 1;
  transition: opacity 1s;
}
.solid:hover {
  opacity: 0;
}
<div class="solid"></div>
<div class="dotted"></div>

Answer №3

To achieve this effect, pseudoelements can be utilized with the added benefit of working seamlessly with transparent backgrounds. A gradient has been included in the body to showcase this feature.

div {
  position: relative;
  
  /* additional styles for demonstration purposes */
  width: 250px;
  height: 250px;
  border-width: 5px;
  border-style: solid;
  transition: 1s ease-in-out all;
  border-radius: 10px;
}

div:after {
  position: absolute;
  content: ' ';
  top: -5px;
  bottom: -5px;
  left: -5px;
  right: -5px;
  border-width: inherit;
  border-style: dotted;
  border-radius: inherit;
}

div, div:after {
  border-color: tomato;
}

div:hover {
  border-color: transparent;
}

/* demonstration styles indicating compatibility with transparent backgrounds */
body {
  background-image: linear-gradient(to bottom, transparent, #ddd);
  min-height: 100vh;
  margin: 0;
}
<div></div>

Answer №4

While this solution may not be the most elegant, it was inspired by 'Niet the Dark Absol's' comment on my original question. Take a look at the code snippet below:

li {
  font-family: 'Roboto', sans-serif;
  font-size: 20px;
  color: black;
  position: relative;
  border-bottom: 1px solid transparent; /* invisible border */
}

li:before, li:after {
  content: '';
  position: absolute;
  left: 0; right: 0;
  top: 0; bottom: 0;
  margin: -1px; /* same as border width */
  transition: opacity 1s linear;
}

li:before {
  border-bottom: 1px solid #000;
}

li:after {
  border-bottom: 1px dotted #000;
  opacity: 0;
}

li:hover:before {
  opacity: 0;
}

li:hover:after {
  opacity: 1;
}

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

Issues with div and hyperlinks/forms

I am currently working on creating a left sidebar in my HTML code, but I am still new to this. In the div for the sidebar, I have three buttons. However, Weebly keeps notifying me of an issue with the <a> tag, and the second button's link appear ...

Using PHP to calculate the total number of records within an HTML document

I am currently working on a PHP script to establish a connection with my MySQL database in order to retrieve the total number of users registered on my forum by counting the records in the table. https://i.sstatic.net/ZR0IY.png The PHP script should disp ...

Tips for steering clear of dragging and dropping layers

Currently, I am utilizing the drag and drop functions from w3schools to implement a drag and drop feature. For better comprehension, you can take a look at my code: http://codepen.io/TutTut/pen/qIGkD I have noticed that when dropping one term onto anothe ...

Set the text alignment to the left/start inside a div contained within a Material UI Box

Trying to figure out how to make the error handling ui of this field look like another field's error handling ui. Note that in the second example, the error text is aligned to the left. How can I achieve this alignment without considering text color ...

CSS: The deleted style refuses to disappear

Despite removing CSS code from my Rails application, after restarting the server and refreshing the page, I still see that the code is in effect. Even when inspecting the elements using Chrome, the code remains visible. @media screen and (min-width: 961px ...

What is the process for integrating php script within javascript?

Is it possible to incorporate php scripts into javascript[1] in the same manner as it can be done in html[2]? server.php: <?php echo 'hello World'; ?> client.js (or client.php if needed): function foo() { var i = <?php include ...

What is the process for utilizing Angular's emulated encapsulation attributes on HTML elements that are dynamically added to an Angular component?

Within my custom Angular component, a third-party library is dynamically adding an HTML element. I am seeking a solution that can apply Angular's encapsulation attributes to these elements regardless of the specific third-party library being used. If ...

Ways to Conceal <div> Tag

I need help with a prank .html page I'm creating for a friend. The idea is that when the user clicks a button, a surprise phrase pops up. I have managed to hide and unhide the phrase successfully using JavaScript. However, my issue is that when the pa ...

Guide to generating customized CSS styles on-the-fly in Vue (similar to Angular's dynamic styling capabilities)

When working with Angular, we have the capability to dynamically set CSS properties. For example: <style ng-if="color"> .theme-color { color: {{color}}; } .theme-background-color { background-color: {{color}}; } .theme-border-color { border-color: { ...

Is there a way to open a link in a new window without using the _blank attribute?

I am facing an issue where I have a webpage with a URL linking to a PHP file that displays an image, and I want to open this picture in a new window without the blank page showing up. Currently, when I click on the URL using the code below, a new window o ...

Executing JavaScript code from an external link

Currently, I am in the process of developing a horizontal parallax website. The functionality is working seamlessly; when I click on the menu, the slides smoothly transition horizontally and display the corresponding content. However, I have encountered a ...

I'm curious as to why a webpage tends to load more quickly when CSS files are loaded before script files. Can anyone shed some

While watching a video, I came across some concepts that were a bit difficult for me to grasp. The video mentions that scripts are 'serialized' and can block subsequent files from loading. According to this explanation, Script 1 would load first ...

Tailwind CSS - when it comes to styling, larger screen media queries are taking precedence over smaller media queries

Currently tackling screen responsiveness issues in a Nextjs + Tailwind CSS project. All Tailwind breakpoints are functioning correctly except for "sm" and below. Snippet of the problematic code: <div className="bg-red-200 sm:bg-white md:bg-gray-70 ...

Integrate a scrollbar seamlessly while maintaining the website's responsiveness

I encountered an issue where I couldn't add a scrollbar while maintaining a responsive page layout. In order to include a scrollbar in my datatables, I found the code snippet: "scrollY": "200px" However, this code sets the table size to 200 pixels r ...

encountering difficulty incorporating menu.php within main.php

I'm currently working on integrating the menu.php file into my main.php. Here is what I have accomplished so far: Main.php <html> <body> <?php include("menu.php"); ?> <p>This is an example to demonstrate how ...

Dealing with pesky table cell padding issues in Chrome

Struggling with table cell paddings in Chrome because it appears that if a table cell doesn't have pure text, the padding isn't applied. Here's my code: <table> <tr> <th></th> <th ...

What is the best way to retrieve the values of a select element from LEVEL 4 within the form submission of LEVEL 3?

To enhance readability, the intricate code has been abstracted. Within our Angular 2 project, we are working with a component called <top-component> (LEVEL 1): <top-component> </top-component> This component includes a template known a ...

A JavaScript code snippet that stores the total number of bytes read from a CSV file in a variable

I currently have a CSV file located on a web server that is regularly updated with numeric and string data of varying lengths. I am seeking a solution to calculate the total byte values of each row when reading the file, as the byte count is crucial due ...

Adding a class to a div upon loading: A guide

Currently using the following script: $(document).ready(function() { $(".accept").change(function () { if ($(this).val() == "0") { $(".generateBtn").addClass("disable"); } else { $(".generateBtn").remove("dis ...

Troubleshooting: Style sheets not loading on a page rendered by node

Today was my first attempt at learning Node.js and creating a basic server to display a single page with a header tag. However, I encountered an error stating that the CSS file could not be loaded. This is the code I used: const express = require('ex ...