Animating an element from its center with pure CSS

I've dedicated countless hours to uncovering a solution that can be achieved using only CSS. My goal is to create an animation on a div element where its height and width decrease uniformly when hovered over.

<div class="a"></div>

Above you'll see a basic div element with the following CSS styling:

.a {
    width: 350px;
    height: 350px;
    background: #000;
    margin: 100px 0 0 400px;
    transition: all 2s linear;
    position:fixed;
}

The objective is to shrink both the height and width of the element from the center upon hover, utilizing CSS exclusively.

Answer №1

Implement the scale transformation:

.b {
  width: 300px;
  height: 300px;
  background: #333;
  margin: 20px;
  transition: all 3s ease-in-out;
  position: absolute;
}
.b:hover {
  transform: scale(1.5);
}
<div class="b"></div>

Answer №2

If you're searching for a specific function, it's the scale() one. To meet your needs, the ideal approach is to trigger the scale() function when hovering over the target element like so:

.a {
  width: 350px;
  height: 350px;
  background: #000;
  margin: 10px;
  transition: all 2s linear;
  position: fixed;
}
.a:hover {
  transform:scale(0.7);
}

This CSS function will enlarge the div element while maintaining its center. Refer to this link for a more detailed explanation.

Answer №3

Implement the :hover effect using the pseudoclass.

.a:hover {
   width: 300px;
   height: 300px;
}

The transition on element .a will function as intended.

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

Navigational strip within the grid

My grid has a size of 300px. When there are more than 10 records, a vertical scroll bar appears, which is fine. However, at the same time, a horizontal scroll bar also appears, which I do not want to display. My CSS class for vertical scrolling is: .vstyl ...

Unable to assign a className to a personalized React component - troubleshooting needed!

I have a component that relies on another component. I am trying to pass CSS positioning from the outer component to the inner one by using the following code: import OptionsMenu from './OptionsMenu' import { withStyles } from 'material-ui/ ...

The XmlUtil.serialize(root) function is converting empty tags into self-closing tags

Check out this cool code snippet featuring an empty div tag (<div></div>): import groovy.xml.DOMBuilder import groovy.xml.XmlUtil def HTML_STRING = ''' <html> <div></div> <div>Some text</d ...

Stop capturing mouse and keyboard events within a specific div element on all web browsers

I manage a website with forms that have jquery autosave features and are updated using ajax. These forms include various types of input elements such as textboxes, jQuery UI datepickers, and time pickers... <div id="content"> <form id="line1"&g ...

The error message indicates a validation issue with the img tag: The attribute src has an invalid value, as it uses a backslash () as a path segment delimiter instead of

<div class="logo"> <img src="assets\images\main-logo\logo.jpg" alt="logo"> </div> </a> </div> The code was validated using validate.w3.org and this error was encountered: Bad value asse ...

Transform black and white image to vibrant colors and add various hover effects

There are three images displayed on this webpage: This website is built on WordPress and utilizes the WP Bakery plugin for designing layouts. The images here are set to change from color to grayscale, and back to color upon mouseover. The following CSS c ...

Error: Unexpected syntax error occurred due to the use of '===' operator

Click here for image descriptionError: There is a syntax error in line 7 of the file add.php Code: <?php require_once('db.php'); if ( !empty($_POST['name']) &&!empty($_POST['alias']) &&!empty($_POST[&apo ...

CSS unable to modify the color of the switch in HTML code

I've been struggling to change the color of the Switch to yellow when it's turned on. Despite my attempts, I haven't been successful in doing so. Is it even possible to achieve this color change? <Switch size="small& ...

Centering text underneath bootstrap image design

After trying various methods, I still couldn't get the text to display directly below my images in the center. Below is the code I attempted: <div class="our_partners"> <div class="container"> <div class="row"> ...

Using Radio button to access local HTML pages - A step-by-step guide

I am currently working on a project that involves the use of radio buttons and their corresponding options. My goal is to have each radio button selection lead to a specific HTML page being displayed. I've come across solutions involving external URLs ...

"Create a New Browser Tab When User Interacts with a Hyperlink leading to an External Website

I am currently working on a recommendations app built with React and I want to enable users to navigate to external websites when they click on a component that displays information about that website. At the moment, I have implemented a wrapper that, upo ...

Retrieve data from an ASP.NET Web API endpoint utilizing AngularJS for seamless file extraction

In my project using Angular JS, I have an anchor tag (<a>) that triggers an HTTP request to a WebAPI method. This method returns a file. Now, my goal is to ensure that the file is downloaded to the user's device once the request is successful. ...

Unable to load the CSS file

Having trouble with the folder structure in my content directory: -Content --jquery-ui-1.10.4.custom --------------------------css --------------------------smoothness ------------------------------------jquery-ui-1.10.4.custom.css ----------------------- ...

Tips for utilizing append and remove to modify HTML forms

Currently, I am working on a WordPress project that requires the use of append and remove for dynamically changing forms in HTML. I attempted to implement this functionality, but encountered some errors. The code snippet below provides a brief overview of ...

Assign the chosen option value to a PHP variable for storage

Is there a way to assign the selected value of a select input to a php variable? I'm trying to take the value that is selected in a select box and store it in a php variable, then echo out the selected option value. However, I am having trouble access ...

Top method for uploading outside materials

My website is running smoothly with fast load speeds on a one-page layout, but I want to ensure it stays that way by preventing slow loading times. Right now, I have a portfolio page with tiles that pop up an overlay and slider when clicked. The current m ...

Add CSS styling to a section of a canvas

I want to use a specific cursor png for just a portion of a canvas. Currently, I have code that applies the cursor to the entire canvas like so: .myClass { cursor: url('../img/myCursor.png') 7 33, auto; /* img hotspot centred*/ } However, I ...

Tips for transferring PHP variable from a drop down menu

Hello, I am currently working on creating a dropdown menu using the <select> tag. My goal is to have it so that when someone clicks on one of the options in the dropdown, a new window opens. Additionally, I want the PHP variable from the main page to ...

Setting the height of the text area in a three-column layout cannot be achieved using a percentage value

I'm working on creating a 3-column layout with two text areas. The issue I'm facing is that when I adjust the width of the text areas to create columns, the height shrinks. I need a solution that will be compatible with HTML5 browsers - support f ...

Having difficulty modifying the width of the BODY element using CSS

For my webpage, I want the body to have a silver background that fills only 75% of the page. This means that only 75% of the page will be painted silver, while the remaining part will be left unused and painted according to the browser's defaults. The ...