Transitioning background with background sizing set to cover

I have been searching for an answer to this question, but haven't found one yet.

Currently, I have a series of divs with background-images set to 'background-size: cover'.

My goal is to make the images zoom in and grow on hover. However, it seems that this transition doesn't work when using the 'cover' attribute. The image zooms without the desired transition effect, going instantly from 'cover' to 110% size. It works properly if the original background-size was set as 100%.

Despite this, setting the background-size to 100% causes the image to tile somewhat behind the div when resized, which is not ideal. The 'cover' attribute keeps the image central at all times, which is what I prefer.

If anyone has advice on how to achieve a transition while maintaining the 'cover' effect, it would be greatly appreciated.

Ilmiont

Answer №1

It's important to note that when using CSS animations for background-size, you cannot use keyword values like 'cover'.

For more information, please visit: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties

In summary:

The property background-size can have a list of length, percentage, or calc(); the values get interpolated accordingly. However, keywords are not animatable.

To achieve a similar effect, one workaround is to wrap the element with the background image in a container with overflow hidden and apply a scale transform.

.wrapper { 
  width:300px;
  height:400px;
  overflow:hidden;
}
.image {
  background:url("http://placekitten.com/g/500/500");
  background-size:cover;
  width:100%;
  height:100%;
  transition: transform 2s;
}

.image:hover { transform:scale(1.1) }
<div class="wrapper">
  <div class="image"></div>
</div>

Answer №2

My solution to the issue involved loading the image within a nested div element.

<div class='slide'>
    <div class='img' style='background-image:url(img/slide1.jpg)'></div>    
</div>

I then made adjustments to the size of the image container as follows:

  #slideshow .slide {
    position:absolute;
    top:0%;
    left:0%;
    height:100%;
    width:100%;
    background-position:center center;
    overflow:hidden;
  } 

   #slideshow .img {
      position:absolute;
      background-size:cover;
      top:-5%;
      left:-5%;
      width:110%;
      height:110%;
      transition: width 1s, height 1s, top 1s, left 1s;  
   }  

  #slideshow .active .img{
      width:100%;
      height:100%;
      top:0;
      left:0;
  } 

The full markup for the slideshow is shown below:

<div id='slideshow' class='slider' >
    <div class='slides'>
        <div class='slide'>
            <div class='img' style='background-image:url(img/slide1.jpg)'></div>
            <div class='caption'>
                <hr />
                <h1>PRIVATE HAVEN</h1>
            </div>
        </div>
        <div class='slide'>
            <div class='img' style='background-image:url(img/slide2.jpg)'></div>
            <div class='caption'>
                <hr />
                <h1>BLISSFUL SATISFACTION</h1>
            </div>
        </div>
        <div class='slide' >
            <div class='img' style='background-image:url(img/slide3.jpg)'></div>
            <div class='caption'>
                <hr />
                <h1>LIVE IN NATURE AT THE MOUNTAIN’S DOOR</h1>
            </div>
        </div>
    </div>  
</div>

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

Is it possible to implement the 'opacity' feature on the homepage of my HTML website?

I am currently working on my website and I would like to display a notice to members when they open the site. This notice should only appear once, right above the HTML homepage, as the homepage content may vary for different members. I plan to use 'op ...

Is the height of the element determined by the parent rather than the content?

I need help with a layout featuring three containers - Total (with a red border), Left (yellow border containing an image), and Right (blue border containing text). Left is set to approximately 30% width with float: left, while Right is set to 70% width w ...

Can you help me create a CSS Toggle similar to this design?

I am currently working on a website and I need to create a FAQs page with a specific layout. When a user clicks on a question (Q), the answer (A) should open smoothly using jQuery animation. Only one question should be visible at a time. If a user clicks ...

Addressing the spacing and alignment issues within the progress bar

I need some help with my Angular app. I am currently working on creating a progress bar, but I am facing some issues with the alignment of the bars. Here is the code snippet that I have used: CSS: .progressbar { position: relative; height: 56px; mar ...

Exploring the world of child theme customization in WordPress

I am looking to make some modifications to my website. Working with a child theme, I have plans to adjust the CSS and make some structural changes. Modifying the CSS seems pretty straightforward as I just need to reference the classes or IDs and input new ...

display child element at full width within a parent element that is not full width

I have a container with a maximum width of 1024px, and I want one of its child elements to take up 100% width starting from the left edge of the screen. Is there a way to achieve this without adding extra HTML markup? I know I can make the child element 1 ...

What steps can be taken to resolve the link prefetch warning in a Next.js application?

I am currently working on a Next.js application using version 13.4, and I've encountered a warning in the console: After preloading the resource at <URL>, it was not used within a few seconds of the window's load event. Please ensure that i ...

css hover function not operational with back to top button

I have implemented a back to top button using the following resource: Although the button is functioning as expected, I am encountering an issue with the hover effect. In Chrome inspector, manually forcing hover on .cd-top does not apply the CSS style as ...

Looking to display a different HTML document in a div - any suggestions?

I am in the process of creating a website that will feature four tiles on the front page. Once a tile is clicked, I would like the content to appear in a window that has a slight transparency to allow the main page to show through. So far, I have managed ...

Grab all the text using Java Jsoup

I am having an issue with the code I have written. The doc.body.text() statement is not displaying the text content within the style and script tags. I examined the .text() function code and found that it searches for all instances of TextNode. Can someone ...

Focus on targeting dynamic IDs such as #event_rules_attributes_0_interval, #event_rules_attributes_1_interval, etc. using CSS3 styling

Styling input tags using their IDs can be very convenient due to the higher precedence weight they hold compared to classes. For instance, I set a default width for input.text elements within a specific container: .details .metadata input.text { width: 2 ...

Troubleshooting problems with the height of nested DIV elements

Issue I am attempting to replicate the layout depicted in the image below. The black area represents the body with id="library", the grey div is id="body" (which is not visible due to lack of margins for inner divs), the light blue div is id="sideBar", th ...

Images in the navigation bar are bouncing around on the page, even though the CSS and HTML configurations

We are experiencing some erratic behavior with our nav bar images that seems to occur only on page refreshes. The issue appears to be related to the loading of our sprite, which contains all the images for the nav bar links. Despite trying different float ...

Using Angular/Typescript to interact with HTML5 Input type Date on Firefox (FF)

Are there any Angular/Typescript projects that are completely built without relying on third-party libraries? I am encountering problems with Firefox and IE11. It works fine on Chrome where the value can be read, but the calendar does not display when us ...

Interactive Dropdown-Buttons dynamically inserted

I have been utilizing a third-party library called Materializecss from this link: My issue arises when attempting to create a Dropdown feature upon clicking a button. It works perfectly fine when I manually place the button in the HTML and click on it, tr ...

Obtaining the field name from a SQL Server database by utilizing the field ID

When viewing my selections, I have several drop-down lists that display the ID and I would like the name to be shown to the user for clarity. However, when I list everything that has been added, it displays the chosen ID instead of the name. I attempted t ...

Placing numbers at the top of the HTML table cell for better visibility

I am currently utilizing a two-column HTML table to showcase a list of rules. In one column, I have the rule's number, while in the other, I have the rule itself. However, I've noticed that the rule numbers are appearing near the bottom of their ...

Adjusting the gap between TableRows in Material-UI

Is there a way to increase the spacing between TableRow MaterialUI components in my code? <S.MainTable> <TableBody> {rows.map(row => { return ( <S.StyledTableRow key={row.id}> <TableCell component="th" s ...

Denied rendering of design due to incompatible MIME type

Just delved into the world of node / express and decided to test my skills by creating a simple tip calculator app. However, I seem to have hit a roadblock with loading my CSS. The console keeps throwing this error: "Refused to apply style from &apos ...

What is the process for altering the color of an HTML output depending on its values?

I created a simple HTML code to showcase some outcomes. The possible results are SUCCESS, Failure, and Still Failing. I want these results to be displayed with corresponding colors, such as green for SUCCESS, and red for both Failure and Still Failing. I ...