Solution for applying CSS3 transforms to background images

Solution for applying CSS3 transforms to background images

CSS transformations, while cool, have not yet been applied to background images. This post provides a good workaround for when you really want to rotate a background image, or keep the background image constant when the container element rotates.

Use CSS3 transform properties to scale, skew, and rotate any element. It is supported in all modern browsers without vendor prefixes. (I've added -webkit- in case you want to support some older browsers.)

#myelement {
-webkit-transform: rotate(30deg);
transform: rotate(30deg);
}

Great stuff. However, this rotates the entire element - its content, borders, and background image. What if you only want to rotate the background image? Or if you want the background to remain constant when the element rotates

Currently, there is no W3C recommendation for background image transitions. This would be very useful, so I suspect there will be one eventually, but it won't help developers who want to use a similar effect today.

Fortunately, there is a solution. Essentially, this is a hack to apply the background image to the before or after pseudo-element, rather than the parent container. The pseudo-elements can then be transformed independently.

Convert only the background

The container element can have any styles applied to it, but it must be set to position:relative since our pseudo-element will be positioned within that element. You should also set overflow:hidden unless you are willing to have the background overflow outside of the container.

#myelement {
position: relative;
overflow: hidden;
}

We can now create an absolutely positioned pseudo-element with a transition background. The z-index is set to -1 to ensure it appears below the container content.

#myelement:before {
content: "";
position: absolute;
width: 200%;
height: 200%;
top: -50%;
left: -50%;
z-index: -1;
background: url(background.png) 0 0 repeat;
-webkit-transform: rotate(30deg);
transform: rotate(30deg);
}

Note that you may need to adjust the width, height, and position of the pseudo-element. For example, if you are using a repeating image, the rotated area must be larger than its container to completely cover the background:

Fixed background on transition element

Fixed background of transformed elements. All transformations on the parent container are applied to the pseudo-element. Therefore, we need to undo that transformation, for example, if the container is rotated 30 degrees, we have to rotate the background -30 degrees to get back to the fixed position:

#myelement {
position: relative;
overflow: hidden;
-webkit-transform: rotate(30deg);
transform: rotate(30deg);
}
#myelement:before {
content: "";
position: absolute;
width: 200%;
height: 200%;
top: -50%;
left: -50%;
z-index: -1;
background: url(background.png) 0 0 repeat;
-webkit-transform: rotate(-30deg);
transform: rotate(-30deg);

Again, you'll need to adjust the size and position to ensure the background adequately covers the parent container.

The effect works in all major browsers, reverting to version 9 in both Edge and Internet Explorer. IE8 will not show any transition, but will still show the background.

Summarize

The above is the solution for applying CSS3 transforms to background images that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time!

<<:  Solve the problem of ifconfig being unavailable in docker

>>:  Summary of 7 types of logs in MySQL

Recommend

Summary of several principles that should be followed in HTML page output

1. DOCTYPE is indispensable. The browser determin...

Detailed explanation of Nginx log customization and enabling log buffer

Preface If you want to count the source of websit...

Three implementation methods of Mysql copy table and grant analysis

How to quickly copy a table First, create a table...

Nginx reverse proxy learning example tutorial

Table of contents 1. Reverse proxy preparation 1....

Detailed steps for installing and debugging MySQL database on CentOS7 [Example]

This example requires downloading and installing ...

Class in front-end JavaScript

Table of contents 1. Class 1.1 constructor() 1.2 ...

A brief discussion on the design and optimization of MySQL tree structure tables

Preface In many management and office systems, tr...

Bootstrap 3.0 study notes CSS related supplement

The main contents of this article are as follows:...

How to simplify Redux with Redux Toolkit

Table of contents Problems Redux Toolkit solves W...

Practical method of upgrading PHP to 5.6 in Linux

1: Check the PHP version after entering the termi...

Comparison of the efficiency of different methods of deleting files in Linux

Test the efficiency of deleting a large number of...

Summary of Mysql update multi-table joint update method

Next, I will create two tables and execute a seri...

Do you know the meaning of special symbols in URL?

1.# # represents a location in a web page. The ch...