20

In the example of http://jsfiddle.net/sqk3k2nq/ , the elements are set to be fixed at the same position.

HTML:

<div>
 <span>This is A</span>
 <div>
 <span>This is B</span>
 <div>
 <span>This is C</span>
 </div>
 </div>
</div>

CSS:

div {
 border: 1px solid red;
 position: fixed;
 top: 20px;
 left: 20px;
}

However, if we add perspective: 1000px; to div, the position will look like relative instead of fixed. See http://jsfiddle.net/sqk3k2nq/1/

Why setting perspective changes it to be relative?
For some reason, I have to set perspective to it and I wish A, B, and C can still be in the same position. Is it possible?

asked Oct 15, 2014 at 15:14
10
  • just guessing, but could you use z-index? Commented Oct 15, 2014 at 15:17
  • @MrCoder How exactly? Commented Oct 15, 2014 at 15:18
  • 1
    z-index doesn't seem to do much in this context. Commented Oct 15, 2014 at 15:19
  • @RobertHarvey what happened to your answer? Was working fine for me in chrome? Commented Oct 15, 2014 at 15:27
  • @MrCoder: It's not a 3D element in the fiddle. I'm not convinced that it works at all. I think Fiddle is just ignoring it. Commented Oct 15, 2014 at 15:29

1 Answer 1

20

Why does perspective changes fixed position in CSS?

Because perspective establishes a containing block similar to the way position: relative; does, which is stated in the transform module:

Perspective

The use of this property with any value other than none establishes a stacking context. It also establishes a containing block (somewhat similar to position: relative), just like the transform property does.

Since you're applying a perspective to every div in your example (and top, left properties) each div creates its own containing block and ends up pushed 20px to the right and bottom of the previous div.

answered Oct 15, 2014 at 15:29
Sign up to request clarification or add additional context in comments.

2 Comments

Sounds convincing. But what can I do about it? Even if I set perspective only to the parent, it will look like jsfiddle.net/sqk3k2nq/2 . Is there any chance that A, B, C be at the same position and they are all fixed?
I don't think it's possible (without making significant changes to your CSS).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.