2015-06-26 123 views
3

我如何才能使斜坡像這樣會CSS3? FIDDLE我該如何製作斜坡

HTML

<div class="slope"> 
    Hello 
</div> 

CSS

.slope{ 
    width:500px; 
    height:100px; 
    background:#0093f7; 
    text-align:center; 
    font-size:26px; 
    color:#fff; 
    vertical-align:middle; 
} 

enter image description here

+5

粘貼您的問題到谷歌產生http://tympanus.net/codrops/2011/12/21/slopy-elements- with-css3 /作爲第一個結果。 –

回答

2

這消除了需要了解的主要DIV的寬度。知道了高度仍然是必要的,雖然......

.slope{ 
    width:500px; 
    height:100px; 
    background:#0093f7; 
    text-align:center; 
    font-size:26px; 
    color:#fff; 
    vertical-align:middle; 
    overflow: visible; 
    position: relative;  <--- this is important 
} 

.slope:after { 
    content: ""; 
    position: absolute;  <--- works with the above, such that positioning is relative to the main DIV 
    display: block; 
    right: -100px; 
    top: 0px; 
    width: 0px; 
    height: 0px; 
    border-top: solid 100px #0093f7; 
    border-right: solid 100px transparent; 
} 

小提琴: https://jsfiddle.net/vh1mk5yx/5/

+0

謝謝,我更喜歡這種方法 – Raihan

1
.slope{ 
    width:500px; 
    height:100px; 
    background:#0093f7; 
    text-align:center; 
    font-size:26px; 
    color:#fff; 
    vertical-align:middle; 
    float: left; 
    line-height: 100px; 
} 

.triangle { 
    float: left; 
    border-style: solid; 
    border-width: 100px 100px 0 0; 
    border-color: #0093f7 transparent transparent transparent; 
} 


<div class="slope"> 
    Hello 
</div> 
<div class="triangle"></div> 

你能結合這樣的兩個div嗎?

https://jsfiddle.net/vh1mk5yx/3/

2

基礎上,你知道的.slope的寬度和高度,其實這隻能。儘管如此,這裏是我的解決方案:

body{margin:0;} 
 
.slope{ 
 
    width:500px; 
 
    height:100px; 
 
    background:#0093f7; 
 
    text-align:center; 
 
    font-size:26px; 
 
    color:#fff; 
 
    vertical-align:middle; 
 
} 
 
.slope::after{ 
 
    content: " "; 
 
    display: block; 
 
    border-right: 50px solid transparent; 
 
    border-bottom: 50px solid transparent; \t 
 
    border-left: 50px solid #0093f7; 
 
    border-top: 50px solid #0093f7; 
 
    position:absolute; 
 
    left:500px; 
 
    top:0; 
 
}
<div class="slope"> 
 
    Hello 
 
</div>