Tips for seamlessly placing a div with a background image within a parent container, all while preventing any scrolling issues as the child div rotates

I created a simple demo featuring a square image with rounded corners to give it a circular appearance. The image rotates slowly, but as it moves diagonally, the horizontal width fluctuates, causing the parent container to resize. I want the parent container to maintain 100% width while keeping the content inside without affecting its size. You can see the issue in action here (image courtesy of this):

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.container {
  display: flex;
  flex-shrink: 0;
  padding: 64px;
  width: 100vw;
}

.bg-image {
  background-image: url(https://i.imgur.com/9mhwcLH.png);
  background-size: contain;
  -webkit-transform-origin: 50% 50%;
  -ms-transform-origin: 50% 50%;
  transform-origin: 50% 50%;
  background-position: center center;
  background-repeat: no-repeat;
  -webkit-animation: spin 512s linear infinite;
  animation: spin 512s linear infinite;
  height: 0;
  padding-top: 100%;
  background-size: contain;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  border-radius: 50%;
}
<div class="container">
  <div class="bg-image"></div>
</div>

There are two potential solutions for this issue, both of which would work for me:

  1. The parent container should maintain a constant width since the underlying div is transformed into a circle. This means that changes in the square div's width should not impact the parent.
  2. Alternatively, you could resize the child element so that at a 45-degree angle, it perfectly fits within the parent, shrinking as it rotates. Both approaches would be helpful, and knowing how to implement them would be advantageous for future use.

If you have any ideas on how to resolve this issue and prevent the parent from resizing or being affected by the child's width changes, please share your suggestions!

Answer №1

I have updated the code to ensure that the child element inherits the height and width of its parent.

It is important to note that the parent's height and width are set in a 1:1 ratio, resulting in the desired circular effect for the child element.

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.container {
  display: flex;
  flex-shrink: 0;
  padding: 64px;
  height:300px;
  width: 300px; /* Maintain a 1:1 ratio for parent's height and width */
}

.bg-image {
  background-image: url(https://i.imgur.com/9mhwcLH.png);
  background-size: contain;
  -webkit-transform-origin: 50% 50%;
  -ms-transform-origin: 50% 50%;
  transform-origin: 50% 50%;
  background-position: center center;
  background-repeat: no-repeat;
  -webkit-animation: spin 512s linear infinite;
  animation: spin 512s linear infinite;
  height: 0;
  
  border-radius: 50%;
  width:100%;
  height:100%; /* Inherit parent's height and width */
}
<div class="container">
  <div class="bg-image"></div>
</div>

Answer №2

If you need to trim down the overflowing section of the container, try this method

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.container {
  display: flex;
  flex-shrink: 0;
  padding: 64px;
  width: 100vw;
  overflow-x: clip;
}

.bg-image {
  background-image: url(https://i.imgur.com/9mhwcLH.png);
  background-size: contain;
  -webkit-transform-origin: 50% 50%;
  -ms-transform-origin: 50% 50%;
  transform-origin: 50% 50%;
  background-position: center center;
  background-repeat: no-repeat;
  -webkit-animation: spin 512s linear infinite;
  animation: spin 512s linear infinite;
  height: 0;
  padding-top: 100%;
  background-size: contain;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  border-radius: 50%;
}
<div class="container">
  <div class="bg-image"></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

Issue with jQuery: addClass not toggling within an IF statement

Is there a way to dynamically add the class "disable" to an anchor tag when a user selects the radio button labeled "Remove"? Currently, in the provided fiddle, the class is added as soon as the page loads. I have included code in the script that successf ...

Tips for retaining the original size of an image in HTML5 canvas drawImage

function getBase64ImageData(_id) { var canv = document.createElement('CANVAS'); var context = canv.getContext("2d"); var imageElem = document.getElementById(_id); context.drawImage(imageElem, 0, 0); var dataURL = canv.toDat ...

Changing position of element upon appearance of span in React

Currently, I am working with React and facing an issue where a span element appears whenever a user hovers over a text element. The problem is that the existing text shifts leftwards when the span appears (to the right of the text). The desired behavior ...

Is it possible to customize the background color of the 'rows per page' selector box in Bootstrap 4 bootstrap-table?

The text is set in white on a dark grey background by default and appears just below the formatted table. Best regards, Phil Please refer to the attached image:Section of table showing rows per page selector box ...

Gathering Servlet details from non-form elements

I am currently in the process of developing an application that is capable of parsing JSON strings. At this stage, I am able to input a JSON string using Java, write it to an HTML document, and then have a JavaScript program read and parse it before ultima ...

Adjusting the text color for select values within a <td> element

I have a table that is populated with data retrieved from an API. Each cell in the table contains multiple pieces of data. Below is the code used to create the table: <td class="myclass"> <?php $myvar = explode(" ", $json["data"]); ...

The alignment of the list is off when using Flexbox on smaller screens

When my flexbox shifts into a column layout at 600 pixels, the list in the second box loses its center alignment on smaller screens. Despite trying various margin and padding adjustments within and outside of media queries, the content remains too far to t ...

Putting Text Inside a Video Player in HTML

Is there a way to insert text, like a Logo, into my video player? I would appreciate any help on how to achieve this. Thank you. <video width="320" height="240" controls src="video/flashtrailer.mp4"> Your browser does not support the video tag. & ...

Generating small image previews in JavaScript without distorting proportions

I am currently working on a client-side Drag and Drop file upload script as a bookmarklet. To prepare for the upload process, I am utilizing the File API to convert the images into base64 format and showcase them as thumbnails. These are examples of how m ...

Error encountered during post-installation process for package `[email protected]`. The script `node scripts/build.js` failed to execute, resulting in an exit status of 1

https://i.sstatic.net/LC5wq.pngWhenever I run the gulp serve command for my AngularJS Fuse project, I encounter this error. C:\wamp\www\bank_admin_post_login\Fuse-1.4.1-demo>gulp serve C:\wamp\www\bank_admin_post_log ...

adjust the size of a form field to match the background image dimensions

I'm encountering an issue while trying to solve this particular challenge: My goal is to integrate a login box onto a full-screen image. Although I've come across numerous methods for incorporating an image into a form field, my task requires me ...

Trouble with Bootstrap Popover's visibility

My current setup involves a popover that is not initializing properly. The code I am using is shown below: HTML: <div data-toggle="popover" data-placement="bottom" data-content="xyz">...</div> JavaScript: $(function () { $('[data-t ...

Angular Dynamic Alert Service

Is it possible to customize the text in an Angular Alert service dynamically? I'm looking to make certain words bold, add new lines, and center specific parts of the text. Specifically, I want the word "success" to be bold and centered, while the rest ...

How to Manage Empty Lines in HTML Documents within WordPress

Just recently, I launched my brand new website. Everything seems to be functioning properly except for one issue that I discovered in the HTML source code. There are 15 blank lines before "<!doctype html>", which is causing some problems with SEO and ...

Using Vuetify to send a parameter from a select option to a method as a parameter

Just starting out with vuejs and encountering an issue passing parameters from a selected option to my JavaScript method. In my updateClientIsMaster method, the item is always undefined. However, when I added v-on:change="updateClientIsMaster in the & ...

Display a sneak peek on a separate tab

I have an editor on my website where users can input and edit their own HTML code. Instead of saving this to a database, I want to display the current user's HTML code in a new window using JavaScript. How can I achieve this without storing the code p ...

Two elements occupy the rest of the vertical space

In the center, there is a div with content inside. I want the top and bottom divs to occupy the remaining space. Similar to this example <div class="container"> <div class="top"> </div> <div class="middle"> C ...

How does Facebook access the new hashtags when they are clicked on?

I've been developing a website recently and I'm really impressed with the way a popup page appears when you click on a hashtag. I've been wanting to incorporate a similar feature for a while now, but I'm not sure how to do it. Could thi ...

The functionality of the "enter" key is disabled in the textarea of Internet Explorer 8

Pressing enter in a textarea with the nowrap whitespace attribute set through CSS does not create a new line in IE8. Instead, just a simple whitespace appears. Other browsers like Opera, Chrome, and Firefox do not have this issue. Any solutions for this pr ...

Creating personalized dots in the owl carousel

I have successfully added custom previous and next buttons to my carousel using Owl Carousel, but I am having trouble displaying the navigation dots. Can anyone help me identify where I might have made a mistake? // owl.carousel.css .owl-controls { ...