html – 在另一个div下面堆叠div的一角

前端之家收集整理的这篇文章主要介绍了html – 在另一个div下面堆叠div的一角前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图把蓝色方格div的角落放在橙色div下面.我尝试了我所知道的一切:
z-index不起作用,因为我的div被包裹在另一个div中,如果我打开它,我将在定位八个元素时遇到麻烦.

谁能告诉我怎么做?或者如何对所有元素使用z-index?

是)我有的:

我需要的:

到目前为止我的HTML

  1. body {
  2. background-color: #222;
  3. background-repeat: no-repeat;
  4. }
  5. #blueSquare {
  6. position: absolute;
  7. left: 15px;
  8. top: 15px;
  9. width: 50%;
  10. height: 170px;
  11. -webkit-transform: rotate(-45deg);
  12. }
  13. #rightTopblueSquare {
  14. height: 100%;
  15. width: 50%;
  16. position: relative;
  17. left: 50%;
  18. background-color: #7ab9c2;
  19. opacity: .99;
  20. }
  21. #leftBottomblueSquare {
  22. position: relative;
  23. top: -100%;
  24. height: 100%;
  25. width: 50%;
  26. background-color: #6baaae;
  27. }
  28. /*----------------------------------*/
  29. #greySquare {
  30. width: 50%;
  31. height: 170px;
  32. position: absolute;
  33. bottom: 15px;
  34. left: 15px;
  35. -webkit-transform: rotate(45deg);
  36. }
  37. #lefTopgreySquare {
  38. height: 100%;
  39. width: 50%;
  40. position: relative;
  41. left: 50%;
  42. background-color: #656f78;
  43. }
  44. #rightButtomgreySquare {
  45. position: relative;
  46. top: -100%;
  47. height: 100%;
  48. width: 50%;
  49. background-color: #313439;
  50. }
  51. /*----------------------------------*/
  52. #redSquare {
  53. width: 50%;
  54. height: 170px;
  55. position: absolute;
  56. right: 15px;
  57. bottom: 15px;
  58. -webkit-transform: rotate(-45deg);
  59. }
  60. #leftBottomredSquare {
  61. height: 100%;
  62. width: 50%;
  63. position: relative;
  64. left: 50%;
  65. background-color: #a2191d;
  66. }
  67. #rightTopredSquare {
  68. position: relative;
  69. top: -100%;
  70. height: 100%;
  71. width: 50%;
  72. background-color: #d63030;
  73. }
  74. /*----------------------------------*/
  75. #orangeSquare {
  76. width: 50%;
  77. height: 170px;
  78. position: absolute;
  79. right: 15px;
  80. top: 15px;
  81. -webkit-transform: rotate(45deg);
  82. z-index: -1;
  83. }
  84. #rightBottomorangeSquare {
  85. height: 100%;
  86. width: 50%;
  87. position: relative;
  88. left: 50%;
  89. background-color: #f42b06;
  90. }
  91. #lefttToporangeSquare {
  92. position: relative;
  93. top: -100%;
  94. height: 100%;
  95. width: 50%;
  96. background-color: #ff6a05;
  97. opacity: 1;
  98. }
  1. <div id="orangeSquare">
  2. <div id="rightBottomorangeSquare"></div>
  3. <div id="lefttToporangeSquare"></div>
  4. </div>
  5. <div id="redSquare">
  6. <div id="leftBottomredSquare"></div>
  7. <div id="rightTopredSquare"></div>
  8. </div>
  9. <div id="greySquare">
  10. <div id="lefTopgreySquare">leftTop</div>
  11. <div id="rightButtomgreySquare">rightBottom grey sqr</div>
  12. </div>
  13. <div id="blueSquare">
  14. <div id="rightTopblueSquare">rightTop</div>
  15. <div id="leftBottomblueSquare">LeftBotom blue sqr</div>
  16. </div>

解决方法

这可以使用CSS 3D变换来完成.首先,创建一个外部容器并将HTML包装在其中:
  1. #outer {
  2. position: relative;
  3. width: 500px;
  4. height: 400px;
  5. perspective: 1000px;
  6. transform-style: preserve-3d;
  7. }

外部容器具有较大的透视值,以防止元素在旋转时看起来不同.它使用transform-style:preserve-3d;覆盖默认堆叠引擎并将所有内容堆叠在3D上下文中.这可确保所有内容都正确堆叠.

然后,为了使元素正确重叠,只需在Y轴周围给每个元素一个5度的小扭曲:

  1. transform: ... rotateY(5deg);

你的替代元素会产生相反的变化:

  1. transform: ... rotateY(-5deg);

结果是一个在3d中有意义的场景,并且它完全堆叠在物理世界中.

工作,现场的例子:

  1. body {
  2. background-color: #222;
  3. background-repeat: no-repeat;
  4. }
  5. #blueSquare {
  6. position: absolute;
  7. left: 15px;
  8. top: 15px;
  9. width: 50%;
  10. height: 170px;
  11. -webkit-transform: rotateZ(-45deg) rotateY(5deg) ;
  12. transform: rotateZ(-45deg) rotateY(5deg) ;
  13. }
  14. #rightTopblueSquare {
  15. height: 100%;
  16. width: 50%;
  17. position: relative;
  18. left: 50%;
  19. background-color: #7ab9c2;
  20. }
  21. #leftBottomblueSquare {
  22. position: relative;
  23. top: -100%;
  24. height: 100%;
  25. width: 50%;
  26. background-color: #6baaae;
  27. }
  28. /*----------------------------------*/
  29. #greySquare {
  30. width: 50%;
  31. height: 170px;
  32. position: absolute;
  33. bottom: 15px;
  34. left: 15px;
  35. -webkit-transform:rotateZ(45deg) rotateY(-5deg) ;
  36. transform:rotateZ(45deg) rotateY(-5deg) ;
  37. }
  38. #lefTopgreySquare {
  39. height: 100%;
  40. width: 50%;
  41. position: relative;
  42. left: 50%;
  43. background-color: #656f78;
  44. }
  45. #rightButtomgreySquare {
  46. position: relative;
  47. top: -100%;
  48. height: 100%;
  49. width: 50%;
  50. background-color: #313439;
  51. }
  52. /*----------------------------------*/
  53. #redSquare {
  54. width: 50%;
  55. height: 170px;
  56. position: absolute;
  57. right: 15px;
  58. bottom: 15px;
  59. -webkit-transform: rotateZ(-45deg) rotateY(-5deg);
  60. transform: rotateZ(-45deg) rotateY(-5deg);
  61. }
  62. #leftBottomredSquare {
  63. height: 100%;
  64. width: 50%;
  65. position: relative;
  66. left: 50%;
  67. background-color: #a2191d;
  68. }
  69. #rightTopredSquare {
  70. position: relative;
  71. top: -100%;
  72. height: 100%;
  73. width: 50%;
  74. background-color: #d63030;
  75. }
  76. /*----------------------------------*/
  77. #orangeSquare {
  78. width: 50%;
  79. height: 170px;
  80. position: absolute;
  81. right: 15px;
  82. top: 15px;
  83. -webkit-transform: rotateZ(45deg) rotateY(5deg);
  84. transform: rotateZ(45deg) rotateY(5deg);
  85. }
  86. #rightBottomorangeSquare {
  87. height: 100%;
  88. width: 50%;
  89. position: relative;
  90. left: 50%;
  91. background-color: #f42b06;
  92. }
  93. #lefttToporangeSquare {
  94. position: relative;
  95. top: -100%;
  96. height: 100%;
  97. width: 50%;
  98. background-color: #ff6a05;
  99. }
  100.  
  101. #outer {
  102. position: relative;
  103. width: 500px;
  104. height: 400px;
  105. -webkit-perspective: 1000px;
  106. perspective: 1000px;
  107. -webkit-transform-style: preserve-3d;
  108. transform-style: preserve-3d;
  109. }
  1. <div id="outer">
  2. <div id="orangeSquare">
  3. <div id="rightBottomorangeSquare"></div>
  4. <div id="lefttToporangeSquare"></div>
  5. </div>
  6. <div id="redSquare">
  7. <div id="leftBottomredSquare"></div>
  8. <div id="rightTopredSquare"></div>
  9. </div>
  10. <div id="greySquare">
  11. <div id="lefTopgreySquare">leftTop</div>
  12. <div id="rightButtomgreySquare">rightBottom grey sqr</div>
  13. </div>
  14. <div id="blueSquare">
  15. <div id="rightTopblueSquare">rightTop</div>
  16. <div id="leftBottomblueSquare">LeftBotom blue sqr</div>
  17. </div>
  18. </div>

JSFiddle版本:https://jsfiddle.net/jjurL6j8/1/

猜你在找的HTML相关文章