Revealing child elements by expanding the absolute div from the center

I am trying to create a rectangular mask that expands on hover to display its children with varying heights. Currently, I have achieved this using an absolutely positioned div that adjusts its left, width, and max-height properties. However, I would like it to appear as though it is expanding from the middle rather than revealing from the top-left corner all at once.

<div id="container">
 <div id="mask">
  <div id="background"></div>
 </div>
</div>

    #container {
     width: 300px;
     height: 300px;
     background-color: black;
    }

    #mask {
     width: 50px;
     max-height: 50px;
     position: absolute;
     top: 125px;
     left: 125px;
     overflow: hidden;

     transition: width 1s, max-height 1s, left 1s, top 1s;
    }

    #background {
     background-image: url(https://www.pngfind.com/pngs/m/299-2991041_memes-para-stickers-png-png-download-surprised-pikachu.png);
     background-size: contain;
     width: 150px;
     height: 125px;
    }

    #mask:hover {
     width: 150px;
     max-height: 1000px;
     left: 75px;
     top: 75px;
    }

Check out the live demo here: https://codepen.io/jraynolds/pen/OJJxpOm

Answer №1

#1 – Utilizing clip-path

Compatibility: Supported by all modern browsers except Edge. Limited support in IE10/11 and Edge using url() only.

Clip-path Example

To crop an image, apply the clip-path: inset() property. Here's a demonstration where a 120-pixel box is reduced to 0 on hover.

.reveal {
  background: url(https://i.sstatic.net/3v1Kz.jpg) no-repeat;
  background-size: 150px;
  background-position: center;
  height: 300px;
  width: 300px;
  clip-path: inset(120px 120px 120px 120px);
  transition: clip-path 0.5s;
}

.reveal:hover {
  clip-path: inset(0 0 0 0);
}

/*for example*/
body {
  background: linear-gradient(to bottom right, black 0%, white 100%);
  height: 100vh;
}
<div class="reveal"></div>

Using url() (not working in Edge or IE)

An attempt was made!

Create an SVG like this:

<svg>
    <clipPath id="square">
         <rect />
    </clipPath>
</svg>

Add the SVG to the container. The div is assigned clip-path: url(#square), with width, height, x, and y coordinates specified in the CSS and adjusted on hover.

.reveal-url {
  background: url(https://www.pngfind.com/pngs/m/299-2991041_memes-para-stickers-png-png-download-surprised-pikachu.png)
    no-repeat;
  background-size: 150px;
  background-position: center;
  height: 300px;
  width: 300px;
  clip-path: url(#square);
  position: relative;
}

svg {
  width: 100%;
  height: 100%;
}

.reveal-url rect {
  transition: all 0.5s;
  x: 120px;
  y: 120px;
  width: 60px;
  height: 60px;
}


.reveal-url:hover rect {
  width: 100%; 
  height: 100%;
  x: 0;
  y: 0;
}

/*for example*/
body {
  background: linear-gradient(to bottom right, black 0%, white 100%);
  height: 100vh;
}

h1 {
  font-size: 30px;
  color: white;
}
<h1>This method works only in Chrome and Firefox.</h1>

<div class="reveal-url">
  <svg>
      <clipPath id="square">
  <rect />
      </clipPath>
  </svg>
</div>

#2 – Using Box-shadow

If dealing with solid background colors, one simple technique is to employ inset box-shadow to mask the container contents and then decrease the box shadow on hover.

.reveal {
  background: #000 url(https://www.pngfind.com/pngs/m/299-2991041_memes-para-stickers-png-png-download-surprised-pikachu.png) no-repeat;
  background-size: 150px;
  background-position: center;
  height: 300px;
  width: 300px;
  box-shadow: inset 0 0 0 120px #000;
  transition: box-shadow 1s;
}

.reveal:hover {
  box-shadow: inset 0 0 0 70px #000;
}


/*for example*/

body {
  background: #000;
}
<div class="reveal"></div>

Answer №2

The approach outlined above is effective, although it involves using the mask element as a child of the background element, rather than vice versa.

<div id="container">
  <div id="background>
    <div id="mask"></div>
  </div>
</div>

To achieve this effect, simply set the mask with 100% border and zero dimensions, then transition to zero border and 100% dimension (transparent). The mask should have a higher z-index for it to be displayed on top. Here's an example:

#container 
{
  width: 300px;
  height: 300px;
  background-color: black;
  position: relative;
}
#mask
{
  position: absolute;
  left: 0;
  top: 0;
  z-index: 10;
  height: 0;
  width: 0;
  transition: 0.4s ease;
  border: 150px solid black;
}
#background 
{
  background-image: url(https://www.pngfind.com/pngs/m/299-2991041_memes-para-stickers-png-png-download-surprised-pikachu.png);
  background-size: contain;
  width: 300px;
  height: 300px;
  display: block;
  margin: 1rem auto;
}
#background:hover #mask
{
  border: 0 solid black;
  width: 100%;
  height: 100%;
}

Check out this pen for a live demonstration: https://codepen.io/jaycodist/full/MWWEpMx

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

Forming triangles with outlines

Recently, I had the challenge of designing speech bubbles and found a clever technique to create the triangular tip at the end using CSS. By setting the element's width and height to 0 and playing around with borders, you can achieve diagonal shapes f ...

How can I load only specific images on a webpage using HTML?

I attempted to implement an image filter for my website by using the code below: <script> function myFunction() { // Initialize variables var input, filter, ul, li, a, i; input = document.getElementById('myInput'); filter = input.value.toU ...

Is it a scope issue if ng-click is not firing and the function is not working properly?

I'm facing an issue with an animation that is not working as intended. The goal is to have the search button trigger an animation to pull a search bar from the right side, allowing users to input their search query. However, the ng-click function does ...

Submenu malfunctioning in Internet Explorer and Chrome, but functioning properly in Firefox and Opera

I've been working on some HTML code that runs perfectly in FF and Opera, and even in IE for my friend. However, I'm having trouble getting the output to display properly in Chrome. Any ideas why this might be happening? <html> <head> ...

What is the best way to incorporate sound into a button using HTML or CSS for maximum impact?

Can anyone help me with a school project? I need to make a button play a sound when clicked. I've tried using both the <button> and <audio> tags, but they don't seem to be working for me. Perhaps CSS could be a solution. So far, the o ...

switch the visibility of the p tag based on its content

It seems like solving this shouldn't be too challenging, but I'm still learning when it comes to Javascript or JQuery. HTML: <p><span id="AddLine1Summary"></span>,</p> <p><span id="AddLine2Summary"></span& ...

Securing ASP.NET Core applications with iframe authentication cookies

I seem to be facing an issue with my iframe. Whenever I attempt to login, it appears that my authentication cookie is not functioning correctly as I keep getting redirected back to the login screen. How can I resolve this? The cookie operates normally whe ...

Utilize a unique font exclusively for Internet Explorer

I am encountering a significant issue trying to utilize custom fonts in Internet Explorer. Currently, I have implemented a specific font using @font-face and it works perfectly in modern browsers but fails to render properly in IE. The method I am curren ...

Using Regular expressions in JavaScript to eliminate content between two HTML comment tags

I am currently dealing with two HTML comments in this format: <!--Delete--> Blah blah blah blah <!--Delete--> I need to remove these comments, along with any characters and newlines. I am utilizing JavaScript and Grunt for this replacement ta ...

CSS slideshow animation is not functioning properly

Hey there, I'm new around here and I've got a question for you all. Please bear with me if I make any mistakes. So, I'm attempting to create a simple CSS slide image. In my animation code, I want the picture to fade from the left every time ...

What is the best way to display breadcrumb text on a new line within a pop up when the width exceeds without resorting to a scroll bar

Presently, my breadcrumb has a scrollable wrap with text overflow I want to make the overflowed text continue on the next line. How can I achieve this? The CSS code I am using for the image attached is as follows: .breadcrumb-css { text-overflow: ellip ...

Stepper wizard experiences a variation in CSS styling when minified

Two plunkers showcasing material design wizard selectors are causing different behavior upon stepper clicks. One uses a minified version of CSS while the other is expanded. The issue arises with the transition effects in the .step-wizard .progressbar class ...

Incorporate geographical data from a JSON file into my Google Maps application

Hey there, I'm a newbie on this forum and could really use your expertise. I've put together an html5 page with Google maps using the API key from Google (My code is shown below), it's working fine with a central marker in place and loads pe ...

Placing horizontal text over a Bootstrap 5 grid

Currently working my way through Boostrap5 while developing my website and I am extremely pleased with the progress so far. However, I have hit a snag - I want the name to appear vertically when hovering over the images on the bottom left, but for some rea ...

The wrapAll() method can be used to wrap list items within two columns

I am looking to group multiple li elements within two div containers by using jQuery's wrapAll method. The challenge lies in the fact that these items are rendered within a single <ul> element via a CMS. Here is the current setup: <ul> ...

using the GET method to transfer empty data

I am currently developing a basic search feature using php/sql with a primitive approach. Essentially, I am utilizing the GET method to send the form query across multiple fields in /search.php. This implies that the query will be sent in the following fo ...

Link PayPal payments to the individual making the payment

On my website, I offer in-game features that can be purchased through a miniature store with PayPal buy now buttons. I'm looking for the best and most secure method to automatically deliver these in-game bonuses to users after they have completed thei ...

Troubleshooting Problem with Creating a Button using HTML Checkbox "onclick" Event

My main goal with the checkbox click event is to achieve the following objectives: 1] Create a button with the id = 'id-of-checkbox'+'some-character-here' in a specific div. 2] Clicking on that button will remove both the button and ...

Struggling with incorporating a Carousel feature while navigating through an array

I am struggling to properly implement a carousel using the data from my Videos Array. Instead of getting the desired output where videos should slide one by one, all the videos in the Array are being rendered at the same time. <div id="carouselEx ...

Tips for creating a more condensed materialized table with form elements

Currently, I am working on a timesheet table using Materialize CSS with form elements in cells. However, I find the height of the table rows to be too large for my liking. Is there any way to make the table more compact? If this is a limitation of Materi ...