23

My background-attachment:fixed is working fine. But when I define CSS3 rotate on that DIV and scroll down then background-attachment:fixed stops working.

Check this http://jsfiddle.net/P3jS4/

Right now I am working on chrome18.

When you remove the rotate css then the background-attachment:fixed works fine.

http://jsfiddle.net/P3jS4/2

arttronics
  • 9,957
  • 2
  • 26
  • 62
sandeep
  • 91,313
  • 23
  • 137
  • 155
  • 1
    You dont explain what you want to achieve, so its hard to help you – Enrique Moreno Tent Mar 29 '12 at 15:35
  • @Dbugger remove the rotate css then the background-attachment:fixed work fine http://jsfiddle.net/P3jS4/2/ but add rotate it then it's creating a bug – sandeep Mar 29 '12 at 15:40
  • 1
    I don't have a solution yet, however it seems to be not so much an issue with `fixed` as it does with the `repeat-y` property, as it appears the background is not being repeated in the y-axis. Compare [this fiddle](http://jsfiddle.net/P3jS4/49/embedded/result/) on Chrome versus Firefox. It appears to be a rendering bug for Chrome. – ScottS May 10 '12 at 19:00
  • The bug is caused by the mere presence of the rotation transform as [this fiddle](http://jsfiddle.net/P3jS4/61/) (with `0deg` set) shows. – ScottS May 10 '12 at 19:27
  • Your issue may possibly be related to [this bug report](https://bugs.webkit.org/show_bug.cgi?id=15679), though it is unclear if that bug would cause the effect seen here. – ScottS May 10 '12 at 19:47
  • 1
    The same problem happens in Safari, so this is a WebKit bug. Bounty or not, I don't think anybody can help you with this (unless they go and fix the actual WebKit bug). This also seems to be buggy in Firefox 12 (I changed to `-webkit-` to `-moz-`). Maybe you should instead try to do this effect with the help of JavaScript. – thirtydot May 10 '12 at 23:10
  • I also think it's a bug but may be there is any fix of it. If not then javascript is my last option. – sandeep May 11 '12 at 00:23
  • 4
    This is an interesting question. I feel it deserves attention. – Koba May 11 '12 at 09:41
  • +1 webkit bug. http://jsfiddle.net/toadkicker/Htpup/19/ – Todd Baur May 13 '12 at 17:06
  • Ok, I had misunderstood your question at first. It's a cool effect you are going after. I don't know how you are implementing this, but if there is no way around the bug and you'd prefer to avoid JavaScript, then there's the option to rotate the image server side (like [PHP Rotate](http://php.net/manual/en/function.imagerotate.php)). Unless the image has to be rotated often... that might take up too much server processing. Just a thought :) – D.B. May 14 '12 at 00:06
  • I confirm, that it is webkit bug. – Bud Damyanov May 17 '12 at 08:26
  • Check out the latest [fiddle](http://jsfiddle.net/P3jS4/100/)I made that shows how to use **a working method** per Pankaj's post. – arttronics May 17 '12 at 19:52
  • This [fiddle](http://jsfiddle.net/P3jS4/102/) I made is alternate markup to show and hide **reference** image with rotated div image. – arttronics May 17 '12 at 20:47
  • Added New Answer with Recent Developments just now. – arttronics Jul 05 '12 at 01:30

5 Answers5

8

This is not a bug.

You're trying to do something the spec hasn't defined yet. It says in the comments:

What do fixed backgrounds do in transforms? They should probably ignore the transform completely, since - even transformed - the object should be acting as "porthole" through which the fixed background can be viewed in its original form.

The issue is that "fixed" means relative to the viewport (browser window), not the parent element. The viewport doesn't rotate and there is only one viewport.

Given the specification hasn't decided what the correct approach is then we don't know what you mean by "not work". If it "works" in other browsers that's because they have made a judgement without waiting for the spec to be updated. You should update your question to reflect the behaviour you want, not whether it "works" or not.

There is no point at this stage to expect a browser fix since you are working outside of the specification. My suggestion is to revise your code so you are following the specification through one of the following methods:

  • Using an img rather than a background
  • Using canvas or SVG
  • Rotating the background in a paint program
  • Putting the background on an element behind the rotated element

If none of these can be used, and no other workarounds are available then your answer is that it can't be done.

UPDATE: I just realised the spec I linked has been superceeded by a newer version which clarifies the expected behaviour as:

Fixed backgrounds are affected by any transform specified for the root element, and not by any other transforms.

Thus an element with a fixed background still acts like a "porthole" into an image that's fixed to the viewport, and transforms on the element affect the porthole, not the background behind it. On the other hand, transforming the root element will still transform everything on the page, rather than everything except for fixed backgrounds.

Assuming that by "not working" you mean the background doesn't rotate then what you are doing won't work in a conforming browser.

SpliFF
  • 38,186
  • 16
  • 91
  • 120
  • From comparing [this fiddle](http://jsfiddle.net/P3jS4/49/embedded/result/) between Chrome and Firefox, its not really the rotation that is the issue. There is no `repeat-y` for the fixed background. The image should be repeating in the y-axis on the viewport, like Firefox, but is not in Chrome, so I would think it should still be called a bug. Interesting info on the spec, however. – ScottS May 16 '12 at 12:14
  • well the question doesn't mention repeat-y at all but if that is actually the problem then the solution might be that the fiddle uses an incorrect ordering of parameters in the `background` shortcut. While some browsers support missing/reordered arguments i've found that in practice it's a bad idea. I'll confirm once i've installed chromium. – SpliFF May 16 '12 at 12:26
  • I should clarify--the rotation is an issue in that adding it is what seems to break the repeat-y in the fixed background element. – ScottS May 16 '12 at 12:46
  • @SpliFF If it's not a bug then there is a solution for this. – sandeep May 17 '12 at 05:52
  • **+1** I used your last bullet method for my recent answer, thank you. The only drawback is the content that is not rotated (i.e., behind the rotate element) can not have 100% interaction with it (e.g., Google Maps). Once CSS3 Masks develops, that might be a better solution. Cheers! – arttronics Jul 05 '12 at 23:28
3

I have a working solution that can use both fixed alongside transform rotate.

This info takes into consideration that they can't work together, but let's make them work separate.

Once you have some real content under the image, then will will prevent the bottom right corner of that image from touching the bottom right corner of the browsers viewport, which is the last valid location for using the fixed value. Because after that it's land of void.

Here's the fiddle!

This updated fiddle shows the rotate going the opposite direction.


Per our discussion on your exact requirements, I now have what your looking for. This method is a complete re-write and uses a mask effect combined with the rotate to allow the internal image to remain un-rotated... but that too can have it's own rotate!

Here's the NEW Fiddle with lots of comments that explains the CSS.

Status Update: None of my answers are correct because I did not realize the OP's complete requirements. Currently, the use of rotate with a fixed image is a non-standards method which was previously discussed above via link info. Perhaps a jQuery Parallax method is appropriate for this unique and challenging requirement.

arttronics
  • 9,957
  • 2
  • 26
  • 62
  • You give rotate to the HTML Tag. Which is not a good idea at all – sandeep May 17 '12 at 06:41
  • The HTML tag doesn't mind the rotate element on it. Is there a reason why it's not a good idea? – arttronics May 17 '12 at 07:14
  • If you rotate HTML the whole content inside that HTML is also rotate. – sandeep May 17 '12 at 07:19
  • That is why I have the wrapper that you put everything inside. The wrapper is a div with class name **horizon** which re calibrates the webpage contents. It's **common** to put classes with styles on **html and body tags** themselves, this is no difference from what's already done in tons of webpages. This fresh idea is markup that will **validate without error**. – arttronics May 17 '12 at 07:24
  • Please check my Fiddle example. I want a effect in which DIV is rotate but not image inside it. That's why i give -webkit-transform: rotate(-10deg); to my .pic DIV to get the affect. – sandeep May 17 '12 at 07:26
  • Both of your fiddles do not have the image rotated, they are both horizontal. Which fiddle? – arttronics May 17 '12 at 07:31
2

New Answer for Recent Developments: July 4, 2012


I have a jsFiddle work-in-progress that I would like to share with you and release early.

It should give you ideas on how to accomplish this particular style.

Particularly, it has a Parallax effect to it.

Note in unfinished Demo the image can be set in the div differently (it's set fixed and repeated), use CSS3 contain or cover for example for different Parallax effect for the content it's displaying.

It's not limited to images and any static content can be used instead since this object is never rotated/counter rotated which does not cause browser CPU hit.

jsFiddle Screenshot (it was captured zoomed out):
Mask is intentionally shown in the image below, it should match the background color. For select browsers, you can use CSS3 border-image property instead of colored mask (since yellow mask is actually border!). enter image description here


Example 1:
Here's a jsFiddle with the mask, opacities, and extra padding adjusted (to somewhat view as intended.).

enter image description here


Example 2:
Here's a jsFiddle as above with content shown with CSS3 cover method to illustrate punch-out effect for whole webpage.

enter image description here


I need to get back to this project soon. In the mean time, please feel free to use this early work however you wish. Cheers!

arttronics
  • 9,957
  • 2
  • 26
  • 62
0

I think that what you need to do is remove the background: ...fixed 0 0; property to get the image to rotate properly while not losing it when you scroll. The fiddle for that would be this; otherwise you could just make a rectangular image with a rotated rectangle inside of it which is transparent. This would be the image hack for the css that you're trying to accomplish.

jcrowley
  • 71
  • 2
  • 10
-2

Change background property to content property

.pic{
    background:url(http://cssglobe.com/lab/angled/img.jpg) fixed 0 0;
    height:300px;
    -webkit-transform: rotate(-10deg);
    border:1px solid green;
}

to

.pic{
    content:url(http://cssglobe.com/lab/angled/img.jpg) fixed 0 0;
    height:300px;
    -webkit-transform: rotate(-10deg);
    border:1px solid green;
}​
Pankaj
  • 954
  • 8
  • 15
  • sandeep, this **answer is the correct answer**. My answer uses a lot of CSS and isn't that great but {content:url} works indeed! I will update with fiddle based on this solution. – arttronics May 17 '12 at 11:50
  • My edit above needs to wait. So here's the updated [jsFiddle](http://jsfiddle.net/P3jS4/100/) at 100! Reminder: Chrome only. – arttronics May 17 '12 at 12:25
  • @arttronics--If I look at the fiddle that you state is the answer, I do not see it behaving in Chrome like [this fiddle](http://jsfiddle.net/P3jS4/49/embedded/result/) does in Firefox. The image scrolls (it should not) and the image is not rotated within the parent leaving red corners at top left and lower right. How Firefox is implementing it is how sandeep wants Chrome to show it. – ScottS May 17 '12 at 23:06
  • Thank you ScottS for clarifying what the OP wanted, as I though I had delivered what was expected. Also, I was working on a version exclusively for Chrome 18 and not Firefox whatsoever, which then makes my trials and efforts pointless. To be sure, the OP want's to use a rotate effect with a fixed background that is contained inside a scrolling div. I will try out a new idea and post back if it pans out. – arttronics May 18 '12 at 10:03