Adjusted the background scale transformation without impacting the content inside the div

My concern revolves around the transform: scale property. I am trying to achieve a gradual zoom effect on my background when hovered over, without affecting the text content. I have omitted browser prefixes for brevity.

Below is the CSS code:

#cont-nl {
    background: url('../img/nl.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    height: 60vh;
    background-position: center;
    background-attachment: fixed;
    margin-right: auto;
    margin-left: auto;
    transition: transform 2s ease-in-out;
}


#cont-nl:hover {
    transform: scale(1.05);
    transition: transform 5s ease-in-out;
}

Here is the corresponding HTML:

 <div class="row" id="cont-nl">
        <div class="container t-blk-center">
            <div class="">
                <h1 class="align-left">Next Level Youth Ministry</h1>
                <div class="tab-brk"></div>
                <h3 class="align-left">For students 6th-12th grade.</h3>
            </div>
        </div>
 </div>

Answer №1

The perfect solution

To achieve a zoom effect, consider using either percentage or pixel unit size for the background-size property. You can also add a hover transition to increase the background-size.

An Illustration

#cont-nl {
  background: url(https://i.sstatic.net/cAEjm.jpg);
  background-repeat: no-repeat;
  background-size: 150%;
  height: 60vh;
  background-attachment: fixed;
  margin-right: auto;
  margin-left: auto;
  background-position: center;
  transition: background-size 6s;
}
#cont-nl:hover {
  background-size: 200%;
}
<div class="row" id="cont-nl">
  <div class="container t-blk-center">
    <div class="">
      <h1 class="align-left">Next Level Youth Ministry</h1>
      <div class="tab-brk"></div>
      <h3 class="align-left">For students 6th-12th grade.</h3>
    </div>
  </div>
</div>

A Drawback

It's worth noting that this approach may not work well with centrally placed background images as it can lead to a glitchy transition in various browsers.


An alternate method

Illustrative Example

When the element doesn't inherit width and height from its parent:

You could place the background image on a pseudo-element. This pseudo-element would be:

  • positioned absolutely with properties like left: 0 / top: 0

  • given a z-index: -1 to ensure it stays beneath text layers

  • sized appropriately with height: 60vh and width: 100vw, matching the parent element

The parent element of the pseudo-element should have position: relative set.

#cont-nl {
    height: 60vh;
    position: relative;
    overflow: hidden;
}

#cont-nl:before {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    content: '';
    background: url(https://i.sstatic.net/cAEjm.jpg) no-repeat;
    background-position: center;
    background-size: cover;
    transition: transform .5s ease-in-out;
    z-index: -1;
}

#cont-nl:hover:before {
    transform: scale(1.05);
    transition: transform .5s ease-in-out;
}
<div class="row" id="cont-nl"> 
        <div class="container t-blk-center">
            <div class="">
                <h1 class="align-left">Next Level Youth Ministry</h1>
                <div class="tab-brk"></div>
                <h3 class="align-left">For students 6th-12th grade.</h3>
            </div>
        </div> 
 </div>


Different Approach

If the width and height can be inherited from the parent element:

You could use a similar technique by placing the background image on a pseudo-element, which is then:

  • positioned absolutely with dimensions stretching across the parent element

  • assigned a z-index: -1 for correct layering under text

Remember to set the parent element's position to relative.

#cont-nl {
    height: 60vh;
    position: relative;
    overflow: hidden;
}

#cont-nl:before {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    content: '';
    background: url(https://i.sstatic.net/cAEjm.jpg) no-repeat;
    background-position: center;
    background-size: cover;
    transition: transform .5s ease-in-out;
    z-index: -1;
}

#cont-nl:hover:before {
    transform: scale(1.05);
    transition: transform .5s ease-in-out;
}
<div class="row" id="cont-nl"> 
        <div class="container t-blk-center">
            <div class="">
                <h1 class="align-left">Next Level Youth Ministry</h1>
                <div class="tab-brk"></div>
                <h3 class="align-left">For students 6th-12th grade.</h3>
            </div>
        </div> 
 </div>

Regarding Browser Prefixes

Check out caniuse.com to determine if CSS properties are natively supported. Often, prefixes like -webkit- or -moz- may not be required for widely supported properties such as transform and transition.

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 utilize variable fonts with Google Fonts?

Traditional fonts typically require a separate file for each weight variant, such as 300, 400, and 500. However, with variable fonts, all weight combinations can be accessed through a single compact font file. This is extremely advantageous for web design. ...

Unresolved conflict stemming from an HTML closing tag in the index.html file, despite both source and destination files being identical

I am currently facing a challenge while trying to merge my code with the master branch through a pull request. The conflict arises in the src/index.html file, specifically on line 17 which states </html> needs to be corrected to </html>. It&apo ...

Strategies for avoiding the opening of a new tab when clicking on a link in ReactJs

RenderByTenantFunc({ wp: () => ( <Tooltip id="tooltip-fab" title="Home"> <Button className="home-button"> <a href={ ...

Can minification of JS be achieved in a Jekyll environment?

Currently, I am in the process of developing a project with Jekyll and one of the requirements is to minify HTML, CSS, and JS. I was wondering if Jekyll has built-in features for JS minification. It may seem like a simple question, but since I am new to ...

Eliminate perfect-scrollbar functionality on mobile devices

I have applied the perfect-scrollbar class with 'height: auto' on mobile screens, expecting it to work properly. It's functioning well in responsive mode on the web. However, it does not scroll on mobile devices. @media (max-width: 992px) { ...

Display a PHP warning message when attempting to add methods to ajax jQuery

Recently, I have delved into the world of ajax and jQuery while working on my final year project. However, I have encountered a problem when adding methods to my JavaScript code. Here is an excerpt from my ajax script: $(document).ready(function() { $ ...

What is the best way to display just the border of a background image while hiding the content?

The demonstration at this codepen https://codepen.io/Chester/pen/QPoyjN showcases a border animation effect. While it works effectively with a white background, I am interested in making the box's background dynamic. <div class="rainbow"& ...

Adding a pathway to a clip path: a step-by-step guide

While attempting to create a wave effect on an image, I hit a roadblock. There are two SVGs involved - one with the path I want to use and the other behaving as expected but with the wrong clip path/shape. However, when I try to insert the desired path, ...

What is the best method for effectively organizing and storing DOM elements alongside their related objects?

In order to efficiently handle key input events for multiple textareas on the page, I have decided to create a TextareaState object to cache information related to each textarea. This includes data such as whether changes have been made and the previous co ...

The menu on the webpage in smartphone mode is infiltrating other divs on the page

Encountering an issue with the TwitterBootstrap CSS menu on this page when viewed on a smartphone: The div containing the menu does not resize its height, causing it to invade the content below when the "Menu" is clicked. I've been unable to find a ...

Saving user input from a PHP form into a text file

Below is a simple PHP form that performs a calculation when submitted. Now, I want to save the inputs (height, weight) and calculation result to a text file without displaying the path of the file. Ideally, I would like each submission to add a new line in ...

Changing Images with Jquery on Click Event

This section of the HTML document contains an image link that is designed to switch between two images when clicked. The images in question are timeline-hand and hand-clicked. Clicking on the image should change it from one to the other and vice versa. Ho ...

Aligning Font Awesome icons inside md-buttonsFinding the perfect alignment for

I am attempting to center font awesome icons within a button so that they align perfectly with the text on a toolbar. Below is the code snippet I am working with: <div ng-app="app"> <md-toolbar> <div class="md-toolbar-tools" md-tall ...

Tips for utilizing the output of a pre-defined function within another function in SASS

How can I effectively utilize the outcome of darken( $var, 10% ) in the SASS function oppositeColor( $darkenedColor )? The code I am working with currently looks like this: $color1: #000000 !default; $color2: darken( $color1, 5% ) !default; $color3: oppos ...

Can HTML be rendered within classes?

In the admin section of a website, I am working with multiple CRUD tables that require displaying success, information, or error messages when certain actions are performed, like deleting a record. This is a common practice that involves wrapping messages ...

Chrome Automatically Playing WebRTC Audio

My webapp has webrtc functionality, but I've noticed a strange issue. When connections are established between Chrome browsers, the audio is not played, while video is. However, this problem doesn't occur with Mozilla users. So... Chrome user 1 ...

Preventing jQuery slideToggle functionality from toggling multiple elements at once

I am currently working on a project where I have a series of images with captions that appear underneath each one. To show or hide the caption for each image, I am using slideToggle when the image is clicked. $('.imageholder').click(function() ...

Issues with Bootstrap Navbar Dropdown functionality, including flickering or unresponsiveness

While working on a project, I incorporated the FlexStart Bootstrap Template. However, I encountered an issue with the Dropdown feature on the navbar. When hovering over the menu and submenu, they flicker incessantly. Despite investing a considerable amount ...

Choosing a specific column in an HTML table using jQuery based on the text within it

I have a table with a similar structure as shown below: <table> <c:forEach ...> <tr> <td>test</td> // this is the initial value <td>random value</td> <td>random value</td&g ...

Using ng-options with the Add New feature is not compatible with the select statement

Hello everyone, I have some JSON values that look like this: [{"Employee":{"Emp_id":"EF00001","First_name":"Aruna","Last_name":"Jayalath"}}] Unfortunately, I am having trouble retrieving the values within the select statement when running this function i ...