How to tilt inner element forward when tilting outer back with css 3d transforms

久未见 提交于 2019-12-11 10:59:36

问题


I would like to get an effect of an outer element tilting back along the z axis and an inner element standing up out of it in a normal 2d perspective. In other words given this html

<div id="outer">
  <div id="inner"></div>
</div>

I would like the outer element to tilt backwards with its side lines approaching a vanishing point while the inner element's sides are vertical.

How do I achieve this? Using the css

div {
  display: inline-block;
}
#outer {
  padding: 20px;
  background-color: blue;
  -webkit-transform: perspective(400px) rotateX(45deg)
}

#inner {
  height: 150px;
  width: 150px;
  background-color: tomato;
  background-image: url(http://www.w3schools.com/html/smiley.gif);
  background-size: 100%;    
}

I thought I could just do

#inner {
  -webkit-transform: rotateX(-45deg);
}

but that just makes it tilt more. Here is a jsbin


回答1:


You need to set preserve 3d

#outer {
  padding: 20px;
  background-color: blue;
  -webkit-transform: perspective(400px) rotateX(45deg);
  -webkit-transform-style: preserve-3d;
}

#inner {
  height: 150px;
  width: 150px;
  background-color: tomato;
  background-image: url(http://www.w3schools.com/html/smiley.gif);
  background-size: 100%;   
  -webkit-transform: rotateX(-45deg);
  -webkit-transform-origin: bottom center;
}

jsbin



来源:https://stackoverflow.com/questions/23373737/how-to-tilt-inner-element-forward-when-tilting-outer-back-with-css-3d-transforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!