Change the position of the background image when hovering over the div element

Looking for advice on how to create a div with a background image that only appears when hovered over. I also want the image to either zoom in or move to the sides while hovering. Here is my current progress.

.seg1 p:first-child {
  font-size: 20px;
  padding: 0;
  margin: 10% 0% 0% 10%;
}

.seg1 p {
  color: #363e3e;
  font-size: 32px;
  margin: 0% 0% 0% 10%;
}

.seg1 p:nth-child(3) {
  color: #ccc;
  font-size: 25px;
  margin: 0% 0% 0% 10%;
}

.seg1 {
  -webkit-border-radius: 400px;
  border: 1px solid #b1ebeb;
  height: 250px;
  width: 250px;
  float: left;
  background-color: #f1fbfb;
}

.seg1:hover {
  -webkit-transition: all 1s ease-in-out;
  -moz-transition: all 1s ease-in-out;
  -ms-transition: all 1s ease-in-out;
  -o-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;
  background-image: url(https://via.placeholder.com/350/000000/FFFFFF/?text=BackgroundImage);
}
<div class="seg1">
  <p> test </p>
  <p> dont </p>
  <p> bother </p>


</div>

Thank you in advance.

The answer may be subject to change

Instead, I'll provide a working example here.


.seg1 {
  -webkit-border-radius: 400px;
  border: 1px solid #b1ebeb;
  height: 250px;
  width: 250px;
  float: left;
  background-color: #f1fbfb;
}

.seg1:hover {
  animation-duration: 3s;
  animation-name: zoomin;
  animation-fill-mode: forwards;
  -webkit-animation-duration: 3s;
  -webkit-animation-name: zoomin;
  -webkit-animation-fill-mode: forwards;
  background-image: url('https://via.placeholder.com/350/000000/FFFFFF/?text=Background-image');
  background-position: center;
}

@keyframes zoomin {
  from {
    background-size: 100%;
  }
  to {
    background-size: 200%;
  }
}

@-webkit-keyframes zoomin {
  from {
    background-size: 100%;
  }
  to {
    background-size: 200%;
  }
}
<div class="seg1">
  <p>Click</p>
  <p>To send me an email</p>
  <p>For business enquiries</p>
</div>

Answer №1

Example: http://jsfiddle.net/abhitalks/xE4q4/

  1. In order to create a smooth transition effect, it is recommended to define your transition properties in the base class rather than within the :hover or other change pseudo-classes.
  2. Make sure to set a background for the element in the base class itself.
  3. Only specify the changes you want to apply within the :hover pseudo-class.

Sample CSS:

.seg1 {
    border-radius: 50%;
    border: 1px solid #b1ebeb;
    height: 250px; width: 250px;
    text-align: center;
    background: url('your image') no-repeat center center #f1fbfb;
    background-size: 0px;
    transition: all 1s ease-in-out;
}
.seg1:hover {
    background-size: 100%;
}

Answer №2

Another approach is to utilize CSS animation, which offers similar support to CSS transition (http://caniuse.com/css-animation, http://caniuse.com/css-transitions). However, the code tends to be lengthier due to the inclusion of -webkit prefix versions.

See a demonstration here: http://jsfiddle.net/32Qrb/3/

Here's the corresponding CSS:

.seg1{
-webkit-border-radius:400px;
border:1px solid #b1ebeb;
height:250px;
width:250px;
float:left;
background-color:#f1fbfb;
}

.seg1:hover{
    animation-duration: 3s;
    animation-name: zoomin;
    animation-fill-mode: forwards;
    -webkit-animation-duration: 3s;
    -webkit-animation-name: zoomin;
    -webkit-animation-fill-mode: forwards;

    background-image:url('http://lorempixel.com/256/256'); 
    background-position: center;
}

@keyframes zoomin {
  from {
      background-size: 100%;
  }

  to {
      background-size: 200%;
  }
}

@-webkit-keyframes zoomin {
  from {
      background-size: 100%;
  }

  to {
      background-size: 200%;
  }
}

Answer №3

If you're looking for an alternative solution, consider implementing the following:

http://codepen.io/9j4gm/

background-position: -50px center;

Experiment with changing this property:

background-size: contain;

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

What is the best way to eliminate all padding from a bootstrap toast notification?

I'm attempting to display a bootstrap alert message as an overlay toast (so it automatically disappears and appears above other elements). Issue: I am encountering a gap at the bottom of the toast and struggling to remove it: https://jsfiddle.net/der ...

Leveraging flot in conjunction with NPM, React, and Node

I've been attempting to utilize flot in a Node.js React project, but I'm running into issues with the import. The browser console is showing the error: Uncaught TypeError: _jquery2.default.plot is not a function This error is essentially the sa ...

Refreshing the content of a webpage with AJAX technology

There is a page on my website called logs.php that I have set to update every 4 seconds using jquery/ajax. This updates the details of an .xml file stored locally. file_put_contents("../../scripts/xml/logs.xml", $dom) or print_r(error_get_last()); Below ...

Creating a canvas with multiple label introductions can be achieved by following these

I am looking to group labels and sublabels on the x-axis of a canvas component. Currently, I only have sublabels like: RTI_0, RTI_1... I would like to add labels to these sublabels like: RTI = {RTI_0,RTI_1,RTI_2] BB = {BB_0, BB_1,BB_2,BB_3] <div cla ...

What is the process for adjusting the checkbox border color in Material Design 15 and above?

Ever since the latest update to Angular Material 15, I've noticed some changes in how styling is applied to Angular Material components. Previously, in version 14, I used to be able to accomplish styling changes with code like this: ::ng-deep .mat-che ...

ajax-triggered forms

Is there a way to ensure that a form functions correctly when it is included in the HTML loaded via AJAX? I am utilizing jQuery for this task. Any suggestions or advice would be appreciated. ...

The use of the django-piston method is prohibited

Below is the code snippet: handlers.py class ChatHandler(BaseHandler): model = ChatMsg allowed_methods = ('POST','GET') def create(self, request, text): message = ChatMsg(user = request.user, text = request.POST[& ...

Shifting gradient hues for illustrations?

One technique I have mastered is creating dynamic gradient colors for text elements using the following CSS: a { &:hover { background: linear-gradient(-45deg, green, yellow, red, blue); background-size: 200% 200%; animation: gradient-movi ...

The webpage is inaccessible only on mobile Safari, with an error message indicating a lack of internet

Initially, I do not possess an iPad, however, some clients have reported an unusual issue with one of my websites on the iPad. They are unable to access any page on the website, instead encountering a blank page with the error message: "Safari cannot open ...

Acquiring data from an XMLHttpRequest in PHP

I am currently using an ajax request to submit a form, which sends values to a PHP script that stores the data in a database. The following is my ajax post: $.ajax({ type:"POST", url: "wp-content/plugins/super- ...

Creating a dynamic AJAX function in an MVC framework to validate the ModelState

I am facing a challenge with my Ajax function that triggers automatically upon submitting a contact form. I need to ensure that this function only fires if the form passes model validation. Marriage.cs public class Marriage { [Required] [DisplayN ...

Add a variety of parameters to the URL in order to make an AJAX request

I want to dynamically add values from checked checkboxes to a URL for use in an AJAX call. I only want these values to be added to the URL if the checkbox is checked. If a user checks and then unchecks, I do not want that value included in the URL. Below ...

Are you looking to add some flair to your Flexbox layout using Media

I am in the process of developing a website using flexbox, and one key aspect of the design is to hide the navigation bar, specifically the left panel in this JSFiddle, when the viewport width is between 480px and 1023px. The current implementation achieve ...

Text that is not aligned in the middle and has a colored background

After setting up a flexbox container with some flex-items, I encountered an issue: When clicking on either "Overview" or "Alerts", the white background border is not displayed. To highlight the selected item, a class called .selected is triggered which ad ...

Is it just me or does this radio button in IE11 look like a pair of googly eyes?

Recently, I noticed a strange issue on a website I'm working on with radio buttons. While everything looks fine in most browsers, in IE11 some of the radio buttons appear like googly eyes. Could this be a bug specific to IE11 or am I missing something ...

Using jQuery's `fn.each` method to generate a JSON object

In this scenario, the code is utilizing jQuery.fn.each to create a variable that will store a JSON Object. However, an issue arises when attempting to add to the array, leading to a JavaScript error. var formfields = { step: $(this).data('step') ...

In a basic situation, the DOMContentLoaded event does not function the same way as $(document).ready

Having trouble listening for the DOMContentLoaded event in my webpack assets bundle for an Angular2 app. The main file only contains the following lines: // styles const bootstrapLoader = require('bootstrap-loader'); const styleScss = requ ...

Utilize jQuery $.ajax to integrate a RESTful WCF service within an MVC 4 web application

I'm currently working on an MVC 4 application and am attempting to consume a RESTful WCF service using the jQuery $.ajax function, but I'm encountering some issues. Here is the jQuery code I have written: $(document).ready(function () { $.ajax( ...

Having trouble positioning items to the right in bootstrap

<div id="unique-content-wrapper"> <nav class="navbar navbar-expand-lg navbar-light bg-light" id=""> <button class="navbar-toggler" id="menu-toggle"><span class="mater ...

When the width of a single table is large, Bootstrap tables can appear misaligned and not properly

I am facing an issue with displaying two tables side by side. Whenever the width of the first table is too large, the second table is pushed below it. https://i.sstatic.net/w8zwp.png https://i.sstatic.net/SZdZU.png Is there a way to make them stay side b ...