2015-08-05 16 views
8

Tôi đang cố gắng đặt góc của div hình vuông màu xanh dưới div màu da cam. Tôi đã thử tất cả mọi thứ tôi biết: z-index không hoạt động vì div của tôi được gói trong div khác, và nếu tôi mở nó, tôi sẽ gặp rắc rối với việc định vị tám phần tử.Xếp chồng một góc của một div bên dưới một div

Ai đó có thể cho tôi biết cách thực hiện việc này không? Hoặc làm thế nào để sử dụng z-index cho tất cả các yếu tố?

Những gì tôi có:

one

Những gì tôi cần:

two

HTML của tôi cho đến nay:

body { 
 
    background-color: #222; 
 
    background-repeat: no-repeat; 
 
} 
 
#blueSquare { 
 
    position: absolute; 
 
    left: 15px; 
 
    top: 15px; 
 
    width: 50%; 
 
    height: 170px; 
 
    -webkit-transform: rotate(-45deg); 
 
} 
 
#rightTopblueSquare { 
 
    height: 100%; 
 
    width: 50%; 
 
    position: relative; 
 
    left: 50%; 
 
    background-color: #7ab9c2; 
 
    opacity: .99; 
 
} 
 
#leftBottomblueSquare { 
 
    position: relative; 
 
    top: -100%; 
 
    height: 100%; 
 
    width: 50%; 
 
    background-color: #6baaae; 
 
} 
 
/*----------------------------------*/ 
 
#greySquare { 
 
    width: 50%; 
 
    height: 170px; 
 
    position: absolute; 
 
    bottom: 15px; 
 
    left: 15px; 
 
    -webkit-transform: rotate(45deg); 
 
} 
 
#lefTopgreySquare { 
 
    height: 100%; 
 
    width: 50%; 
 
    position: relative; 
 
    left: 50%; 
 
    background-color: #656f78; 
 
} 
 
#rightButtomgreySquare { 
 
    position: relative; 
 
    top: -100%; 
 
    height: 100%; 
 
    width: 50%; 
 
    background-color: #313439; 
 
} 
 
/*----------------------------------*/ 
 
#redSquare { 
 
    width: 50%; 
 
    height: 170px; 
 
    position: absolute; 
 
    right: 15px; 
 
    bottom: 15px; 
 
    -webkit-transform: rotate(-45deg); 
 
} 
 
#leftBottomredSquare { 
 
    height: 100%; 
 
    width: 50%; 
 
    position: relative; 
 
    left: 50%; 
 
    background-color: #a2191d; 
 
} 
 
#rightTopredSquare { 
 
    position: relative; 
 
    top: -100%; 
 
    height: 100%; 
 
    width: 50%; 
 
    background-color: #d63030; 
 
} 
 
/*----------------------------------*/ 
 
#orangeSquare { 
 
    width: 50%; 
 
    height: 170px; 
 
    position: absolute; 
 
    right: 15px; 
 
    top: 15px; 
 
    -webkit-transform: rotate(45deg); 
 
    z-index: -1; 
 
} 
 
#rightBottomorangeSquare { 
 
    height: 100%; 
 
    width: 50%; 
 
    position: relative; 
 
    left: 50%; 
 
    background-color: #f42b06; 
 
} 
 
#lefttToporangeSquare { 
 
    position: relative; 
 
    top: -100%; 
 
    height: 100%; 
 
    width: 50%; 
 
    background-color: #ff6a05; 
 
    opacity: 1; 
 
}
<div id="orangeSquare"> 
 
    <div id="rightBottomorangeSquare"></div> 
 
    <div id="lefttToporangeSquare"></div> 
 
</div> 
 
<div id="redSquare"> 
 
    <div id="leftBottomredSquare"></div> 
 
    <div id="rightTopredSquare"></div> 
 
</div> 
 
<div id="greySquare"> 
 
    <div id="lefTopgreySquare">leftTop</div> 
 
    <div id="rightButtomgreySquare">rightBottom grey sqr</div> 
 
</div> 
 
<div id="blueSquare"> 
 
    <div id="rightTopblueSquare">rightTop</div> 
 
    <div id="leftBottomblueSquare">LeftBotom blue sqr</div> 
 
</div>

+1

Điều bạn cần không chỉ thay đổi chỉ mục z, những gì bạn cần là chuyển đổi 3D thực sự. http://www.w3schools.com/css/css3_3dtransforms.asp – abimelex

+0

đã thử, cả div z-index: 1; và div đó vị trí cao hơn được trên đầu trang, không có vấn đề làm thế nào để bạn quay nó trong tất cả 3 dimentions X Y Z –

Trả lời

7

Điều này có thể được thực hiện bằng cách sử dụng các biến đổi CSS 3D. Trước tiên, hãy tạo vùng chứa bên ngoài và gói mã HTML của bạn trong đó:

#outer { 
    position: relative; 
    width: 500px; 
    height: 400px; 
    perspective: 1000px; 
    transform-style: preserve-3d; 
} 

Thùng chứa bên ngoài có giá trị phối cảnh lớn để giữ cho các yếu tố trông khác khi chúng ta xoay chúng. Nó sử dụng transform-style: preserve-3d; để ghi đè lên công cụ xếp chồng mặc định và ngăn xếp mọi thứ trong ngữ cảnh 3D. Điều này đảm bảo rằng mọi thứ đều ổn định.

Sau đó, để có được các yếu tố của bạn để chồng lên nhau đúng chỉ cung cấp cho mỗi phần tử một twist nhỏ 5 độ quanh trục Y:

transform: ... rotateY(5deg); 

yếu tố thay thế của bạn sẽ nhận được xoắn ngược lại:

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

Kết quả là một cảnh có ý nghĩa trong 3d, và ngăn xếp chính xác nó sẽ như thế nào trong thế giới vật lý.


làm việc, ví dụ sống:

body { 
 
    background-color: #222; 
 
    background-repeat: no-repeat; 
 
} 
 
#blueSquare { 
 
    position: absolute; 
 
    left: 15px; 
 
    top: 15px; 
 
    width: 50%; 
 
    height: 170px; 
 
    -webkit-transform: rotateZ(-45deg) rotateY(5deg) ; 
 
      transform: rotateZ(-45deg) rotateY(5deg) ; 
 
} 
 
#rightTopblueSquare { 
 
    height: 100%; 
 
    width: 50%; 
 
    position: relative; 
 
    left: 50%; 
 
    background-color: #7ab9c2; 
 
} 
 
#leftBottomblueSquare { 
 
    position: relative; 
 
    top: -100%; 
 
    height: 100%; 
 
    width: 50%; 
 
    background-color: #6baaae; 
 
} 
 
/*----------------------------------*/ 
 
#greySquare { 
 
    width: 50%; 
 
    height: 170px; 
 
    position: absolute; 
 
    bottom: 15px; 
 
    left: 15px; 
 
    -webkit-transform:rotateZ(45deg) rotateY(-5deg) ; 
 
      transform:rotateZ(45deg) rotateY(-5deg) ; 
 
} 
 
#lefTopgreySquare { 
 
    height: 100%; 
 
    width: 50%; 
 
    position: relative; 
 
    left: 50%; 
 
    background-color: #656f78; 
 
} 
 
#rightButtomgreySquare { 
 
    position: relative; 
 
    top: -100%; 
 
    height: 100%; 
 
    width: 50%; 
 
    background-color: #313439; 
 
} 
 
/*----------------------------------*/ 
 
#redSquare { 
 
    width: 50%; 
 
    height: 170px; 
 
    position: absolute; 
 
    right: 15px; 
 
    bottom: 15px; 
 
    -webkit-transform: rotateZ(-45deg) rotateY(-5deg); 
 
      transform: rotateZ(-45deg) rotateY(-5deg); 
 
} 
 
#leftBottomredSquare { 
 
    height: 100%; 
 
    width: 50%; 
 
    position: relative; 
 
    left: 50%; 
 
    background-color: #a2191d; 
 
} 
 
#rightTopredSquare { 
 
    position: relative; 
 
    top: -100%; 
 
    height: 100%; 
 
    width: 50%; 
 
    background-color: #d63030; 
 
} 
 
/*----------------------------------*/ 
 
#orangeSquare { 
 
    width: 50%; 
 
    height: 170px; 
 
    position: absolute; 
 
    right: 15px; 
 
    top: 15px; 
 
    -webkit-transform: rotateZ(45deg) rotateY(5deg); 
 
      transform: rotateZ(45deg) rotateY(5deg); 
 
} 
 
#rightBottomorangeSquare { 
 
    height: 100%; 
 
    width: 50%; 
 
    position: relative; 
 
    left: 50%; 
 
    background-color: #f42b06; 
 
} 
 
#lefttToporangeSquare { 
 
    position: relative; 
 
    top: -100%; 
 
    height: 100%; 
 
    width: 50%; 
 
    background-color: #ff6a05; 
 
} 
 

 
#outer { 
 
    position: relative; 
 
    width: 500px; 
 
    height: 400px; 
 
    -webkit-perspective: 1000px; 
 
      perspective: 1000px; 
 
    -webkit-transform-style: preserve-3d; 
 
      transform-style: preserve-3d; 
 
}
<div id="outer"> 
 
    <div id="orangeSquare"> 
 
     <div id="rightBottomorangeSquare"></div> 
 
     <div id="lefttToporangeSquare"></div> 
 
    </div> 
 
    <div id="redSquare"> 
 
     <div id="leftBottomredSquare"></div> 
 
     <div id="rightTopredSquare"></div> 
 
    </div> 
 
    <div id="greySquare"> 
 
     <div id="lefTopgreySquare">leftTop</div> 
 
     <div id="rightButtomgreySquare">rightBottom grey sqr</div> 
 
    </div> 
 
    <div id="blueSquare"> 
 
     <div id="rightTopblueSquare">rightTop</div> 
 
     <div id="leftBottomblueSquare">LeftBotom blue sqr</div> 
 
    </div> 
 
</div>

JSFiddle Version: https://jsfiddle.net/jjurL6j8/1/

+1

Genius, đây là giải pháp chính xác – Evochrome

+0

Nhìn vào câu trả lời của tôi, đơn giản nhất và làm việc tốt hơn =) –

0

Bạn có thể sao chép hình vuông màu cam một phần bằng cách mã hóa cứng hoặc bằng cách sử dụng một số thư viện JS. Sau đó, đặt chỉ mục z cao hơn hình vuông màu xanh lam. Nếu bạn cắt chính xác nó sẽ không chồng lên hình vuông màu đỏ.

Đó không phải là giải pháp hoàn hảo và nó gây ra các sự cố khác, ví dụ: khi văn bản chồng lên đường viền của phần tử được nhân đôi và bị cắt.

Kỹ thuật này cũng được sử dụng trong các phiên bản cũ hơn của Photoshop, nơi không thể tạo thành phần 3D.

1

giải pháp đơn giản của câu đố này là trùng lặp cuối cùng div và thiết lập opacity cho ông

Có mã HTML và CSS dưới đây:

<body> 
<div id="orangeSquare"> 
     <div id="rightBottomorangeSquare"></div> 
     <div id="lefttToporangeSquare"></div> 
    </div> 
     <div id="orangeSquare2"> <!- this new line-> 
    <div id="rightBottomorangeSquare2"></div> 
    <div id="lefttToporangeSquare2"></div> 
     </div><!- this new line end-> 
    <div id="redSquare"> 
     <div id="leftBottomredSquare"></div> 
     <div id="rightTopredSquare"></div> 
    </div> 
    <div id="greySquare"> 
     <div id="lefTopgreySquare">leftTop</div> 
     <div id="rightButtomgreySquare">rightBottom grey sqr</div> 
    </div> 
    <div id="blueSquare"> 
     <div id="rightTopblueSquare">rightTop</div> 
     <div id="leftBottomblueSquare">LeftBotom blue sqr</div> 
    </div> 

Và thêm vào CSS đầu tiên đoạn mã này CSS:

#orangeSquare2 { 
    width: 50%; 
    height: 170px; 
    position: absolute; 
    right: 15px; 
    top: 15px; 
    -webkit-transform: rotate(45deg); 
    z-index: -1; 
} 
#rightBottomorangeSquare2 { 
    height: 100%; 
    width: 50%; 
    position: relative; 
    left: 50%; 
    background-color: #f42b06; 

} 
#lefttToporangeSquare2 { 
    position: relative; 
    top: -100%; 
    height: 100%; 
    width: 50%; 
    background-color: #ff6a05; 
    opacity: 0; 
} 

này hoạt động tuyệt vời =) và thay đổi với kích thước của cửa sổ đây ảnh

Solution

Các vấn đề liên quan